added logger.c

This commit is contained in:
Aaron Blakely 2024-03-09 02:40:06 -06:00
parent c12e101f58
commit 5ec7b93a7a
15 changed files with 108 additions and 20 deletions

2
.gitignore vendored
View File

@ -6,7 +6,7 @@ Release
mods/test mods/test
.cache .cache
UpgradeLog*.XML UpgradeLog*.XML
xbot.log
xbot.db xbot.db
*.so *.so
*.dll *.dll

View File

@ -23,6 +23,7 @@ main:
$(CC) $(CFLAGS) $(SRC)/channel.c -o $(OBJ)/channel.o $(CC) $(CFLAGS) $(SRC)/channel.c -o $(OBJ)/channel.o
$(CC) $(CFLAGS) $(SRC)/timers.c -o $(OBJ)/timers.o $(CC) $(CFLAGS) $(SRC)/timers.c -o $(OBJ)/timers.o
$(CC) $(CFLAGS) $(SRC)/db.c -o $(OBJ)/db.o $(CC) $(CFLAGS) $(SRC)/db.c -o $(OBJ)/db.o
$(CC) $(CFLAGS) $(SRC)/logger.c -o $(OBJ)/logger.o
$(CC) -o $(EXEC) $(OBJECTS) $(BINFLAGS) $(CC) -o $(EXEC) $(OBJECTS) $(BINFLAGS)
@echo "All Done!" @echo "All Done!"

View File

@ -36,6 +36,7 @@ struct irc_conn
char real_name[512]; char real_name[512];
char db_file[256]; char db_file[256];
char log_file[256];
struct db_table *db; struct db_table *db;
// I/O Buffers // I/O Buffers

19
lib/logger.h Executable file
View File

@ -0,0 +1,19 @@
#ifndef LOGGER_H
#define LOGGER_H
#include <stdio.h>
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

View File

@ -7,6 +7,8 @@
#ifndef UTIL_H #ifndef UTIL_H
#define UTIL_H #define UTIL_H
#include "logger.h"
#ifdef _WIN32 #ifdef _WIN32
#define true TRUE #define true TRUE
#define false FALSE #define false FALSE

View File

@ -16,7 +16,7 @@ void add_channel(char *name)
if (channel_exists(name) == 1) if (channel_exists(name) == 1)
return; return;
printf("Adding channel %s\n", name); xlog("Adding channel %s\n", name);
channels[chan_count] = calloc(1, sizeof(struct channel)); channels[chan_count] = calloc(1, sizeof(struct channel));
strlcpy(channels[chan_count]->name, name, 32); strlcpy(channels[chan_count]->name, name, 32);
@ -35,7 +35,7 @@ void remove_channel(char *name)
{ {
if (!strcmp(channels[i]->name, 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]->users);
free(channels[i]); free(channels[i]);
@ -122,7 +122,7 @@ void add_user_to_channel(char *user, char *host, char *chan)
if (user_exists(chan, user) == 1) if (user_exists(chan, user) == 1)
return; 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++) 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)) 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++) 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)) 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++) for (j = j; j < channels[i]->user_count; j++)
{ {

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <libconfig.h> #include <libconfig.h>
#include "irc.h" #include "irc.h"
#include "util.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)) 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_line(cf),
config_error_text(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)) if (config_lookup_string(cf, "bot.db", &base))
strlcpy(bot.db_file, base, sizeof bot.db_file); 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); config_destroy(cf);
return bot; return bot;
@ -63,7 +67,7 @@ void run_autoload(struct irc_conn *bot)
if (!config_read_file(cf, "xbot.cfg")) 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_line(cf),
config_error_text(cf) config_error_text(cf)
); );

View File

@ -27,7 +27,7 @@ int db_write(struct db_table *db, char *fname)
fullpath = realpath(fname, NULL); fullpath = realpath(fname, NULL);
#endif #endif
printf("Writing db to file: %s\n", fullpath); xlog("Writing db to file: %s\n", fullpath);
// write the header // write the header
fwrite(db, sizeof(struct db_table), 1, fp); fwrite(db, sizeof(struct db_table), 1, fp);
@ -65,7 +65,7 @@ struct db_table *db_read(char *fname)
// check the magic value // check the magic value
if (db->db_magic != DB_MAGIC) 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; return NULL;
} }
@ -74,7 +74,7 @@ struct db_table *db_read(char *fname)
// check the version // check the version
if (db->db_ver != DB_VER) 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; return NULL;
} }

View File

