wlog: change variable naming and fix documentation
[platform/upstream/freerdp.git] / winpr / libwinpr / utils / wlog / wlog.c
index 34dc579..5526e69 100644 (file)
 
 #if defined(ANDROID)
 #include <android/log.h>
+#include "../log.h"
 #endif
 
-#include <winpr/wlog.h>
+#include "wlog.h"
 
-#include "wlog/wlog.h"
 
-#include "../../log.h"
+struct _wLogFilter
+{
+       DWORD Level;
+       LPSTR* Names;
+       DWORD NameCount;
+};
+typedef struct _wLogFilter wLogFilter;
 
 /**
  * References for general logging concepts:
@@ -63,53 +69,68 @@ const char* WLOG_LEVELS[7] =
 static DWORD g_FilterCount = 0;
 static wLogFilter* g_Filters = NULL;
 
-static void log_recursion(const char* file, const char* fkt, int line)
+static BOOL log_recursion(const char* file, const char* fkt, int line)
 {
+       char** msg;
        size_t used, i;
        void* bt = winpr_backtrace(20);
-       char** msg = winpr_backtrace_symbols(bt, &used);
 #if defined(ANDROID)
        const char* tag = WINPR_TAG("utils.wlog");
-       __android_log_print(ANDROID_LOG_FATAL, tag, "Recursion detected!!!");
-       __android_log_print(ANDROID_LOG_FATAL, tag, "Check %s [%s:%d]", fkt, file, line);
+#endif
+
+       if (!bt)
+               return FALSE;
+       msg = winpr_backtrace_symbols(bt, &used);
+       if (!msg)
+               return FALSE;
+#if defined(ANDROID)
+       if (__android_log_print(ANDROID_LOG_FATAL, tag, "Recursion detected!!!") < 0)
+               return FALSE;
+       if (__android_log_print(ANDROID_LOG_FATAL, tag, "Check %s [%s:%d]", fkt, file, line) < 0)
+               return FALSE;
 
        for (i=0; i<used; i++)
-               __android_log_print(ANDROID_LOG_FATAL, tag, "%d: %s", i, msg[i]);
+               if (__android_log_print(ANDROID_LOG_FATAL, tag, "%d: %s", i, msg[i]) < 0)
+                       return FALSE;
 
 #else
-       fprintf(stderr, "[%s]: Recursion detected!\n", fkt);
-       fprintf(stderr, "[%s]: Check %s:%d\n", fkt, file, line);
+       if (fprintf(stderr, "[%s]: Recursion detected!\n", fkt) < 0)
+               return FALSE;
+       if (fprintf(stderr, "[%s]: Check %s:%d\n", fkt, file, line) < 0)
+               return FALSE;
 
        for (i=0; i<used; i++)
-               fprintf(stderr, "%s: %zd: %s\n", fkt, i, msg[i]);
+               if (fprintf(stderr, "%s: %zd: %s\n", fkt, i, msg[i]) < 0)
+                       return FALSE;
 
 #endif
 
-       if (msg)
-               free(msg);
+       free(msg);
 
        winpr_backtrace_free(bt);
+       return TRUE;
 }
 
-int WLog_Write(wLog* log, wLogMessage* message)
+BOOL WLog_Write(wLog* log, wLogMessage* message)
 {
-       int status = -1;
+       BOOL status;
        wLogAppender* appender;
        appender = WLog_GetLogAppender(log);
 
        if (!appender)
-               return -1;
+               return FALSE;
 
-       if (!appender->State)
-               WLog_OpenAppender(log);
+       if (!appender->active)
+               if (!WLog_OpenAppender(log))
+                       return FALSE;
 
        if (!appender->WriteMessage)
-               return -1;
+               return FALSE;
 
        EnterCriticalSection(&appender->lock);
 
        if (appender->recursive)
-               log_recursion(message->FileName, message->FunctionName, message->LineNumber);
+               status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
        else
        {
                appender->recursive = TRUE;
@@ -121,25 +142,26 @@ int WLog_Write(wLog* log, wLogMessage* message)
        return status;
 }
 
-int WLog_WriteData(wLog* log, wLogMessage* message)
+BOOL WLog_WriteData(wLog* log, wLogMessage* message)
 {
-       int status = -1;
+       BOOL status;
        wLogAppender* appender;
        appender = WLog_GetLogAppender(log);
 
        if (!appender)
-               return -1;
+               return FALSE;
 
-       if (!appender->State)
-               WLog_OpenAppender(log);
+       if (!appender->active)
+               if (!WLog_OpenAppender(log))
+                       return FALSE;
 
        if (!appender->WriteDataMessage)
-               return -1;
+               return FALSE;
 
        EnterCriticalSection(&appender->lock);
 
        if (appender->recursive)
-               log_recursion(message->FileName, message->FunctionName, message->LineNumber);
+               status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
        else
        {
                appender->recursive = TRUE;
@@ -151,25 +173,26 @@ int WLog_WriteData(wLog* log, wLogMessage* message)
        return status;
 }
 
-int WLog_WriteImage(wLog* log, wLogMessage* message)
+BOOL WLog_WriteImage(wLog* log, wLogMessage* message)
 {
-       int status = -1;
+       BOOL status;
        wLogAppender* appender;
        appender = WLog_GetLogAppender(log);
 
        if (!appender)
-               return -1;
+               return FALSE;
 
-       if (!appender->State)
-               WLog_OpenAppender(log);
+       if (!appender->active)
+               if (!WLog_OpenAppender(log))
+                       return FALSE;
 
        if (!appender->WriteImageMessage)
-               return -1;
+               return FALSE;
 
        EnterCriticalSection(&appender->lock);
 
        if (appender->recursive)
-               log_recursion(message->FileName, message->FunctionName, message->LineNumber);
+               status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
        else
        {
                appender->recursive = TRUE;
@@ -181,25 +204,26 @@ int WLog_WriteImage(wLog* log, wLogMessage* message)
        return status;
 }
 
-int WLog_WritePacket(wLog* log, wLogMessage* message)
+BOOL WLog_WritePacket(wLog* log, wLogMessage* message)
 {
-       int status = -1;
+       BOOL status;
        wLogAppender* appender;
        appender = WLog_GetLogAppender(log);
 
        if (!appender)
-               return -1;
+               return FALSE;
 
-       if (!appender->State)
-               WLog_OpenAppender(log);
+       if (!appender->active)
+               if (!WLog_OpenAppender(log))
+                       return FALSE;
 
        if (!appender->WritePacketMessage)
-               return -1;
+               return FALSE;
 
        EnterCriticalSection(&appender->lock);
 
        if (appender->recursive)
-               log_recursion(message->FileName, message->FunctionName, message->LineNumber);
+               status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
        else
        {
                appender->recursive = TRUE;
@@ -211,9 +235,9 @@ int WLog_WritePacket(wLog* log, wLogMessage* message)
        return status;
 }
 
-int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
+BOOL WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
 {
-       int status = -1;
+       BOOL status = FALSE;
 
        if (message->Type == WLOG_MESSAGE_TEXT)
        {
@@ -225,7 +249,8 @@ int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
                else
                {
                        char formattedLogMessage[WLOG_MAX_STRING_SIZE];
-                       wvsnprintfx(formattedLogMessage, WLOG_MAX_STRING_SIZE - 1, message->FormatString, args);
+                       if (wvsnprintfx(formattedLogMessage, WLOG_MAX_STRING_SIZE - 1, message->FormatString, args) < 0)
+                               return FALSE;
                        message->TextString = formattedLogMessage;
                        status = WLog_Write(log, message);
                }
@@ -255,13 +280,14 @@ int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
        return status;
 }
 
-void WLog_PrintMessage(wLog* log, wLogMessage* message, ...)
+BOOL WLog_PrintMessage(wLog* log, wLogMessage* message, ...)
 {
-       int status;
+       BOOL status;
        va_list args;
        va_start(args, message);
        status = WLog_PrintMessageVA(log, message, args);
        va_end(args);
+       return status;
 }
 
 DWORD WLog_GetLogLevel(wLog* log)
@@ -276,14 +302,18 @@ DWORD WLog_GetLogLevel(wLog* log)
        }
 }
 
-void WLog_SetLogLevel(wLog* log, DWORD logLevel)
+BOOL WLog_SetLogLevel(wLog* log, DWORD logLevel)
 {
+       if (!log)
+               return FALSE;
+
        if ((logLevel > WLOG_OFF) && (logLevel != WLOG_LEVEL_INHERIT))
        {
                logLevel = WLOG_OFF;
        }
 
        log->Level = logLevel;
+       return TRUE;
 }
 
 int WLog_ParseLogLevel(const char* level)
@@ -311,7 +341,7 @@ int WLog_ParseLogLevel(const char* level)
        return iLevel;
 }
 
-int WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
+BOOL WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
 {
        char* p;
        char* q;
@@ -319,17 +349,32 @@ int WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
        LPSTR names;
        int iLevel;
        count = 1;
+
+       if(!name)
+               return FALSE;
+
        p = (char*) name;
 
-       while ((p = strchr(p, '.')) != NULL)
+       if (p)
        {
-               count++;
-               p++;
+               while ((p = strchr(p, '.')) != NULL)
+               {
+                       count++;
+                       p++;
+               }
        }
 
        names = _strdup(name);
+       if (!names)
+               return FALSE;
        filter->NameCount = count;
-       filter->Names = (LPSTR*) malloc(sizeof(LPSTR) * (count + 1));
+       filter->Names = (LPSTR*) calloc((count + 1UL), sizeof(LPSTR));
+       if(!filter->Names)
+       {
+               free(names);
+               filter->NameCount = 0;
+               return FALSE;
+       }
        filter->Names[count] = NULL;
        count = 0;
        p = (char*) names;
@@ -337,46 +382,62 @@ int WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
        q = strrchr(p, ':');
 
        if (!q)
-               return -1;
+       {
+               free(names);
+               free(filter->Names);
+               filter->Names = NULL;
+               filter->NameCount = 0;
+               return FALSE;
+       }
 
        *q = '\0';
        q++;
        iLevel = WLog_ParseLogLevel(q);
 
        if (iLevel < 0)
-               return -1;
+       {
+               free(names);
+               free(filter->Names);
+               filter->Names = NULL;
+               filter->NameCount = 0;
+               return FALSE;
+       }
 
        filter->Level = (DWORD) iLevel;
 
        while ((p = strchr(p, '.')) != NULL)
        {
-               filter->Names[count++] = p + 1;
+               if (count < filter->NameCount)
+                       filter->Names[count++] = p + 1;
                *p = '\0';
                p++;
        }
 
-       return 0;
+       return TRUE;
 }
 
-int WLog_ParseFilters()
+BOOL WLog_ParseFilters()
 {
        char* p;
        char* env;
        DWORD count;
        DWORD nSize;
        int status;
-       char** strs;
+       LPCSTR* strs;
+
        nSize = GetEnvironmentVariableA("WLOG_FILTER", NULL, 0);
 
        if (nSize < 1)
-               return 0;
+               return TRUE;
 
        env = (LPSTR) malloc(nSize);
 
        if (!env)
-               return -1;
+               return FALSE;
+
+       if (!GetEnvironmentVariableA("WLOG_FILTER", env, nSize))
+               return FALSE;
 
-       nSize = GetEnvironmentVariableA("WLOG_FILTER", env, nSize);
        count = 1;
        p = env;
 
@@ -388,13 +449,22 @@ int WLog_ParseFilters()
 
        g_FilterCount = count;
        p = env;
+
        count = 0;
-       strs = (char**) calloc(g_FilterCount, sizeof(char*));
+       strs = (LPCSTR*) calloc(g_FilterCount, sizeof(LPCSTR));
+
+       if (!strs)
+       {
+               free(env);
+               return FALSE;
+       }
+
        strs[count++] = p;
 
        while ((p = strchr(p, ',')) != NULL)
        {
-               strs[count++] = p + 1;
+               if (count < g_FilterCount)
+                       strs[count++] = p + 1;
                *p = '\0';
                p++;
        }
@@ -402,18 +472,28 @@ int WLog_ParseFilters()
        g_Filters = calloc(g_FilterCount, sizeof(wLogFilter));
 
        if (!g_Filters)
-               return -1;
+       {
+               free(strs);
+               free(env);
+               return FALSE;
+       }
 
        for (count = 0; count < g_FilterCount; count++)
        {
                status = WLog_ParseFilter(&g_Filters[count], strs[count]);
 
                if (status < 0)
-                       return -1;
+               {
+                       free(strs);
+                       free(env);
+                       return FALSE;
+               }
        }
 
        free(strs);
-       return 0;
+       free(env);
+
+       return TRUE;
 }
 
 int WLog_GetFilterLogLevel(wLog* log)
@@ -457,7 +537,7 @@ int WLog_GetFilterLogLevel(wLog* log)
        return iLevel;
 }
 
-int WLog_ParseName(wLog* log, LPCSTR name)
+BOOL WLog_ParseName(wLog* log, LPCSTR name)
 {
        char* p;
        int count;
@@ -472,8 +552,15 @@ int WLog_ParseName(wLog* log, LPCSTR name)
        }
 
        names = _strdup(name);
+       if (!names)
+               return FALSE;
        log->NameCount = count;
-       log->Names = (LPSTR*) malloc(sizeof(LPSTR) * (count + 1));
+       log->Names = (LPSTR*) calloc((count + 1UL), sizeof(LPSTR));
+       if(!log->Names)
+       {
+               free(names);
+               return FALSE;
+       }
        log->Names[count] = NULL;
        count = 0;
        p = (char*) names;
@@ -481,69 +568,85 @@ int WLog_ParseName(wLog* log, LPCSTR name)
 
        while ((p = strchr(p, '.')) != NULL)
        {
-               log->Names[count++] = p + 1;
+               if (count < log->NameCount)
+                       log->Names[count++] = p + 1;
                *p = '\0';
                p++;
        }
 
-       return 0;
+       return TRUE;
 }
 
 wLog* WLog_New(LPCSTR name, wLog* rootLogger)
 {
-       wLog* log;
-       char* env;
+       wLog* log = NULL;
+       char* env = NULL;
        DWORD nSize;
        int iLevel;
+
        log = (wLog*) calloc(1, sizeof(wLog));
+       if (!log)
+               return NULL;
 
-       if (log)
-       {
-               log->Name = _strdup(name);
+    log->Name = _strdup(name);
 
-               if (!log->Name)
-                       return NULL;
+    if (!log->Name)
+               goto out_fail;
 
-               WLog_ParseName(log, name);
-               log->Parent = rootLogger;
-               log->ChildrenCount = 0;
-               log->ChildrenSize = 16;
-               log->Children = (wLog**) calloc(log->ChildrenSize, sizeof(wLog*));
+    if (!WLog_ParseName(log, name))
+               goto out_fail;
 
-               if (!log->Children)
-                       return NULL;
+    log->Parent = rootLogger;
+    log->ChildrenCount = 0;
+    log->ChildrenSize = 16;
 
-               log->Appender = NULL;
+    if (!(log->Children = (wLog**) calloc(log->ChildrenSize, sizeof(wLog*))))
+               goto out_fail;
 
-               if (rootLogger)
-               {
-                       log->Level = WLOG_LEVEL_INHERIT;
-               }
-               else
-               {
-                       log->Level = WLOG_INFO;
-                       nSize = GetEnvironmentVariableA("WLOG_LEVEL", NULL, 0);
+    log->Appender = NULL;
 
-                       if (nSize)
-                       {
-                               env = (LPSTR) malloc(nSize);
-                               nSize = GetEnvironmentVariableA("WLOG_LEVEL", env, nSize);
-                               iLevel = WLog_ParseLogLevel(env);
+    if (rootLogger)
+    {
+        log->Level = WLOG_LEVEL_INHERIT;
+    }
+    else
+    {
+        log->Level = WLOG_INFO;
+        nSize = GetEnvironmentVariableA("WLOG_LEVEL", NULL, 0);
 
-                               if (iLevel >= 0)
-                                       log->Level = (DWORD) iLevel;
+        if (nSize)
+        {
+            env = (LPSTR) malloc(nSize);
+                       if (!env)
+                               goto out_fail;
 
+                       if (!GetEnvironmentVariableA("WLOG_LEVEL", env, nSize))
+                       {
+                               fprintf(stderr, "WLOG_LEVEL environment variable changed in my back !\n");
                                free(env);
+                               goto out_fail;
                        }
-               }
 
-               iLevel = WLog_GetFilterLogLevel(log);
+                       iLevel = WLog_ParseLogLevel(env);
+                       free(env);
 
-               if (iLevel >= 0)
-                       log->Level = (DWORD) iLevel;
-       }
+                       if (iLevel >= 0)
+                               log->Level = (DWORD) iLevel;
+        }
+    }
+
+    iLevel = WLog_GetFilterLogLevel(log);
+
+    if (iLevel >= 0)
+        log->Level = (DWORD) iLevel;
 
        return log;
+
+out_fail:
+       free (log->Children);
+       free (log->Name);
+       free (log);
+       return NULL;
 }
 
 void WLog_Free(wLog* log)
@@ -574,7 +677,9 @@ wLog* WLog_GetRoot()
 
        if (!g_RootLog)
        {
-               g_RootLog = WLog_New("", NULL);
+               if (!(g_RootLog = WLog_New("", NULL)))
+                       return NULL;
+
                g_RootLog->IsRoot = TRUE;
                WLog_ParseFilters();
                logAppenderType = WLOG_APPENDER_CONSOLE;
@@ -583,38 +688,81 @@ wLog* WLog_GetRoot()
                if (nSize)
                {
                        env = (LPSTR) malloc(nSize);
-                       nSize = GetEnvironmentVariableA("WLOG_APPENDER", env, nSize);
+                       if (!env)
+                               goto fail;
 
-                       if (env)
+                       if (!GetEnvironmentVariableA("WLOG_APPENDER", env, nSize))
                        {
-                               if (_stricmp(env, "CONSOLE") == 0)
-                                       logAppenderType = WLOG_APPENDER_CONSOLE;
-                               else if (_stricmp(env, "FILE") == 0)
-                                       logAppenderType = WLOG_APPENDER_FILE;
-                               else if (_stricmp(env, "BINARY") == 0)
-                                       logAppenderType = WLOG_APPENDER_BINARY;
-
+                               fprintf(stderr, "WLOG_APPENDER environment variable modified in my back");
                                free(env);
+                               goto fail;
                        }
+
+                       if (_stricmp(env, "CONSOLE") == 0)
+                               logAppenderType = WLOG_APPENDER_CONSOLE;
+                       else if (_stricmp(env, "FILE") == 0)
+                               logAppenderType = WLOG_APPENDER_FILE;
+                       else if (_stricmp(env, "BINARY") == 0)
+                               logAppenderType = WLOG_APPENDER_BINARY;
+#ifdef HAVE_SYSLOG_H
+                       else if (_stricmp(env, "SYSLOG") == 0)
+                               logAppenderType = WLOG_APPENDER_SYSLOG;
+#endif /* HAVE_SYSLOG_H */
+#ifdef HAVE_JOURNALD_H
+                       else if (_stricmp(env, "JOURNALD") == 0)
+                               logAppenderType = WLOG_APPENDER_JOURNALD;
+#endif
+                       else if (_stricmp(env, "UDP") == 0)
+                               logAppenderType = WLOG_APPENDER_UDP;
+
+                       free(env);
                }
 
