Logger.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // Logger.h
  3. // MingMen
  4. //
  5. // Created by 罗云飞 on 2017/3/9.
  6. // Copyright © 2017年 罗云飞. All rights reserved.
  7. //
  8. #ifndef Logger_h
  9. #define Logger_h
  10. #define StarDebug DEBUG // DEBUG环境下使用
  11. #if StarDebug /* Debug Begin */
  12. static const char * NSLogTitle = "Info";
  13. static const char * InfoTitle = "Info";
  14. static const char * DebugTitle = "Debug";
  15. static const char * WarningTitle = "Warning";
  16. static const char * ErrorTitle = "Error";
  17. static const char * SuccessTitle = "Success";
  18. static const char * FailureTitle = "Failure";
  19. static const char * AssertTitle = "Assert";
  20. static const char * BackTraceTitle = "BackTrace";
  21. static const int StarBackTrace = 0; // 开关方法调用栈输出
  22. static const int StarBackTraceDepth = 4; // 栈深度
  23. #define PrivateLog(color, title, stack, format, ...)\
  24. printf("<%s> [%s][方法%s%d行]\n %s %s\n",\
  25. star_current_time(),\
  26. title,\
  27. [[[NSString stringWithUTF8String:__func__] lastPathComponent] UTF8String],\
  28. __LINE__,\
  29. [[NSString stringWithFormat:format,##__VA_ARGS__] UTF8String],\
  30. star_back_trace(stack, StarBackTraceDepth)\
  31. );\
  32. // NSLog
  33. #define NSLog(format, ...) \
  34. PrivateLog(0, NSLogTitle, StarBackTrace, format, ##__VA_ARGS__)
  35. #define NSLogCMD \
  36. NSLog(@"%s", __PRETTY_FUNCTION__);
  37. // Information
  38. #define LogInfo(format, ...) \
  39. PrivateLog(0, InfoTitle, StarBackTrace, format, ##__VA_ARGS__)
  40. // Debug
  41. #define LogDebug(format, ...) \
  42. PrivateLog(0, DebugTitle, StarBackTrace, format, ##__VA_ARGS__)
  43. // Warning
  44. #define LogWarning(format, ...) \
  45. PrivateLog(0, WarningTitle, StarBackTrace, format, ##__VA_ARGS__)
  46. // Error
  47. #define LogError(format, ...) \
  48. PrivateLog(0, ErrorTitle, StarBackTrace, format, ##__VA_ARGS__)
  49. // Success
  50. #define LogSuccess(format, ...) \
  51. PrivateLog(0, SuccessTitle, StarBackTrace, format, ##__VA_ARGS__)
  52. // Failure
  53. #define LogFailure(format, ...) \
  54. PrivateLog(0, FailureTitle, StarBackTrace, format, ##__VA_ARGS__)
  55. // Assert
  56. #define LogAssert(condition, format, ...)\
  57. PrivateLog(0, FailureTitle, StarBackTrace, format, ##__VA_ARGS__);\
  58. NSAssert(condition, format, ##__VA_ARGS__)
  59. // Stack
  60. #define LogBackTrace(format, ...) \
  61. PrivateLog(0, BackTraceTitle, 1, format, ##__VA_ARGS__)\
  62. #else /* Debug Else */
  63. #define PrivateLog(color, title, format, ...) while(0){}
  64. #define NSLog(...) while(0){}
  65. #define NSLogCMD while(0){}
  66. #define LogInfo(...) while(0){}
  67. #define LogDebug(...) while(0){}
  68. #define LogError(...) while(0){}
  69. #define LogWarning(...) while(0){}
  70. #define LogSuccess(...) while(0){}
  71. #define LogFailure(...) while(0){}
  72. #define LogAssert(condition, format, ...) while(0){}
  73. #define LogBackTrace(...) while(0){}
  74. #endif /* Debug End */
  75. /* Function Begin */
  76. #include <string.h>
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79. #include <execinfo.h>
  80. #include <time.h>
  81. #include <sys/timeb.h>
  82. static const int STAR_BACK_TRACE_BUFFER = 4096;
  83. static const int STAR_TIME_BUFFER = 255;
  84. static inline const char * star_back_trace(int open, int depth) {
  85. if (!open) {
  86. return "";
  87. }
  88. static char star_back_trace_str[STAR_BACK_TRACE_BUFFER];
  89. void *callstack[128];
  90. int frames = backtrace(callstack, 128);
  91. char **strs = backtrace_symbols(callstack, frames);
  92. memset(star_back_trace_str, 0, STAR_BACK_TRACE_BUFFER * sizeof(char));
  93. strcat(star_back_trace_str, "\n<BackTrace Begin>");
  94. for (int i = 1; i < frames; i++) {
  95. // if (strlen(star_back_trace_str) + strlen(strs[i]) + 16 > STAR_BACK_TRACE_BUFFER)
  96. // break;
  97. strcat(star_back_trace_str, "\n\t");
  98. strcat(star_back_trace_str, strs[i]);
  99. if (i == depth)
  100. break;
  101. }
  102. strcat(star_back_trace_str, "\n<End>");
  103. free(strs);
  104. strs = NULL;
  105. return star_back_trace_str;
  106. }
  107. static inline const char * star_current_time() {
  108. static char star_time_str[STAR_TIME_BUFFER];
  109. struct timeb timeinfo;
  110. ftime(&timeinfo);
  111. struct tm * second_timeinfo;
  112. second_timeinfo = localtime(&timeinfo.time);
  113. memset(star_time_str, 0, STAR_TIME_BUFFER * sizeof(char));
  114. strftime(star_time_str, STAR_TIME_BUFFER, "%Y-%m-%d %H:%M:%S", second_timeinfo);
  115. char millitm[16];
  116. sprintf(millitm, ".%03d", timeinfo.millitm);
  117. strcat(star_time_str, millitm);
  118. return star_time_str;
  119. }
  120. #endif /* Logger_h */