// LogAPI.cpp : 6(Re DLL S&SC3LPr5D5<3v:/J}!# // #include "debug.h" #include #include #include #include #include #include #include #include #include #include #include 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 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