-               WLog_SetLogAppenderType(g_RootLog, logAppenderType);
+               if (!WLog_SetLogAppenderType(g_RootLog, logAppenderType))
+                       goto fail;
        }
 
        return g_RootLog;
+
+fail:
+       free(g_RootLog);
+       g_RootLog = NULL;
+       return NULL;
 }
 
-int WLog_AddChild(wLog* parent, wLog* child)
+BOOL WLog_AddChild(wLog* parent, wLog* child)
 {
        if (parent->ChildrenCount >= parent->ChildrenSize)
        {
+               wLog **tmp;
                parent->ChildrenSize *= 2;
-               parent->Children = (wLog**) realloc(parent->Children, sizeof(wLog*) * parent->ChildrenSize);
+               if (!parent->ChildrenSize)
+               {
+                       if (parent->Children)
+                               free (parent->Children);
+                       parent->Children = NULL;
+                       
+               }
+               else
+               {
+                       tmp = (wLog**) realloc(parent->Children, sizeof(wLog*) * parent->ChildrenSize);
+                       if (!tmp)
+                       {
+                               if (parent->Children)
+                                       free (parent->Children);
+                               parent->Children = NULL;
+                               return FALSE;
+                       }
+                       parent->Children = tmp;
+               }
        }
 
+       if (!parent->Children)
+               return FALSE;
+
        parent->Children[parent->ChildrenCount++] = child;
        child->Parent = parent;
-       return 0;
+       return TRUE;
 }
 
 wLog* WLog_FindChild(LPCSTR name)
