debug.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // LogAPI.cpp : 6(Re DLL S&SC3LPr5D5<3v:/J}!#
  2. //
  3. #include "debug.h"
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include <iostream>
  7. #include <vector>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #include <pthread.h>
  13. #include <locale.h>
  14. #include <wchar.h>
  15. using namespace std;
  16. #ifdef _WIN32
  17. #pragma warning(push)
  18. #pragma warning(disable:4267)
  19. #endif
  20. #ifdef EL_SERVER
  21. #define CONSOLE_OUTPUT
  22. #endif
  23. #ifdef _WIN32
  24. HANDLE dump_Mutex;
  25. #else
  26. pthread_mutex_t dump_Mutex = PTHREAD_MUTEX_INITIALIZER;
  27. #endif
  28. bool dum_log_file = true;
  29. char *g_log_abuff = NULL;
  30. wchar_t *g_log_wbuff = NULL;
  31. #define EAST_LIGHT_LOG_FILE ("ccos_start_log.log")
  32. void GetLogFile(char*szDir)
  33. {
  34. char csCurrentPath[MAX_PATH] = {0};
  35. ssize_t len = readlink("/proc/self/exe", csCurrentPath, sizeof(csCurrentPath) - 1);
  36. if (len != -1) {
  37. csCurrentPath[len] = '\0';
  38. char* last_slash = strrchr(csCurrentPath, '/');
  39. if (last_slash) *last_slash = '\0';
  40. }
  41. strcat(csCurrentPath, "/");
  42. strcat(csCurrentPath, EAST_LIGHT_LOG_FILE);
  43. strcpy(szDir, csCurrentPath);
  44. }
  45. std::wstring myformat(const wchar_t *fmt, ...)
  46. {
  47. std::wstring strResult=L"";
  48. if (NULL != fmt)
  49. {
  50. va_list marker;
  51. va_start(marker, fmt);
  52. size_t nLength = vswprintf(NULL, 0, fmt, marker) + 1;
  53. std::vector<wchar_t> vBuffer(nLength, L'\0');
  54. vswprintf(&vBuffer[0], nLength, fmt, marker);
  55. if (vBuffer[0]) strResult = &vBuffer[0];
  56. va_end(marker);
  57. }
  58. return strResult;
  59. }
  60. std::wstring ConvU8ToWCHAR(char *pChar)
  61. {
  62. std::wstring str;
  63. size_t len = mbstowcs(NULL, pChar, 0) + 1;
  64. if (len > 0) {
  65. str.resize(len);
  66. mbstowcs(&str[0], pChar, len);
  67. }
  68. return str;
  69. }
  70. void ClearDumpFile()
  71. {
  72. if(g_log_wbuff)
  73. {
  74. delete []g_log_wbuff;
  75. g_log_wbuff = NULL;
  76. g_log_abuff = NULL;
  77. }
  78. }
  79. bool InitDumpFile()
  80. {
  81. if(g_log_wbuff == NULL)
  82. {
  83. g_log_wbuff = new wchar_t[ONE_LOG_MAX_SIZE];//512k buff
  84. if(g_log_wbuff == NULL)
  85. {
  86. return false;
  87. }
  88. g_log_abuff = (char *)g_log_wbuff;
  89. #ifdef _WIN32
  90. dump_Mutex = CreateMutex(0, 0, 0);
  91. #endif
  92. }
  93. return true;
  94. }
  95. void SetPrintLog(bool Enable)
  96. {
  97. dum_log_file = Enable;
  98. }
  99. void __DebugPrintA(
  100. char* file,
  101. int line,
  102. char* function,
  103. char* format,
  104. ...
  105. )
  106. {
  107. const char* strFileName;
  108. va_list args;
  109. if (!InitDumpFile()) return;
  110. va_start(args, format);
  111. strFileName = strrchr(file, '/');
  112. if (!strFileName) strFileName = file;
  113. else strFileName++;
  114. memset(g_log_abuff, 0, ONE_LOG_MAX_SIZE);
  115. // 时间处理
  116. time_t rawtime;
  117. struct tm timeinfo;
  118. time(&rawtime);
  119. localtime_r(&rawtime, &timeinfo);
  120. char timebuf[80];
  121. strftime(timebuf, sizeof(timebuf), "[%Y/%m/%d %H:%M:%S] ", &timeinfo);
  122. snprintf(g_log_abuff, ONE_LOG_MAX_SIZE, "[%s]<%s>(%d), %s(): ",
  123. PROJECT_NAME, strFileName, line, function);
  124. vsnprintf(g_log_abuff + strlen(g_log_abuff),
  125. ONE_LOG_MAX_SIZE - strlen(g_log_abuff), format, args);
  126. if (dum_log_file) {
  127. char szLogFile[MAX_PATH] = { 0 };
  128. GetLogFile(szLogFile);
  129. #ifdef _WIN32
  130. WaitForSingleObject(dump_Mutex, INFINITE);
  131. #else
  132. pthread_mutex_lock(&dump_Mutex);
  133. #endif
  134. FILE* fp = fopen(szLogFile, "a");
  135. if (fp) {
  136. fwrite(timebuf, 1, strlen(timebuf), fp);
  137. fwrite(g_log_abuff, 1, strlen(g_log_abuff), fp);
  138. fwrite("\n", 1, 1, fp);
  139. fclose(fp);
  140. }
  141. #ifdef _WIN32
  142. ReleaseMutex(dump_Mutex);
  143. #else
  144. pthread_mutex_unlock(&dump_Mutex);
  145. #endif
  146. }
  147. // 控制台输出
  148. fprintf(stderr, "%s", timebuf);
  149. fprintf(stderr, "%s\n", g_log_abuff);
  150. #ifdef CONSOLE_OUTPUT
  151. printf("%s", timebuf);
  152. printf("%s\n", g_log_abuff);
  153. #endif
  154. va_end(args);
  155. }
  156. void __DebugPrintW(
  157. char* file,
  158. int line,
  159. char* function,
  160. wchar_t* format,
  161. ...
  162. )
  163. {
  164. const char* strFileName;
  165. va_list args;
  166. if (!InitDumpFile()) return;
  167. va_start(args, format);
  168. strFileName = strrchr(file, '/');
  169. if (!strFileName) strFileName = file;
  170. else strFileName++;
  171. memset(g_log_wbuff, 0, ONE_LOG_MAX_SIZE * sizeof(wchar_t));
  172. // 时间处理
  173. time_t rawtime;
  174. struct tm timeinfo;
  175. time(&rawtime);
  176. localtime_r(&rawtime, &timeinfo);
  177. wchar_t timebuf[80];
  178. wcsftime(timebuf, sizeof(timebuf) / sizeof(wchar_t), L"[%Y/%m/%d %H:%M:%S] ", &timeinfo);
  179. swprintf(g_log_wbuff, ONE_LOG_MAX_SIZE / sizeof(wchar_t), L"[%s]<%s>(%d), %s(): ",
  180. PROJECT_NAME, strFileName, line, function);
  181. vswprintf(g_log_wbuff + wcslen(g_log_wbuff),
  182. ONE_LOG_MAX_SIZE / sizeof(wchar_t) - wcslen(g_log_wbuff), format, args);
  183. if (dum_log_file) {
  184. char szLogFile[MAX_PATH] = { 0 };
  185. GetLogFile(szLogFile);
  186. #ifdef _WIN32
  187. WaitForSingleObject(dump_Mutex, INFINITE);
  188. #else
  189. pthread_mutex_lock(&dump_Mutex);
  190. #endif
  191. FILE* fp = fopen(szLogFile, "a");
  192. if (fp) {
  193. char narrow_time[256];
  194. wcstombs(narrow_time, timebuf, sizeof(narrow_time));
  195. fwrite(narrow_time, 1, strlen(narrow_time), fp);
  196. char narrow_buf[ONE_LOG_MAX_SIZE];
  197. wcstombs(narrow_buf, g_log_wbuff, sizeof(narrow_buf));
  198. fwrite(narrow_buf, 1, strlen(narrow_buf), fp);
  199. fwrite("\n", 1, 1, fp);
  200. fclose(fp);
  201. }
  202. #ifdef _WIN32
  203. ReleaseMutex(dump_Mutex);
  204. #else
  205. pthread_mutex_unlock(&dump_Mutex);
  206. #endif
  207. }
  208. // 控制台输出
  209. fwprintf(stderr, L"%s", timebuf);
  210. fwprintf(stderr, L"%s\n", g_log_wbuff);
  211. va_end(args);
  212. }
  213. void __ReleasePrintA(
  214. char* file,
  215. int line,
  216. char* function,
  217. char* format,
  218. ...
  219. )
  220. {
  221. if (dum_log_file) {
  222. char szLogFile[MAX_PATH] = { 0 };
  223. const char* strFileName;
  224. va_list args;
  225. if (!InitDumpFile()) return;
  226. va_start(args, format);
  227. strFileName = strrchr(file, '/');
  228. if (!strFileName) strFileName = file;
  229. else strFileName++;
  230. #ifdef _WIN32
  231. WaitForSingleObject(dump_Mutex, INFINITE);
  232. #else
  233. pthread_mutex_lock(&dump_Mutex);
  234. #endif
  235. memset(g_log_abuff, 0, ONE_LOG_MAX_SIZE);
  236. // 时间处理
  237. time_t rawtime;
  238. struct tm timeinfo;
  239. time(&rawtime);
  240. localtime_r(&rawtime, &timeinfo);
  241. char timebuf[80];
  242. strftime(timebuf, sizeof(timebuf), "[%Y/%m/%d %H:%M:%S] ", &timeinfo);
  243. vsnprintf(g_log_abuff, ONE_LOG_MAX_SIZE, format, args);
  244. GetLogFile(szLogFile);
  245. FILE* fp = fopen(szLogFile, "a");
  246. if (fp) {
  247. fwrite(timebuf, 1, strlen(timebuf), fp);
  248. fwrite(g_log_abuff, 1, strlen(g_log_abuff), fp);
  249. fwrite("\n", 1, 1, fp);
  250. fclose(fp);
  251. }
  252. #ifdef _WIN32
  253. ReleaseMutex(dump_Mutex);
  254. #else
  255. pthread_mutex_unlock(&dump_Mutex);
  256. #endif
  257. va_end(args);
  258. }
  259. }
  260. void __ReleasePrintW(
  261. char* file,
  262. int line,
  263. char* function,
  264. wchar_t* format,
  265. ...
  266. )
  267. {
  268. if (dum_log_file) {
  269. char szLogFile[MAX_PATH] = { 0 };
  270. const char* strFileName;
  271. va_list args;
  272. if (!InitDumpFile()) return;
  273. va_start(args, format);
  274. strFileName = strrchr(file, '/');
  275. if (!strFileName) strFileName = file;
  276. else strFileName++;
  277. #ifdef _WIN32
  278. WaitForSingleObject(dump_Mutex, INFINITE);
  279. #else
  280. pthread_mutex_lock(&dump_Mutex);
  281. #endif
  282. memset(g_log_wbuff, 0, ONE_LOG_MAX_SIZE * sizeof(wchar_t));
  283. // 时间处理
  284. time_t rawtime;
  285. struct tm timeinfo;
  286. time(&rawtime);
  287. localtime_r(&rawtime, &timeinfo);
  288. wchar_t timebuf[80];
  289. wcsftime(timebuf, sizeof(timebuf) / sizeof(wchar_t), L"[%Y/%m/%d %H:%M:%S] ", &timeinfo);
  290. vswprintf(g_log_wbuff, ONE_LOG_MAX_SIZE / sizeof(wchar_t), format, args);
  291. GetLogFile(szLogFile);
  292. FILE* fp = fopen(szLogFile, "a");
  293. if (fp) {
  294. char narrow_time[256];
  295. wcstombs(narrow_time, timebuf, sizeof(narrow_time));
  296. fwrite(narrow_time, 1, strlen(narrow_time), fp);
  297. char narrow_buf[ONE_LOG_MAX_SIZE];
  298. wcstombs(narrow_buf, g_log_wbuff, sizeof(narrow_buf));
  299. fwrite(narrow_buf, 1, strlen(narrow_buf), fp);
  300. fwrite("\n", 1, 1, fp);
  301. fclose(fp);
  302. }
  303. #ifdef _WIN32
  304. ReleaseMutex(dump_Mutex);
  305. #else
  306. pthread_mutex_unlock(&dump_Mutex);
  307. #endif
  308. va_end(args);
  309. }
  310. }
  311. #ifdef _WIN32
  312. #pragma warning(pop)
  313. #endif