123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- // LogAPI.cpp : 6(Re DLL S&SC3LPr5D5<3v:/J}!#
- //
- #include "debug.h"
- #include <stdarg.h>
- #include <stdio.h>
- #include <iostream>
- #include <vector>
- #include <string.h>
- #include <time.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <locale.h>
- #include <wchar.h>
- using namespace std;
- #ifdef _WIN32
- #pragma warning(push)
- #pragma warning(disable:4267)
- #endif
- #ifdef EL_SERVER
- #define CONSOLE_OUTPUT
- #endif
- #ifdef _WIN32
- HANDLE dump_Mutex;
- #else
- pthread_mutex_t dump_Mutex = PTHREAD_MUTEX_INITIALIZER;
- #endif
- bool dum_log_file = true;
- char *g_log_abuff = NULL;
- wchar_t *g_log_wbuff = NULL;
- #define EAST_LIGHT_LOG_FILE ("ccos_start_log.log")
- void GetLogFile(char*szDir)
- {
- char csCurrentPath[MAX_PATH] = {0};
- ssize_t len = readlink("/proc/self/exe", csCurrentPath, sizeof(csCurrentPath) - 1);
- if (len != -1) {
- csCurrentPath[len] = '\0';
- char* last_slash = strrchr(csCurrentPath, '/');
- if (last_slash) *last_slash = '\0';
- }
- strcat(csCurrentPath, "/");
- strcat(csCurrentPath, EAST_LIGHT_LOG_FILE);
- strcpy(szDir, csCurrentPath);
- }
- std::wstring myformat(const wchar_t *fmt, ...)
- {
- std::wstring strResult=L"";
- if (NULL != fmt)
- {
- va_list marker;
- va_start(marker, fmt);
- size_t nLength = vswprintf(NULL, 0, fmt, marker) + 1;
- std::vector<wchar_t> vBuffer(nLength, L'\0');
- vswprintf(&vBuffer[0], nLength, fmt, marker);
- if (vBuffer[0]) strResult = &vBuffer[0];
- va_end(marker);
- }
- return strResult;
- }
- std::wstring ConvU8ToWCHAR(char *pChar)
- {
- std::wstring str;
- size_t len = mbstowcs(NULL, pChar, 0) + 1;
- if (len > 0) {
- str.resize(len);
- mbstowcs(&str[0], pChar, len);
- }
- return str;
- }
- void ClearDumpFile()
- {
- if(g_log_wbuff)
- {
- delete []g_log_wbuff;
- g_log_wbuff = NULL;
- g_log_abuff = NULL;
- }
- }
- bool InitDumpFile()
- {
- if(g_log_wbuff == NULL)
- {
- g_log_wbuff = new wchar_t[ONE_LOG_MAX_SIZE];//512k buff
- if(g_log_wbuff == NULL)
- {
- return false;
- }
- g_log_abuff = (char *)g_log_wbuff;
- #ifdef _WIN32
- dump_Mutex = CreateMutex(0, 0, 0);
- #endif
- }
- return true;
- }
- void SetPrintLog(bool Enable)
- {
- dum_log_file = Enable;
- }
- void __DebugPrintA(
- char* file,
- int line,
- char* function,
- char* format,
- ...
- )
- {
- const char* strFileName;
- va_list args;
- if (!InitDumpFile()) return;
- va_start(args, format);
- strFileName = strrchr(file, '/');
- if (!strFileName) strFileName = file;
- else strFileName++;
- memset(g_log_abuff, 0, ONE_LOG_MAX_SIZE);
- // 时间处理
- time_t rawtime;
- struct tm timeinfo;
- time(&rawtime);
- localtime_r(&rawtime, &timeinfo);
- char timebuf[80];
- strftime(timebuf, sizeof(timebuf), "[%Y/%m/%d %H:%M:%S] ", &timeinfo);
- snprintf(g_log_abuff, ONE_LOG_MAX_SIZE, "[%s]<%s>(%d), %s(): ",
- PROJECT_NAME, strFileName, line, function);
- vsnprintf(g_log_abuff + strlen(g_log_abuff),
- ONE_LOG_MAX_SIZE - strlen(g_log_abuff), format, args);
- if (dum_log_file) {
- char szLogFile[MAX_PATH] = { 0 };
- GetLogFile(szLogFile);
- #ifdef _WIN32
- WaitForSingleObject(dump_Mutex, INFINITE);
- #else
- pthread_mutex_lock(&dump_Mutex);
- #endif
- FILE* fp = fopen(szLogFile, "a");
- if (fp) {
- fwrite(timebuf, 1, strlen(timebuf), fp);
- fwrite(g_log_abuff, 1, strlen(g_log_abuff), fp);
- fwrite("\n", 1, 1, fp);
- fclose(fp);
- }
- #ifdef _WIN32
- ReleaseMutex(dump_Mutex);
- #else
- pthread_mutex_unlock(&dump_Mutex);
- #endif
- }
- // 控制台输出
- fprintf(stderr, "%s", timebuf);
- fprintf(stderr, "%s\n", g_log_abuff);
- #ifdef CONSOLE_OUTPUT
- printf("%s", timebuf);
- printf("%s\n", g_log_abuff);
- #endif
- va_end(args);
- }
- void __DebugPrintW(
- char* file,
- int line,
- char* function,
- wchar_t* format,
- ...
- )
- {
- const char* strFileName;
- va_list args;
- if (!InitDumpFile()) return;
- va_start(args, format);
- strFileName = strrchr(file, '/');
- if (!strFileName) strFileName = file;
- else strFileName++;
- memset(g_log_wbuff, 0, ONE_LOG_MAX_SIZE * sizeof(wchar_t));
- // 时间处理
- time_t rawtime;
- struct tm timeinfo;
- time(&rawtime);
- localtime_r(&rawtime, &timeinfo);
- wchar_t timebuf[80];
- wcsftime(timebuf, sizeof(timebuf) / sizeof(wchar_t), L"[%Y/%m/%d %H:%M:%S] ", &timeinfo);
- swprintf(g_log_wbuff, ONE_LOG_MAX_SIZE / sizeof(wchar_t), L"[%s]<%s>(%d), %s(): ",
- PROJECT_NAME, strFileName, line, function);
- vswprintf(g_log_wbuff + wcslen(g_log_wbuff),
- ONE_LOG_MAX_SIZE / sizeof(wchar_t) - wcslen(g_log_wbuff), format, args);
- if (dum_log_file) {
- char szLogFile[MAX_PATH] = { 0 };
- GetLogFile(szLogFile);
- #ifdef _WIN32
- WaitForSingleObject(dump_Mutex, INFINITE);
- #else
- pthread_mutex_lock(&dump_Mutex);
- #endif
- FILE* fp = fopen(szLogFile, "a");
- if (fp) {
- char narrow_time[256];
- wcstombs(narrow_time, timebuf, sizeof(narrow_time));
- fwrite(narrow_time, 1, strlen(narrow_time), fp);
- char narrow_buf[ONE_LOG_MAX_SIZE];
- wcstombs(narrow_buf, g_log_wbuff, sizeof(narrow_buf));
- fwrite(narrow_buf, 1, strlen(narrow_buf), fp);
- fwrite("\n", 1, 1, fp);
- fclose(fp);
- }
- #ifdef _WIN32
- ReleaseMutex(dump_Mutex);
- #else
- pthread_mutex_unlock(&dump_Mutex);
- #endif
- }
- // 控制台输出
- fwprintf(stderr, L"%s", timebuf);
- fwprintf(stderr, L"%s\n", g_log_wbuff);
- va_end(args);
- }
- void __ReleasePrintA(
- char* file,
- int line,
- char* function,
- char* format,
- ...
- )
- {
- if (dum_log_file) {
- char szLogFile[MAX_PATH] = { 0 };
- const char* strFileName;
- va_list args;
- if (!InitDumpFile()) return;
- va_start(args, format);
- strFileName = strrchr(file, '/');
- if (!strFileName) strFileName = file;
- else strFileName++;
- #ifdef _WIN32
- WaitForSingleObject(dump_Mutex, INFINITE);
- #else
- pthread_mutex_lock(&dump_Mutex);
- #endif
- memset(g_log_abuff, 0, ONE_LOG_MAX_SIZE);
- // 时间处理
- time_t rawtime;
- struct tm timeinfo;
- time(&rawtime);
- localtime_r(&rawtime, &timeinfo);
- char timebuf[80];
- strftime(timebuf, sizeof(timebuf), "[%Y/%m/%d %H:%M:%S] ", &timeinfo);
- vsnprintf(g_log_abuff, ONE_LOG_MAX_SIZE, format, args);
- GetLogFile(szLogFile);
- FILE* fp = fopen(szLogFile, "a");
- if (fp) {
- fwrite(timebuf, 1, strlen(timebuf), fp);
- fwrite(g_log_abuff, 1, strlen(g_log_abuff), fp);
- fwrite("\n", 1, 1, fp);
- fclose(fp);
- }
- #ifdef _WIN32
- ReleaseMutex(dump_Mutex);
- #else
- pthread_mutex_unlock(&dump_Mutex);
- #endif
- va_end(args);
- }
- }
- void __ReleasePrintW(
- char* file,
- int line,
- char* function,
- wchar_t* format,
- ...
- )
- {
- if (dum_log_file) {
- char szLogFile[MAX_PATH] = { 0 };
- const char* strFileName;
- va_list args;
- if (!InitDumpFile()) return;
- va_start(args, format);
- strFileName = strrchr(file, '/');
- if (!strFileName) strFileName = file;
- else strFileName++;
- #ifdef _WIN32
- WaitForSingleObject(dump_Mutex, INFINITE);
- #else
- pthread_mutex_lock(&dump_Mutex);
- #endif
- memset(g_log_wbuff, 0, ONE_LOG_MAX_SIZE * sizeof(wchar_t));
- // 时间处理
- time_t rawtime;
- struct tm timeinfo;
- time(&rawtime);
- localtime_r(&rawtime, &timeinfo);
- wchar_t timebuf[80];
- wcsftime(timebuf, sizeof(timebuf) / sizeof(wchar_t), L"[%Y/%m/%d %H:%M:%S] ", &timeinfo);
- vswprintf(g_log_wbuff, ONE_LOG_MAX_SIZE / sizeof(wchar_t), format, args);
- GetLogFile(szLogFile);
- FILE* fp = fopen(szLogFile, "a");
- if (fp) {
- char narrow_time[256];
- wcstombs(narrow_time, timebuf, sizeof(narrow_time));
- fwrite(narrow_time, 1, strlen(narrow_time), fp);
- char narrow_buf[ONE_LOG_MAX_SIZE];
- wcstombs(narrow_buf, g_log_wbuff, sizeof(narrow_buf));
- fwrite(narrow_buf, 1, strlen(narrow_buf), fp);
- fwrite("\n", 1, 1, fp);
- fclose(fp);
- }
- #ifdef _WIN32
- ReleaseMutex(dump_Mutex);
- #else
- pthread_mutex_unlock(&dump_Mutex);
- #endif
- va_end(args);
- }
- }
- #ifdef _WIN32
- #pragma warning(pop)
- #endif
|