123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137 |
- /**
- * WinPR: Windows Portable Runtime
- * WinPR Logger
- *
- * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <stdio.h>
- #include <string.h>
- #include <winpr/crt.h>
- #include <winpr/print.h>
- #include <winpr/debug.h>
- #include <winpr/environment.h>
- #include <winpr/wlog.h>
- #if defined(ANDROID)
- #include <android/log.h>
- #include "../log.h"
- #endif
- #include "wlog.h"
- struct _wLogFilter
- {
- DWORD Level;
- LPSTR* Names;
- DWORD NameCount;
- };
- typedef struct _wLogFilter wLogFilter;
- #define WLOG_FILTER_NOT_FILTERED -1
- #define WLOG_FILTER_NOT_INITIALIZED -2
- /**
- * References for general logging concepts:
- *
- * Short introduction to log4j:
- * http://logging.apache.org/log4j/1.2/manual.html
- *
- * logging - Logging facility for Python:
- * http://docs.python.org/2/library/logging.html
- */
- LPCSTR WLOG_LEVELS[7] = { "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF" };
- static INIT_ONCE _WLogInitialized = INIT_ONCE_STATIC_INIT;
- static DWORD g_FilterCount = 0;
- static wLogFilter* g_Filters = NULL;
- static wLog* g_RootLog = NULL;
- static wLog* WLog_New(LPCSTR name, wLog* rootLogger);
- static void WLog_Free(wLog* log);
- static LONG WLog_GetFilterLogLevel(wLog* log);
- static int WLog_ParseLogLevel(LPCSTR level);
- static BOOL WLog_ParseFilter(wLogFilter* filter, LPCSTR name);
- static BOOL WLog_ParseFilters(void);
- static char g_logRoot[256] = "";
- #if !defined(_WIN32)
- static void WLog_Uninit_(void) __attribute__((destructor));
- #endif
- #ifdef _WIN32
- #include <windows.h>
- #include <tchar.h>
- char* get_process_name() {
- // 拼接前缀、进程名和后缀
- char date_str[MAX_PATH];
- char* full_path = malloc(MAX_PATH);
-
- time_t now = time(NULL);
- struct tm* local_time = localtime(&now);
- memset(full_path, 0, MAX_PATH);
- snprintf(date_str, sizeof(date_str), "%04d%02d%02d", local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday);
- if (strlen(g_logRoot) == 0)
- return NULL;
- snprintf(full_path, MAX_PATH, "%s\\wlog_%s.log", g_logRoot, date_str);
- //MessageBox(NULL, full_path, NULL, 0);
- // 检查文件是否存在
- FILE* fp = fopen(full_path, "r");
- if (!fp) {
- // 创建文件
- fp = fopen(full_path, "w");
- if (!fp) {
- printf("Failed to create file: %s\n", full_path);
- return 1;
- }
- printf("File created: %s\n", full_path);
- fclose(fp);
- }
- else {
- fclose(fp);
- printf("File already exists: %s\n", full_path);
- }
-
- return full_path;
- }
- char* getLogPath()
- {
- static char* dst = NULL;
- if (dst == NULL)
- dst = get_process_name();
- return dst;
- }
- void WLog_WriteLogFile(int type, const char* fmt, ...)
- {
- char time_str[20] = "";
- FILETIME ft;
- SYSTEMTIME st;
- GetSystemTimePreciseAsFileTime(&ft);
- FileTimeToSystemTime(&ft, &st);
- // 将 UTC 时间转换为本地时间
- SYSTEMTIME local_st;
- FileTimeToLocalFileTime(&ft, &ft);
- FileTimeToSystemTime(&ft, &local_st);
- int hour = local_st.wHour;
- int min = local_st.wMinute;
- int sec = local_st.wSecond;
- int msec = local_st.wMilliseconds;
- snprintf(time_str, sizeof(time_str), "%02d:%02d:%02d.%03d", hour, min, sec, msec);
- const char* _type[] = { "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" };
- const char* path = getLogPath();
- if (path == NULL)
- return;
- static FILE* pFile = NULL;
- if (pFile == NULL)
- {
- pFile = fopen(path, "a");
- if (!pFile) {
- pFile = NULL;
- // 错误处理
- return;
- }
- }
-
- if (pFile)
- {
- //MessageBox(NULL, path, NULL, 0);
- fprintf(pFile, "[%s] [%s] ", time_str, _type[type]);
- va_list args;
- va_start(args, fmt);
- vfprintf(pFile, fmt, args);
- va_end(args);
- fprintf(pFile, "\n");
- }
-
- }
- #endif
- static void WLog_Uninit_(void)
- {
- DWORD index;
- wLog* child = NULL;
- wLog* root = g_RootLog;
- if (!root)
- return;
- for (index = 0; index < root->ChildrenCount; index++)
- {
- child = root->Children[index];
- WLog_Free(child);
- }
- WLog_Free(root);
- g_RootLog = NULL;
- }
- static BOOL CALLBACK WLog_InitializeRoot(PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context)
- {
- char* env;
- DWORD nSize;
- DWORD logAppenderType;
- LPCSTR appender = "WLOG_APPENDER";
- if (!(g_RootLog = WLog_New("", NULL)))
- return FALSE;
- g_RootLog->IsRoot = TRUE;
- WLog_ParseFilters();
- logAppenderType = WLOG_APPENDER_CONSOLE;
- nSize = GetEnvironmentVariableA(appender, NULL, 0);
- if (nSize)
- {
- env = (LPSTR)malloc(nSize);
- if (!env)
- goto fail;
- if (GetEnvironmentVariableA(appender, env, nSize) != nSize - 1)
- {
- fprintf(stderr, "%s environment variable modified in my back", appender);
- 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);
- }
- if (!WLog_SetLogAppenderType(g_RootLog, logAppenderType))
- goto fail;
- #if defined(_WIN32)
- atexit(WLog_Uninit_);
- #endif
- return TRUE;
- fail:
- free(g_RootLog);
- g_RootLog = NULL;
- return FALSE;
- }
- static BOOL log_recursion(LPCSTR file, LPCSTR fkt, int line)
- {
- BOOL status = FALSE;
- char** msg = NULL;
- size_t used, i;
- void* bt = winpr_backtrace(20);
- #if defined(ANDROID)
- LPCSTR tag = WINPR_TAG("utils.wlog");
- #endif
- if (!bt)
- return FALSE;
- msg = winpr_backtrace_symbols(bt, &used);
- if (!msg)
- goto out;
- #if defined(ANDROID)
- if (__android_log_print(ANDROID_LOG_FATAL, tag, "Recursion detected!!!") < 0)
- goto out;
- if (__android_log_print(ANDROID_LOG_FATAL, tag, "Check %s [%s:%d]", fkt, file, line) < 0)
- goto out;
- for (i = 0; i < used; i++)
- if (__android_log_print(ANDROID_LOG_FATAL, tag, "%zd: %s", i, msg[i]) < 0)
- goto out;
- #else
- if (fprintf(stderr, "[%s]: Recursion detected!\n", fkt) < 0)
- goto out;
- if (fprintf(stderr, "[%s]: Check %s:%d\n", fkt, file, line) < 0)
- goto out;
- for (i = 0; i < used; i++)
- if (fprintf(stderr, "%s: %" PRIuz ": %s\n", fkt, i, msg[i]) < 0)
- goto out;
- #endif
- status = TRUE;
- out:
- free(msg);
- winpr_backtrace_free(bt);
- return status;
- }
- static BOOL WLog_Write(wLog* log, wLogMessage* message)
- {
- BOOL status;
- wLogAppender* appender;
- appender = WLog_GetLogAppender(log);
- if (!appender)
- return FALSE;
- if (!appender->active)
- if (!WLog_OpenAppender(log))
- return FALSE;
- if (!appender->WriteMessage)
- return FALSE;
- EnterCriticalSection(&appender->lock);
- if (appender->recursive)
- status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
- else
- {
- appender->recursive = TRUE;
- status = appender->WriteMessage(log, appender, message);
- appender->recursive = FALSE;
- }
- LeaveCriticalSection(&appender->lock);
- return status;
- }
- static BOOL WLog_WriteData(wLog* log, wLogMessage* message)
- {
- BOOL status;
- wLogAppender* appender;
- appender = WLog_GetLogAppender(log);
- if (!appender)
- return FALSE;
- if (!appender->active)
- if (!WLog_OpenAppender(log))
- return FALSE;
- if (!appender->WriteDataMessage)
- return FALSE;
- EnterCriticalSection(&appender->lock);
- if (appender->recursive)
- status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
- else
- {
- appender->recursive = TRUE;
- status = appender->WriteDataMessage(log, appender, message);
- appender->recursive = FALSE;
- }
- LeaveCriticalSection(&appender->lock);
- return status;
- }
- static BOOL WLog_WriteImage(wLog* log, wLogMessage* message)
- {
- BOOL status;
- wLogAppender* appender;
- appender = WLog_GetLogAppender(log);
- if (!appender)
- return FALSE;
- if (!appender->active)
- if (!WLog_OpenAppender(log))
- return FALSE;
- if (!appender->WriteImageMessage)
- return FALSE;
- EnterCriticalSection(&appender->lock);
- if (appender->recursive)
- status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
- else
- {
- appender->recursive = TRUE;
- status = appender->WriteImageMessage(log, appender, message);
- appender->recursive = FALSE;
- }
- LeaveCriticalSection(&appender->lock);
- return status;
- }
- static BOOL WLog_WritePacket(wLog* log, wLogMessage* message)
- {
- BOOL status;
- wLogAppender* appender;
- appender = WLog_GetLogAppender(log);
- if (!appender)
- return FALSE;
- if (!appender->active)
- if (!WLog_OpenAppender(log))
- return FALSE;
- if (!appender->WritePacketMessage)
- return FALSE;
- EnterCriticalSection(&appender->lock);
- if (appender->recursive)
- status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
- else
- {
- appender->recursive = TRUE;
- status = appender->WritePacketMessage(log, appender, message);
- appender->recursive = FALSE;
- }
- LeaveCriticalSection(&appender->lock);
- return status;
- }
- BOOL WLog_PrintMessageVA(wLog* log, DWORD type, DWORD level, DWORD line, const char* file,
- const char* function, va_list args)
- {
- BOOL status = FALSE;
- wLogMessage message = { 0 };
- message.Type = type;
- message.Level = level;
- message.LineNumber = line;
- message.FileName = file;
- message.FunctionName = function;
- switch (type)
- {
- case WLOG_MESSAGE_TEXT:
- message.FormatString = va_arg(args, const char*);
- if (!strchr(message.FormatString, '%'))
- {
- message.TextString = (LPSTR)message.FormatString;
- status = WLog_Write(log, &message);
- }
- else
- {
- char formattedLogMessage[WLOG_MAX_STRING_SIZE];
- if (wvsnprintfx(formattedLogMessage, WLOG_MAX_STRING_SIZE - 1, message.FormatString,
- args) < 0)
- return FALSE;
- message.TextString = formattedLogMessage;
- status = WLog_Write(log, &message);
- }
- break;
- case WLOG_MESSAGE_DATA:
- message.Data = va_arg(args, void*);
- message.Length = va_arg(args, int);
- status = WLog_WriteData(log, &message);
- break;
- case WLOG_MESSAGE_IMAGE:
- message.ImageData = va_arg(args, void*);
- message.ImageWidth = va_arg(args, int);
- message.ImageHeight = va_arg(args, int);
- message.ImageBpp = va_arg(args, int);
- status = WLog_WriteImage(log, &message);
- break;
- case WLOG_MESSAGE_PACKET:
- message.PacketData = va_arg(args, void*);
- message.PacketLength = va_arg(args, int);
- message.PacketFlags = va_arg(args, int);
- status = WLog_WritePacket(log, &message);
- break;
- default:
- break;
- }
- return status;
- }
- BOOL WLog_PrintMessage(wLog* log, DWORD type, DWORD level, DWORD line, const char* file,
- const char* function, ...)
- {
- BOOL status;
- va_list args;
- va_start(args, function);
- status = WLog_PrintMessageVA(log, type, level, line, file, function, args);
- va_end(args);
- return status;
- }
- DWORD WLog_GetLogLevel(wLog* log)
- {
- if (!log)
- return WLOG_OFF;
- if (log->FilterLevel <= WLOG_FILTER_NOT_INITIALIZED)
- log->FilterLevel = WLog_GetFilterLogLevel(log);
- if (log->FilterLevel > WLOG_FILTER_NOT_FILTERED)
- return (DWORD)log->FilterLevel;
- else if (log->Level == WLOG_LEVEL_INHERIT)
- log->Level = WLog_GetLogLevel(log->Parent);
- return log->Level;
- }
- BOOL WLog_IsLevelActive(wLog* _log, DWORD _log_level)
- {
- DWORD level;
- if (!_log)
- return FALSE;
- level = WLog_GetLogLevel(_log);
- if (level == WLOG_OFF)
- return FALSE;
- return _log_level >= level;
- }
- BOOL WLog_SetStringLogLevel(wLog* log, LPCSTR level)
- {
- int lvl;
- if (!log || !level)
- return FALSE;
- lvl = WLog_ParseLogLevel(level);
- if (lvl < 0)
- return FALSE;
- return WLog_SetLogLevel(log, (DWORD)lvl);
- }
- static BOOL WLog_reset_log_filters(wLog* log)
- {
- DWORD x;
- if (!log)
- return FALSE;
- log->FilterLevel = WLOG_FILTER_NOT_INITIALIZED;
- for (x = 0; x < log->ChildrenCount; x++)
- {
- wLog* child = log->Children[x];
- if (!WLog_reset_log_filters(child))
- return FALSE;
- }
- return TRUE;
- }
- BOOL WLog_AddStringLogFilters(LPCSTR filter)
- {
- DWORD pos;
- DWORD size;
- DWORD count;
- LPSTR p;
- LPSTR filterStr;
- LPSTR cp;
- wLogFilter* tmp;
- if (!filter)
- return FALSE;
- count = 1;
- p = (LPSTR)filter;
- while ((p = strchr(p, ',')) != NULL)
- {
- count++;
- p++;
- }
- pos = g_FilterCount;
- size = g_FilterCount + count;
- tmp = (wLogFilter*)realloc(g_Filters, size * sizeof(wLogFilter));
- if (!tmp)
- return FALSE;
- g_Filters = tmp;
- cp = (LPSTR)_strdup(filter);
- if (!cp)
- return FALSE;
- p = cp;
- filterStr = cp;
- do
- {
- p = strchr(p, ',');
- if (p)
- *p = '\0';
- if (pos < size)
- {
- if (!WLog_ParseFilter(&g_Filters[pos++], filterStr))
- {
- free(cp);
- return FALSE;
- }
- }
- else
- break;
- if (p)
- {
- filterStr = p + 1;
- p++;
- }
- } while (p != NULL);
- g_FilterCount = size;
- free(cp);
- return WLog_reset_log_filters(WLog_GetRoot());
- }
- static BOOL WLog_UpdateInheritLevel(wLog* log, DWORD logLevel)
- {
- if (!log)
- return FALSE;
- if (log->inherit)
- {
- DWORD x;
- log->Level = logLevel;
- for (x = 0; x < log->ChildrenCount; x++)
- {
- wLog* child = log->Children[x];
- if (!WLog_UpdateInheritLevel(child, logLevel))
- return FALSE;
- }
- }
- return TRUE;
- }
- BOOL WLog_SetLogLevel(wLog* log, DWORD logLevel)
- {
- DWORD x;
- if (!log)
- return FALSE;
- if ((logLevel > WLOG_OFF) && (logLevel != WLOG_LEVEL_INHERIT))
- logLevel = WLOG_OFF;
- log->Level = logLevel;
- log->inherit = (logLevel == WLOG_LEVEL_INHERIT) ? TRUE : FALSE;
- for (x = 0; x < log->ChildrenCount; x++)
- {
- wLog* child = log->Children[x];
- if (!WLog_UpdateInheritLevel(child, logLevel))
- return FALSE;
- }
- return WLog_reset_log_filters(log);
- }
- int WLog_ParseLogLevel(LPCSTR level)
- {
- int iLevel = -1;
- if (!level)
- return -1;
- if (_stricmp(level, "TRACE") == 0)
- iLevel = WLOG_TRACE;
- else if (_stricmp(level, "DEBUG") == 0)
- iLevel = WLOG_DEBUG;
- else if (_stricmp(level, "INFO") == 0)
- iLevel = WLOG_INFO;
- else if (_stricmp(level, "WARN") == 0)
- iLevel = WLOG_WARN;
- else if (_stricmp(level, "ERROR") == 0)
- iLevel = WLOG_ERROR;
- else if (_stricmp(level, "FATAL") == 0)
- iLevel = WLOG_FATAL;
- else if (_stricmp(level, "OFF") == 0)
- iLevel = WLOG_OFF;
- return iLevel;
- }
- BOOL WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
- {
- char* p;
- char* q;
- int count;
- LPSTR names;
- int iLevel;
- count = 1;
- if (!name)
- return FALSE;
- p = (char*)name;
- if (p)
- {
- while ((p = strchr(p, '.')) != NULL)
- {
- count++;
- p++;
- }
- }
- names = _strdup(name);
- if (!names)
- return FALSE;
- filter->NameCount = count;
- 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;
- filter->Names[count++] = p;
- q = strrchr(p, ':');
- if (!q)
- {
- free(names);
- free(filter->Names);
- filter->Names = NULL;
- filter->NameCount = 0;
- return FALSE;
- }
- *q = '\0';
- q++;
- iLevel = WLog_ParseLogLevel(q);
- if (iLevel < 0)
- {
- free(names);
- free(filter->Names);
- filter->Names = NULL;
- filter->NameCount = 0;
- return FALSE;
- }
- filter->Level = (DWORD)iLevel;
- while ((p = strchr(p, '.')) != NULL)
- {
- if (count < (int)filter->NameCount)
- filter->Names[count++] = p + 1;
- *p = '\0';
- p++;
- }
- return TRUE;
- }
- BOOL WLog_ParseFilters(void)
- {
- LPCSTR filter = "WLOG_FILTER";
- BOOL res = FALSE;
- char* env;
- DWORD nSize;
- free(g_Filters);
- g_Filters = NULL;
- g_FilterCount = 0;
- nSize = GetEnvironmentVariableA(filter, NULL, 0);
- if (nSize < 1)
- return TRUE;
- env = (LPSTR)malloc(nSize);
- if (!env)
- return FALSE;
- if (GetEnvironmentVariableA(filter, env, nSize) == nSize - 1)
- res = WLog_AddStringLogFilters(env);
- free(env);
- return res;
- }
- LONG WLog_GetFilterLogLevel(wLog* log)
- {
- DWORD i, j;
- BOOL match = FALSE;
- if (log->FilterLevel >= 0)
- return log->FilterLevel;
- for (i = 0; i < g_FilterCount; i++)
- {
- for (j = 0; j < g_Filters[i].NameCount; j++)
- {
- if (j >= log->NameCount)
- break;
- if (_stricmp(g_Filters[i].Names[j], "*") == 0)
- {
- match = TRUE;
- break;
- }
- if (_stricmp(g_Filters[i].Names[j], log->Names[j]) != 0)
- break;
- if (j == (log->NameCount - 1))
- {
- match = TRUE;
- break;
- }
- }
- if (match)
- break;
- }
- if (match)
- log->FilterLevel = g_Filters[i].Level;
- else
- log->FilterLevel = WLOG_FILTER_NOT_FILTERED;
- return log->FilterLevel;
- }
- static BOOL WLog_ParseName(wLog* log, LPCSTR name)
- {
- char* p;
- int count;
- LPSTR names;
- count = 1;
- p = (char*)name;
- while ((p = strchr(p, '.')) != NULL)
- {
- count++;
- p++;
- }
- names = _strdup(name);
- if (!names)
- return FALSE;
- log->NameCount = count;
- log->Names = (LPSTR*)calloc((count + 1UL), sizeof(LPSTR));
- if (!log->Names)
- {
- free(names);
- return FALSE;
- }
- log->Names[count] = NULL;
- count = 0;
- p = (char*)names;
- log->Names[count++] = p;
- while ((p = strchr(p, '.')) != NULL)
- {
- if (count < (int)log->NameCount)
- log->Names[count++] = p + 1;
- *p = '\0';
- p++;
- }
- return TRUE;
- }
- wLog* WLog_New(LPCSTR name, wLog* rootLogger)
- {
- wLog* log = NULL;
- char* env = NULL;
- DWORD nSize;
- int iLevel;
- log = (wLog*)calloc(1, sizeof(wLog));
- if (!log)
- return NULL;
- log->Name = _strdup(name);
- if (!log->Name)
- goto out_fail;
- if (!WLog_ParseName(log, name))
- goto out_fail;
- log->Parent = rootLogger;
- log->ChildrenCount = 0;
- log->ChildrenSize = 16;
- log->FilterLevel = WLOG_FILTER_NOT_INITIALIZED;
- if (!(log->Children = (wLog**)calloc(log->ChildrenSize, sizeof(wLog*))))
- goto out_fail;
- log->Appender = NULL;
- if (rootLogger)
- {
- log->Level = WLOG_LEVEL_INHERIT;
- log->inherit = TRUE;
- }
- else
- {
- // Change temporary. [3/26/2020 12:40 Gifur]
- LPCSTR level = "WLOG_LEVEL";
- log->Level = WLOG_TRACE/*WLOG_INFO*/;
- nSize = GetEnvironmentVariableA(level, NULL, 0);
- if (nSize)
- {
- env = (LPSTR)malloc(nSize);
- if (!env)
- goto out_fail;
- if (GetEnvironmentVariableA(level, env, nSize) != nSize - 1)
- {
- fprintf(stderr, "%s environment variable changed in my back !\n", level);
- free(env);
- goto out_fail;
- }
- iLevel = WLog_ParseLogLevel(env);
- free(env);
- if (iLevel >= 0)
- {
- if (!WLog_SetLogLevel(log, (DWORD)iLevel))
- goto out_fail;
- }
- }
- }
- iLevel = WLog_GetFilterLogLevel(log);
- if (iLevel >= 0)
- {
- if (!WLog_SetLogLevel(log, (DWORD)iLevel))
- goto out_fail;
- }
- return log;
- out_fail:
- free(log->Children);
- free(log->Name);
- free(log);
- return NULL;
- }
- void WLog_Free(wLog* log)
- {
- if (log)
- {
- if (log->Appender)
- {
- WLog_Appender_Free(log, log->Appender);
- log->Appender = NULL;
- }
- free(log->Name);
- free(log->Names[0]);
- free(log->Names);
- free(log->Children);
- free(log);
- }
- }
- wLog* WLog_GetRoot(void)
- {
- if (!InitOnceExecuteOnce(&_WLogInitialized, WLog_InitializeRoot, NULL, NULL))
- return NULL;
- return g_RootLog;
- }
- static BOOL WLog_AddChild(wLog* parent, wLog* child)
- {
- if (parent->ChildrenCount >= parent->ChildrenSize)
- {
- wLog** tmp;
- parent->ChildrenSize *= 2;
- 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 TRUE;
- }
- static wLog* WLog_FindChild(LPCSTR name)
- {
- DWORD index;
- wLog* root;
- wLog* child = NULL;
- BOOL found = FALSE;
- root = WLog_GetRoot();
- if (!root)
- return NULL;
- for (index = 0; index < root->ChildrenCount; index++)
- {
- child = root->Children[index];
- if (strcmp(child->Name, name) == 0)
- {
- found = TRUE;
- break;
- }
- }
- return (found) ? child : NULL;
- }
- wLog* WLog_Get(LPCSTR name)
- {
- wLog* log;
- if (!(log = WLog_FindChild(name)))
- {
- 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;
- }
- BOOL WLog_Init(void)
- {
- return WLog_GetRoot() != NULL;
- }
- BOOL WLog_Uninit(void)
- {
- return TRUE;
- }
- void WLog_initRVC(const char* key)
- {
- char t[256] = { '\0' };
- if (strnicmp(key, "SpShell", strlen("SpShell")) != 0) {
- if (GetEnvironmentVariableA("WLOG_APPENDER", NULL, 0) == 0) {
- return;
- }
- }
- SetEnvironmentVariableA("WLOG_APPENDER", "FILE");
- #ifndef _WIN32
- sprintf(t, "/opt/wlog/%s", key);
- SetEnvironmentVariableA("WLOG_FILEAPPENDER_OUTPUT_FILE_PATH", t);
- if (WLog_SetLogAppenderType(WLog_GetRoot(), WLOG_APPENDER_FILE)) {
- WLog_OpenAppender(WLog_GetRoot());
- }
- #else
- sprintf(g_logRoot, "D:\\rvc\\Temp\\%s", key);
- CreateDirectoryA(g_logRoot, NULL);
- #endif //NOT _WIN32
-
- }
|