diff --git a/README.md b/README.md index 74dde2e..21e1b3b 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,9 @@ This project consists of an IRC server implementation and a load testing tool de ### Running the IRC Server To start the IRC server, run the following command: ```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 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 - Total bytes sent and received - Send and receive rates in MB/s - - diff --git a/irc_server.c b/irc_server.c index dc75e97..0964b5d 100644 --- a/irc_server.c +++ b/irc_server.c @@ -69,6 +69,22 @@ static Channel *channels = NULL; static int listen_fd; static int epoll_fd; 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) { 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[c->channel_count++] = ch; + logmsg("User '%s' joined channel '%s'", c->nickname[0]?c->nickname:"", ch->name); } 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; } } + logmsg("User '%s' left channel '%s'", c->nickname[0]?c->nickname:"", ch->name); } 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) { + logmsg("Message received from '%s': %s", c->nickname[0]?c->nickname:"", line); char *cmd = strtok(line, " "); if (!cmd) return; if (strcasecmp(cmd, "NICK") == 0) { @@ -417,6 +436,7 @@ static void remove_client(Client *c) { while (prev && prev->next != c) prev = prev->next; if (prev) prev->next = c->next; } + logmsg("User '%s' disconnected", c->nickname[0]?c->nickname:""); del_epoll(c->fd); if (c->fd >= 0) close(c->fd); c->fd = -1; @@ -436,12 +456,26 @@ static void accept_new_client() { c->next = clients; clients = c; add_epoll(fd, EPOLLIN, c); + logmsg("User connected from %s (fd=%d)", c->host, fd); } int main(int argc, char **argv) { int port = 6667; - if (argc > 1) port = atoi(argv[1]); - if (argc > 2) password = argv[2]; + int argi = 1; + 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); int opt = 1;