From: Wilfried Goesgens Date: Tue, 24 Apr 2012 18:23:31 +0000 (+0200) Subject: Add framework for enabling (debug) logging per module X-Git-Tag: v8.11~70 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=e85aa196b15dae29036b46a28412ce57961c85f9 Add framework for enabling (debug) logging per module - -x / 'all' - export CITADEL_LOGDEBUG= / 'all' --- diff --git a/citadel/serv_extensions.c b/citadel/serv_extensions.c index d2890ebae..45221c514 100644 --- a/citadel/serv_extensions.c +++ b/citadel/serv_extensions.c @@ -51,7 +51,12 @@ * 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) { diff --git a/citadel/serv_extensions.h b/citadel/serv_extensions.h index 47a6070bf..0ef8e1408 100644 --- a/citadel/serv_extensions.h +++ b/citadel/serv_extensions.h @@ -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 */ diff --git a/citadel/server_main.c b/citadel/server_main.c index bab3ab69f..0dc52a0e4 100644 --- a/citadel/server_main.c +++ b/citadel/server_main.c @@ -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. */ diff --git a/citadel/sysdep.c b/citadel/sysdep.c index 422370772..b8d190a8b 100644 --- a/citadel/sysdep.c +++ b/citadel/sysdep.c @@ -972,6 +972,7 @@ void sysdep_master_cleanup(void) { CtdlDestroyServiceHook(); CtdlDestroyRoomHooks(); CtdlDestroySearchHooks(); + CtdlDestroyDebugTable(); #ifdef HAVE_BACKTRACE /// eCrash_Uninit(); #endif