Added logging.
This commit is contained in:
parent
911fbb4681
commit
020138aa7f
@ -29,9 +29,9 @@ This project consists of an IRC server implementation and a load testing tool de
|
|||||||
### Running the IRC Server
|
### Running the IRC Server
|
||||||
To start the IRC server, run the following command:
|
To start the IRC server, run the following command:
|
||||||
```bash
|
```bash
|
||||||
./irc_server
|
./irc_server --log
|
||||||
```
|
```
|
||||||
The server will listen on port 6667 by default.
|
The server will listen on port 6667 by default and will log messages to the terminal when the `--log` flag is used.
|
||||||
|
|
||||||
### Running the Load Testing Tool
|
### Running the Load Testing Tool
|
||||||
To execute the load testing tool, run:
|
To execute the load testing tool, run:
|
||||||
@ -47,5 +47,3 @@ After running the load test, the following metrics will be displayed:
|
|||||||
- Messages per second
|
- Messages per second
|
||||||
- Total bytes sent and received
|
- Total bytes sent and received
|
||||||
- Send and receive rates in MB/s
|
- Send and receive rates in MB/s
|
||||||
|
|
||||||
|
|
||||||
|
38
irc_server.c
38
irc_server.c
@ -69,6 +69,22 @@ static Channel *channels = NULL;
|
|||||||
static int listen_fd;
|
static int listen_fd;
|
||||||
static int epoll_fd;
|
static int epoll_fd;
|
||||||
static char *password = NULL;
|
static char *password = NULL;
|
||||||
|
static int logging_enabled = 0;
|
||||||
|
|
||||||
|
static void logmsg(const char *fmt, ...) {
|
||||||
|
if (!logging_enabled) return;
|
||||||
|
time_t now = time(NULL);
|
||||||
|
struct tm tm;
|
||||||
|
char tbuf[32];
|
||||||
|
gmtime_r(&now, &tm);
|
||||||
|
strftime(tbuf, sizeof(tbuf), "%Y-%m-%dT%H:%M:%SZ", &tm);
|
||||||
|
fprintf(stderr, "%s ", tbuf);
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vfprintf(stderr, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
}
|
||||||
|
|
||||||
static void fatal(const char *msg) {
|
static void fatal(const char *msg) {
|
||||||
perror(msg);
|
perror(msg);
|
||||||
@ -151,6 +167,7 @@ static void add_client_to_channel(Client *c, Channel *ch) {
|
|||||||
c->channels = realloc(c->channels, c->channel_capacity * sizeof(Channel*));
|
c->channels = realloc(c->channels, c->channel_capacity * sizeof(Channel*));
|
||||||
}
|
}
|
||||||
c->channels[c->channel_count++] = ch;
|
c->channels[c->channel_count++] = ch;
|
||||||
|
logmsg("User '%s' joined channel '%s'", c->nickname[0]?c->nickname:"<unreg>", ch->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void remove_client_from_channel(Client *c, Channel *ch) {
|
static void remove_client_from_channel(Client *c, Channel *ch) {
|
||||||
@ -170,6 +187,7 @@ static void remove_client_from_channel(Client *c, Channel *ch) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logmsg("User '%s' left channel '%s'", c->nickname[0]?c->nickname:"<unreg>", ch->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Client *find_client_by_nick(const char *nick) {
|
static Client *find_client_by_nick(const char *nick) {
|
||||||
@ -238,6 +256,7 @@ static void handle_registration(Client *c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void handle_line(Client *c, char *line) {
|
static void handle_line(Client *c, char *line) {
|
||||||
|
logmsg("Message received from '%s': %s", c->nickname[0]?c->nickname:"<unreg>", line);
|
||||||
char *cmd = strtok(line, " ");
|
char *cmd = strtok(line, " ");
|
||||||
if (!cmd) return;
|
if (!cmd) return;
|
||||||
if (strcasecmp(cmd, "NICK") == 0) {
|
if (strcasecmp(cmd, "NICK") == 0) {
|
||||||
@ -417,6 +436,7 @@ static void remove_client(Client *c) {
|
|||||||
while (prev && prev->next != c) prev = prev->next;
|
while (prev && prev->next != c) prev = prev->next;
|
||||||
if (prev) prev->next = c->next;
|
if (prev) prev->next = c->next;
|
||||||
}
|
}
|
||||||
|
logmsg("User '%s' disconnected", c->nickname[0]?c->nickname:"<unreg>");
|
||||||
del_epoll(c->fd);
|
del_epoll(c->fd);
|
||||||
if (c->fd >= 0) close(c->fd);
|
if (c->fd >= 0) close(c->fd);
|
||||||
c->fd = -1;
|
c->fd = -1;
|
||||||
@ -436,12 +456,26 @@ static void accept_new_client() {
|
|||||||
c->next = clients;
|
c->next = clients;
|
||||||
clients = c;
|
clients = c;
|
||||||
add_epoll(fd, EPOLLIN, c);
|
add_epoll(fd, EPOLLIN, c);
|
||||||
|
logmsg("User connected from %s (fd=%d)", c->host, fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int port = 6667;
|
int port = 6667;
|
||||||
if (argc > 1) port = atoi(argv[1]);
|
int argi = 1;
|
||||||
if (argc > 2) password = argv[2];
|
while (argi < argc) {
|
||||||
|
if (strcmp(argv[argi], "--log") == 0) {
|
||||||
|
logging_enabled = 1;
|
||||||
|
argi++;
|
||||||
|
} else if (argi == 1) {
|
||||||
|
port = atoi(argv[argi]);
|
||||||
|
argi++;
|
||||||
|
} else if (argi == 2) {
|
||||||
|
password = argv[argi];
|
||||||
|
argi++;
|
||||||
|
} else {
|
||||||
|
argi++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
int opt = 1;
|
int opt = 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user