added new multi log system
[NeonServV5.git] / src / main.c
index 23903d93cc26c9ddf4e8804af3887713bd1f244a..d2c482af117619ca01653c4aa38b113f3f628933 100644 (file)
@@ -17,7 +17,6 @@
 
 #define DEFAULT_PID_FILE "neonserv.pid"
 #define DEFAULT_CONF_FILE "neonserv.conf"
-#define DEFAULT_LOG_FILE "neonserv.log"
 
 #include "main.h"
 #include "signal.h"
 #include "ModuleFunctions.h"
 #include "IOHandler.h"
 #include "statistics.h"
+#include "log.h"
 
 struct ProcessState process_state;
 
-static FILE *log_fptr = NULL;
-
 #ifdef HAVE_THREADS
 pthread_mutex_t cache_sync;
 pthread_mutex_t whohandler_sync, whohandler_mass_sync;
@@ -120,7 +118,7 @@ static void main_daemonize() {
         fprintf(stderr, "Unable to fork: %s\n", strerror(errno));
     } else if (pid > 0) {
         printf("Forking into the background (pid: %d)...\n", pid);
-        putlog(LOGLEVEL_INFO, "Forking into the background (pid: %d)...\n", pid);
+        printf_log("main", LOG_INFO, "Forking into the background (pid: %d)...\n", pid);
         exit(0);
     }
     setsid();
@@ -129,7 +127,7 @@ static void main_daemonize() {
     FILE *pidfile = fopen(process_state.pidfile, "w");
     if (pidfile == NULL) {
         fprintf(stderr, "Unable to create PID file: %s\n", strerror(errno));
-        putlog(LOGLEVEL_ERROR, "Unable to create PID file: %s\n", strerror(errno));
+        printf_log("main", LOG_ERROR, "Unable to create PID file: %s\n", strerror(errno));
     } else {
         fprintf(pidfile, "%i\n", (int)getpid());
         fclose(pidfile);
@@ -142,8 +140,11 @@ static void main_daemonize() {
 }
 
 static int reload_configuration() {
-    if(!loadConfig(process_state.config))
+    printf_log("main", LOG_DEBUG, "reloading configuration file: %s", process_state.config);
+    if(!loadConfig(process_state.config)) {
+        printf_log("main", LOG_ERROR, "could not reload configuration file: %s", process_state.config);
         return 1;
+    }
     if(process_state.loaded_config) {
         if(!reload_mysql())
             return 2;
@@ -163,12 +164,13 @@ static int reload_configuration() {
 /* INITIALISATION OF SUBSYSTEMS */
 void initialize_subsystems() {
     init_bind();
+    init_log();
+    printf_log("main", LOG_INFO, "starting up NeonServ %s subsystems...", NEONSERV_VERSION);
     init_lang();
     init_parser();
     init_UserNode();
     init_ChanNode();
     init_ModeNode();
-    init_bind();
        init_modcmd();
     register_module_commands();
     init_handleinfohandler();
@@ -183,6 +185,7 @@ void initialize_subsystems() {
 }
 
 void shutdown_subsystems() {
+    printf_log("main", LOG_INFO, "stopping NeonServ subsystems...");
     free_sockets(1);
     //wait 50ms (run iohandler)
     {
@@ -485,46 +488,6 @@ static TIMEQ_CALLBACK(main_checkauths) {
     timeq_add(next_call, 0, main_checkauths, NULL);
 }
 
-/* LOG BACKEND */
-
-void write_log(int loglevel, const char *line, int len) {
-    SYNCHRONIZE(log_sync);
-    if(!process_state.daemonized && (process_state.loglevel & loglevel)) {
-        printf("%s", line);
-    } else if(!process_state.daemonized && loglevel == LOGLEVEL_ERROR) {
-        fprintf(stderr, "%s", line);
-    }
-    if(get_int_field("log.loglevel") & loglevel) {
-        if(!log_fptr) {
-            log_fptr = fopen(DEFAULT_LOG_FILE, "a");
-            if(!log_fptr) goto write_log_end;
-        }
-        time_t rawtime;
-        struct tm *timeinfo;
-        time(&rawtime);
-        timeinfo = localtime(&rawtime);
-        char timestr[20];
-        int timepos = strftime(timestr, 20, "%x %X ", timeinfo);
-        fwrite(timestr, 1, timepos, log_fptr);
-        fwrite(line, 1, len, log_fptr);
-    }
-    write_log_end:
-    DESYNCHRONIZE(log_sync);
-}
-
-void putlog(int loglevel, const char *text, ...) {
-    va_list arg_list;
-    char logBuf[MAXLOGLEN];
-    int pos;
-    logBuf[0] = '\0';
-    va_start(arg_list, text);
-    pos = vsnprintf(logBuf, MAXLOGLEN - 1, text, arg_list);
-    va_end(arg_list);
-    if (pos < 0 || pos > (MAXLOGLEN - 1)) pos = MAXLOGLEN - 1;
-    logBuf[pos] = '\0';
-    write_log(loglevel, logBuf, pos);
-}
-
 /* INSTALLATION SCRIPT */
 
 static void check_firstrun() {