Add framework for enabling (debug) logging per module
authorWilfried Goesgens <dothebart@citadel.org>
Tue, 24 Apr 2012 18:23:31 +0000 (20:23 +0200)
committerWilfried Goesgens <dothebart@citadel.org>
Tue, 24 Apr 2012 18:23:31 +0000 (20:23 +0200)
  - -x <list of coma separated values> / 'all'
  - export CITADEL_LOGDEBUG=<list of coma separated values> / 'all'

citadel/serv_extensions.c
citadel/serv_extensions.h
citadel/server_main.c
citadel/sysdep.c

index d2890ebaeedbd5094f0e06d99bf4bedd4dda892f..45221c514eef4a3ced49c122cadc8e8c49e14c32 100644 (file)
  * Structure defentitions for hook tables
  */
 
-
+typedef struct __LogDebugEntry {
+       CtdlDbgFunction F;
+       const char *Name;
+       long Len;
+} LogDebugEntry;
+HashList *LogDebugEntryTable = NULL;
 
 typedef struct LogFunctionHook LogFunctionHook;
 struct LogFunctionHook {
@@ -61,7 +66,6 @@ struct LogFunctionHook {
 };
 extern LogFunctionHook *LogHookTable;
 
-
 typedef struct FixedOutputHook FixedOutputHook;
 struct FixedOutputHook {
        FixedOutputHook *next;
@@ -328,6 +332,77 @@ long FourHash(const char *key, long length)
 
        return ret;
 }
+/*
+typedef struct __LogDebugEntry {
+       CtdlDbgFunction F;
+       const char *Name;
+       long Len;
+} LogDebugEntry;
+HashList *LogDebugEntryTable = NULL;
+*/
+void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F)
+{
+       LogDebugEntry *E;
+       if (LogDebugEntryTable == NULL)
+               LogDebugEntryTable = NewHash(1, NULL);
+       E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
+       E->F = F;
+       E->Name = Name;
+       E->Len = Len;
+       Put(LogDebugEntryTable, Name, Len, E, NULL);
+       
+}
+void CtdlSetDebugLogFacilities(const char **Str, long n)
+{
+       StrBuf *Token = NULL;
+       StrBuf *Buf = NULL;
+       const char *ch;
+       int i;
+       int DoAll = 0;
+       void *vptr;
+
+       for (i=0; i < n; i++){
+               if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
+                       if (strcmp(Str[i], "all") == 0) {
+                               DoAll = 1;
+                               continue;
+                       }
+                       Buf = NewStrBufPlain(Str[i], -1);
+                       ch = NULL;
+                       if (Token == NULL)
+                               Token = NewStrBufPlain(NULL, StrLength(Buf));
+                       while ((ch != StrBufNOTNULL) &&
+                              StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
+                               if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) && 
+                                   (vptr != NULL))
+                               {
+                                       LogDebugEntry *E = (LogDebugEntry*)vptr;
+                                       E->F();
+                               }
+                       }
+               }
+               FreeStrBuf(&Buf);
+       }
+       FreeStrBuf(&Token);
+       if (DoAll) {
+               long HKLen;
+               const char *ch;
+               HashPos *Pos;
+
+               Pos = GetNewHashPos(LogDebugEntryTable, 0);
+               while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
+                       LogDebugEntry *E = (LogDebugEntry*)vptr;
+                       E->F();
+               }
+
+               DeleteHashPos(&Pos);
+       }
+}
+void CtdlDestroyDebugTable(void)
+{
+
+       DeleteHash(&LogDebugEntryTable);
+}
 
 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
 {
index 47a6070bfa5b1fb563bd55e9c5a41fad0fb0da67..0ef8e14083337390e5c3b697eebe785c6c3565c0 100644 (file)
@@ -15,6 +15,8 @@
 /*
  */
 
+typedef void (*CtdlDbgFunction) (void);
 
 
 /*
@@ -82,4 +84,9 @@ void CtdlDestroySearchHooks(void);
 
 void CtdlDestroyFixedOutputHooks(void);
 int PerformFixedOutputHooks(char *, char *, int);
+
+void CtdlRegisterDebugFlagHook(const char *Name, long len, CtdlDbgFunction F);
+void CtdlSetDebugLogFacilities(const char **Str, long n);
+void CtdlDestroyDebugTable(void);
+
 #endif /* SERV_EXTENSIONS_H */
index bab3ab69f29bc960da866d43e7c5cfd5a1417825..0dc52a0e4667cd6d320be7f7562cfbf4615bd66a 100644 (file)
@@ -100,6 +100,7 @@ int main(int argc, char **argv)
        char relhome[PATH_MAX]="";
        char ctdldir[PATH_MAX]=CTDLDIR;
        int syslog_facility = LOG_DAEMON;
+       const char *eDebuglist[] = {NULL, NULL};
 #ifdef HAVE_RUN_DIR
        struct stat filestats;
 #endif
@@ -136,7 +137,8 @@ int main(int argc, char **argv)
                        home=1;
                        break;
 
-               case 'x':       /* deprecated */
+               case 'x':
+                       eDebuglist [0] = optarg;
                        break;
 
                case 't':       /* deprecated */
@@ -314,6 +316,9 @@ int main(int argc, char **argv)
        
        initialise_modules(0);
 
+       eDebuglist[1] = getenv("CITADEL_LOGDEBUG");
+       CtdlSetDebugLogFacilities(eDebuglist, 2);
+
        /*
         * If we need host auth, start our chkpwd daemon.
         */
index 422370772fddd851f50735d7dd10f53147f7ded7..b8d190a8b254b1e742c8e5989b9db829d3461ea5 100644 (file)
@@ -972,6 +972,7 @@ void sysdep_master_cleanup(void) {
        CtdlDestroyServiceHook();
        CtdlDestroyRoomHooks();
        CtdlDestroySearchHooks();
+       CtdlDestroyDebugTable();
        #ifdef HAVE_BACKTRACE
 ///    eCrash_Uninit();
        #endif