@ -43,7 +43,7 @@ void init_events()
MY_API int add_handler(char *type, void *handler) MY_API int add_handler(char *type, void *handler)
{ {
int i; 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++) for (i = 0; i < handlers_count; i++)
{ {
@ -62,7 +62,7 @@ MY_API int add_handler(char *type, void *handler)
} }
else else
{ {
printf("Handler array is full, cannot add more handlers.\n"); xlog("Handler array is full, cannot add more handlers.\n");
return -1; return -1;
} }
} }

View File

@ -126,7 +126,7 @@ void irc_connect(struct irc_conn *bot)
eprint("Error: Cannot connect to host '%s'\n", bot->host); eprint("Error: Cannot connect to host '%s'\n", bot->host);
} }
printf("Connected!\n"); xlog("Connected!\n");
bot->srv_fd = FDOPEN(srv_fd, "r+"); bot->srv_fd = FDOPEN(srv_fd, "r+");
#endif #endif
} }

54
src/logger.c Executable file
View File

@ -0,0 +1,54 @@
#include "logger.h"
#include "util.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <stdlib.h>
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);
}

View File

@ -100,6 +100,8 @@ int main(int argc, char **argv)
// Read the config // Read the config
bot = read_config(bot, conf); bot = read_config(bot, conf);
log_init(bot.log_file);
// check if the db exists, if not, create it // check if the db exists, if not, create it
#ifdef _WIN32 #ifdef _WIN32
if (access(bot.db_file, 0) == -1) 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) if (access(bot.db_file, F_OK) == -1)
#endif #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)); bot.db = (struct db_table *)malloc(sizeof(struct db_table));
memset(bot.db, 0, sizeof(struct db_table)); memset(bot.db, 0, sizeof(struct db_table));
set_bot_db(bot.db); set_bot_db(bot.db);
@ -119,7 +121,7 @@ int main(int argc, char **argv)
} }
else 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); bot.db = db_read(bot.db_file);
set_bot_db(bot.db); set_bot_db(bot.db);
} }
@ -128,7 +130,7 @@ int main(int argc, char **argv)
run_autoload(&bot); run_autoload(&bot);
// Connect to the server // Connect to the server
printf("Connecting to %s...\n", bot.host); xlog("Connecting to %s...\n", bot.host);
irc_connect(&bot); irc_connect(&bot);
trespond = time(NULL); trespond = time(NULL);

View File

@ -110,7 +110,7 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file)
} }
else else
{ {
printf("Module '%s' loaded.\n", file); xlog("Module '%s' loaded.\n", file);
} }
free(error); free(error);
#else #else
@ -210,7 +210,7 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file)
} }
else else
{ {
printf("Module '%s' loaded.\n", file); xlog("Module '%s' loaded.\n", file);
} }
free(error); free(error);
#endif #endif
@ -239,7 +239,7 @@ void unload_module(struct irc_conn *bot, char *where, char *file)
} }
else else
{ {
printf("Module '%s' unloaded.\n", file); xlog("Module '%s' unloaded.\n", file);
} }
while (i < mods->count) while (i < mods->count)

View File

@ -11,6 +11,9 @@ bot:
# database file name # database file name
db = "xbot.db"; db = "xbot.db";
# log file name
log = "xbot.log";
}; };
server: server:

View File

@ -108,6 +108,7 @@
<ClInclude Include="lib\db.h" /> <ClInclude Include="lib\db.h" />
<ClInclude Include="lib\events.h" /> <ClInclude Include="lib\events.h" />
<ClInclude Include="lib\irc.h" /> <ClInclude Include="lib\irc.h" />
<ClInclude Include="lib\logger.h" />
<ClInclude Include="lib\module.h" /> <ClInclude Include="lib\module.h" />
<ClInclude Include="lib\timers.h" /> <ClInclude Include="lib\timers.h" />
<ClInclude Include="lib\util.h" /> <ClInclude Include="lib\util.h" />
@ -119,6 +120,7 @@
<ClCompile Include="src\db.c" /> <ClCompile Include="src\db.c" />
<ClCompile Include="src\events.c" /> <ClCompile Include="src\events.c" />
<ClCompile Include="src\irc.c" /> <ClCompile Include="src\irc.c" />
<ClCompile Include="src\logger.c" />
<ClCompile Include="src\main.c" /> <ClCompile Include="src\main.c" />
<ClCompile Include="src\module.c" /> <ClCompile Include="src\module.c" />
<ClCompile Include="src\timers.c" /> <ClCompile Include="src\timers.c" />