@@ -625,6 +773,9 @@ wLog* WLog_FindChild(LPCSTR name)
        BOOL found = FALSE;
        root = WLog_GetRoot();
 
+       if (!root)
+               return NULL;
+
        for (index = 0; index < root->ChildrenCount; index++)
        {
                child = root->Children[index];
@@ -642,29 +793,35 @@ wLog* WLog_FindChild(LPCSTR name)
 wLog* WLog_Get(LPCSTR name)
 {
        wLog* log;
-       wLog* root;
-       root = WLog_GetRoot();
-       log = WLog_FindChild(name);
-
-       if (!log)
+       if (!(log = WLog_FindChild(name)))
        {
-               log = WLog_New(name,root);
-               WLog_AddChild(root, log);
+               wLog* root = WLog_GetRoot();
+               if (!root)
+                       return NULL;
+               if (!(log = WLog_New(name, root)))
+                       return NULL;
+               if (!WLog_AddChild(root, log))
+               {
+                       WLog_Free(log);
+                       return NULL;
+               }
        }
-
        return log;
 }
 
-void WLog_Init()
+BOOL WLog_Init()
 {
-       WLog_GetRoot();
+       return WLog_GetRoot() != NULL;
 }
 
-void WLog_Uninit()
+BOOL WLog_Uninit()
 {
-       wLog* root = WLog_GetRoot();
        DWORD index;
        wLog* child = NULL;
+       wLog* root = g_RootLog;
+
+       if (!root)
+               return FALSE;
 
        for (index = 0; index < root->ChildrenCount; index++)
        {
@@ -674,4 +831,5 @@ void WLog_Uninit()
 
        WLog_Free(root);
        g_RootLog = NULL;
+       return TRUE;
 }