From 5ec7b93a7a68dbb88954fb47467f35b5efc61576 Mon Sep 17 00:00:00 2001 From: Aaron Blakely Date: Sat, 9 Mar 2024 02:40:06 -0600 Subject: [PATCH] added logger.c --- .gitignore | 2 +- Makefile | 1 + lib/irc.h | 1 + lib/logger.h | 19 ++++++++++++++++++ lib/util.h | 2 ++ src/channel.c | 10 +++++----- src/config.c | 8 ++++++-- src/db.c | 6 +++--- src/events.c | 4 ++-- src/irc.c | 2 +- src/logger.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 8 +++++--- src/module.c | 6 +++--- xbot.cfg | 3 +++ xbot.vcxproj | 2 ++ 15 files changed, 108 insertions(+), 20 deletions(-) create mode 100755 lib/logger.h create mode 100755 src/logger.c diff --git a/.gitignore b/.gitignore index 4e6bb04..6ea9d54 100755 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ Release mods/test .cache UpgradeLog*.XML - +xbot.log xbot.db *.so *.dll diff --git a/Makefile b/Makefile index 9d30eb8..3f16378 100755 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ main: $(CC) $(CFLAGS) $(SRC)/channel.c -o $(OBJ)/channel.o $(CC) $(CFLAGS) $(SRC)/timers.c -o $(OBJ)/timers.o $(CC) $(CFLAGS) $(SRC)/db.c -o $(OBJ)/db.o + $(CC) $(CFLAGS) $(SRC)/logger.c -o $(OBJ)/logger.o $(CC) -o $(EXEC) $(OBJECTS) $(BINFLAGS) @echo "All Done!" diff --git a/lib/irc.h b/lib/irc.h index dad7d71..edfbca3 100755 --- a/lib/irc.h +++ b/lib/irc.h @@ -36,6 +36,7 @@ struct irc_conn char real_name[512]; char db_file[256]; + char log_file[256]; struct db_table *db; // I/O Buffers diff --git a/lib/logger.h b/lib/logger.h new file mode 100755 index 0000000..3939094 --- /dev/null +++ b/lib/logger.h @@ -0,0 +1,19 @@ +#ifndef LOGGER_H +#define LOGGER_H + +#include + +struct logger +{ + FILE *log; + char log_file[256]; +}; + +extern struct logger logger; + +void log_init(char *file); +void log_close(); + +void xlog(char *fmt, ...); + +#endif diff --git a/lib/util.h b/lib/util.h index db8986e..8776554 100755 --- a/lib/util.h +++ b/lib/util.h @@ -7,6 +7,8 @@ #ifndef UTIL_H #define UTIL_H +#include "logger.h" + #ifdef _WIN32 #define true TRUE #define false FALSE diff --git a/src/channel.c b/src/channel.c index 3520d14..357321e 100755 --- a/src/channel.c +++ b/src/channel.c @@ -16,7 +16,7 @@ void add_channel(char *name) if (channel_exists(name) == 1) return; - printf("Adding channel %s\n", name); + xlog("Adding channel %s\n", name); channels[chan_count] = calloc(1, sizeof(struct channel)); strlcpy(channels[chan_count]->name, name, 32); @@ -35,7 +35,7 @@ void remove_channel(char *name) { if (!strcmp(channels[i]->name, name)) { - printf("Removing channel %s\n", name); + xlog("Removing channel %s\n", name); free(channels[i]->users); free(channels[i]); @@ -122,7 +122,7 @@ void add_user_to_channel(char *user, char *host, char *chan) if (user_exists(chan, user) == 1) return; - printf("Adding user %s!%s to channel %s\n", user, host, chan); + xlog("Adding user %s!%s to channel %s\n", user, host, chan); for (i = 0; i < chan_count; i++) { @@ -181,7 +181,7 @@ void remove_user_from_channel(char *user, char *chan) { if (!strcmp(channels[i]->users[j].nick, user)) { - printf("Removing user %s from channel %s\n", user, chan); + xlog("Removing user %s from channel %s\n", user, chan); for (j = j; j < channels[i]->user_count; j++) { @@ -286,7 +286,7 @@ void user_quit(char *nick) { if (!strcmp(channels[i]->users[j].nick, nick)) { - printf("Removing user %s from channel %s\n", nick, channels[i]->name); + xlog("Removing user %s from channel %s\n", nick, channels[i]->name); for (j = j; j < channels[i]->user_count; j++) { diff --git a/src/config.c b/src/config.c index f8b953a..0594aba 100755 --- a/src/config.c +++ b/src/config.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "irc.h" #include "util.h" @@ -20,7 +21,7 @@ struct irc_conn read_config(struct irc_conn bot, char *file) if (!config_read_file(cf, file)) { - printf("[xbot.cfg:%d] Configuration error: %s\n", + xlog("[xbot.cfg:%d] Configuration error: %s\n", config_error_line(cf), config_error_text(cf) ); @@ -44,6 +45,9 @@ struct irc_conn read_config(struct irc_conn bot, char *file) if (config_lookup_string(cf, "bot.db", &base)) strlcpy(bot.db_file, base, sizeof bot.db_file); + if (config_lookup_string(cf, "bot.log", &base)) + strlcpy(bot.log_file, base, sizeof bot.log_file); + config_destroy(cf); return bot; @@ -63,7 +67,7 @@ void run_autoload(struct irc_conn *bot) if (!config_read_file(cf, "xbot.cfg")) { - printf("[xbot.cfg:%d] Configuration error: %s\n", + xlog("[xbot.cfg:%d] Configuration error: %s\n", config_error_line(cf), config_error_text(cf) ); diff --git a/src/db.c b/src/db.c index d067d18..be66eb4 100755 --- a/src/db.c +++ b/src/db.c @@ -27,7 +27,7 @@ int db_write(struct db_table *db, char *fname) fullpath = realpath(fname, NULL); #endif - printf("Writing db to file: %s\n", fullpath); + xlog("Writing db to file: %s\n", fullpath); // write the header fwrite(db, sizeof(struct db_table), 1, fp); @@ -65,7 +65,7 @@ struct db_table *db_read(char *fname) // check the magic value if (db->db_magic != DB_MAGIC) { - printf("Error: %s incompatible or unknown db file format: Bad Magic\n", fname); + xlog("Error: %s incompatible or unknown db file format: Bad Magic\n", fname); return NULL; } @@ -74,7 +74,7 @@ struct db_table *db_read(char *fname) // check the version if (db->db_ver != DB_VER) { - printf("Error: %s incompatible or unknown db file format: Incompatible Version\n", fname); + xlog("Error: %s incompatible or unknown db file format: Incompatible Version\n", fname); return NULL; } diff --git a/src/events.c b/src/events.c index 3c71c3b..6750d66 100755 --- a/src/events.c +++ b/src/events.c @@ -43,7 +43,7 @@ void init_events() MY_API int add_handler(char *type, void *handler) { int i; - printf("Installing handler @ %p [type: %s]\n", handler, type); + xlog("Installing handler @ %p [type: %s]\n", handler, type); for (i = 0; i < handlers_count; i++) { @@ -62,7 +62,7 @@ MY_API int add_handler(char *type, void *handler) } else { - printf("Handler array is full, cannot add more handlers.\n"); + xlog("Handler array is full, cannot add more handlers.\n"); return -1; } } diff --git a/src/irc.c b/src/irc.c index 2680b25..b881ba4 100755 --- a/src/irc.c +++ b/src/irc.c @@ -126,7 +126,7 @@ void irc_connect(struct irc_conn *bot) eprint("Error: Cannot connect to host '%s'\n", bot->host); } - printf("Connected!\n"); + xlog("Connected!\n"); bot->srv_fd = FDOPEN(srv_fd, "r+"); #endif } diff --git a/src/logger.c b/src/logger.c new file mode 100755 index 0000000..1a1866b --- /dev/null +++ b/src/logger.c @@ -0,0 +1,54 @@ +#include "logger.h" +#include "util.h" + +#include +#include +#include +#include +#include + +struct logger logger; + +void log_init(char *file) +{ + logger.log = fopen(file, "a"); + strlcpy(logger.log_file, file, sizeof logger.log_file); +} + +void log_close() +{ + fclose(logger.log); +} + +void xlog(char *fmt, ...) +{ + va_list args; + time_t tv; + + char *buf = calloc(8128, sizeof(char)); + char *msg = calloc(4096, sizeof(char)); + char *tbuf = calloc(64, sizeof(char)); + + va_start(args, fmt); + vsnprintf(msg, 4096, fmt, args); + + time(&tv); + + strftime(tbuf, 64, "%Y-%m-%d %H:%M:%S", localtime(&tv)); + + sprintf(buf, "[%s] %s", tbuf, msg); + printf("%s", buf); + + // write to log file + if (logger.log) + { + fprintf(logger.log, "%s", buf); + fflush(logger.log); + } + + va_end(args); + + free(buf); + free(msg); + free(tbuf); +} diff --git a/src/main.c b/src/main.c index 009bcf9..0522b93 100755 --- a/src/main.c +++ b/src/main.c @@ -100,6 +100,8 @@ int main(int argc, char **argv) // Read the config bot = read_config(bot, conf); + log_init(bot.log_file); + // check if the db exists, if not, create it #ifdef _WIN32 if (access(bot.db_file, 0) == -1) @@ -107,7 +109,7 @@ int main(int argc, char **argv) if (access(bot.db_file, F_OK) == -1) #endif { - printf("Creating database file: %s\n", bot.db_file); + xlog("Creating database file: %s\n", bot.db_file); bot.db = (struct db_table *)malloc(sizeof(struct db_table)); memset(bot.db, 0, sizeof(struct db_table)); set_bot_db(bot.db); @@ -119,7 +121,7 @@ int main(int argc, char **argv) } else { - printf("Reading database file: %s\n", bot.db_file); + xlog("Reading database file: %s\n", bot.db_file); bot.db = db_read(bot.db_file); set_bot_db(bot.db); } @@ -128,7 +130,7 @@ int main(int argc, char **argv) run_autoload(&bot); // Connect to the server - printf("Connecting to %s...\n", bot.host); + xlog("Connecting to %s...\n", bot.host); irc_connect(&bot); trespond = time(NULL); diff --git a/src/module.c b/src/module.c index 3c0e315..dfbbcf9 100755 --- a/src/module.c +++ b/src/module.c @@ -110,7 +110,7 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file) } else { - printf("Module '%s' loaded.\n", file); + xlog("Module '%s' loaded.\n", file); } free(error); #else @@ -210,7 +210,7 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file) } else { - printf("Module '%s' loaded.\n", file); + xlog("Module '%s' loaded.\n", file); } free(error); #endif @@ -239,7 +239,7 @@ void unload_module(struct irc_conn *bot, char *where, char *file) } else { - printf("Module '%s' unloaded.\n", file); + xlog("Module '%s' unloaded.\n", file); } while (i < mods->count) diff --git a/xbot.cfg b/xbot.cfg index 7ee3b96..78eddf1 100755 --- a/xbot.cfg +++ b/xbot.cfg @@ -11,6 +11,9 @@ bot: # database file name db = "xbot.db"; + + # log file name + log = "xbot.log"; }; server: diff --git a/xbot.vcxproj b/xbot.vcxproj index 1d847c2..41b44ee 100755 --- a/xbot.vcxproj +++ b/xbot.vcxproj @@ -108,6 +108,7 @@ + @@ -119,6 +120,7 @@ +