Merge branch 'development'
[NeonServV5.git] / src / main.c
index d17647d72f227ee3c5a4d742ca2e739d0e1b06f1..fb16300da97ac5697e13adfcc59b85811a8468d3 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;
-static pthread_mutex_t log_sync;
 static pthread_t *current_threads = NULL;
 #endif
 
-static void main_tread(void *empty);
+static void *main_tread(void *empty);
 static TIMEQ_CALLBACK(clear_cache);
 static TIMEQ_CALLBACK(main_checkauths);
 static void check_firstrun();
@@ -120,7 +117,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 +126,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 +139,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 +163,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 +184,7 @@ void initialize_subsystems() {
 }
 
 void shutdown_subsystems() {
+    printf_log("main", LOG_INFO, "stopping NeonServ subsystems...");
     free_sockets(1);
     //wait 50ms (run iohandler)
     {
@@ -241,10 +243,10 @@ int getRunningThreads() {
 }
 
 static void main_start_threads() {
-    int tid_id = 0;
     int worker_threads = get_int_field("General.worker_threads");
     if(!worker_threads) worker_threads = 1;
     #ifdef HAVE_THREADS
+    int tid_id = 0;
     {
         current_threads = calloc(worker_threads, sizeof(*current_threads));
         for(tid_id = 0; tid_id < worker_threads; tid_id++) {
@@ -266,10 +268,11 @@ static void main_start_threads() {
 
 /* MAIN FUNCTION(S) */
 
-static void main_tread(void *empty) {
+static void *main_tread(void *empty) {
     while(process_state.running) {
         iohandler_poll();
     }
+    return NULL;
 }
 
 static void main_restart_process() {
@@ -302,6 +305,11 @@ int main(int argc, char *argv[]) {
     initMemoryDebug();
     #endif
     
+    //initialize mutex debugger BEFORE using any mutexes
+    #ifdef ENABLE_MUTEX_DEBUG
+    initMutexDebug();
+    #endif
+    
     //deny root startup
     #ifndef WIN32
     if(geteuid() == 0 || getuid() == 0) {
@@ -312,7 +320,7 @@ int main(int argc, char *argv[]) {
     
     //load configuration
     int errid;
-    if(errid = reload_configuration()) {
+    if((errid = reload_configuration())) {
         fprintf(stderr, "Unable to load configuration file `%s`. (errid: %d)\n", process_state.config, errid);
         exit(0);
     }
@@ -332,7 +340,6 @@ int main(int argc, char *argv[]) {
     free(modulelist);
     
     #if HAVE_THREADS
-    THREAD_MUTEX_INIT(log_sync);
     THREAD_MUTEX_INIT(cache_sync);
     THREAD_MUTEX_INIT(whohandler_sync);
     THREAD_MUTEX_INIT(whohandler_mass_sync);
@@ -484,46 +491,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() {