log.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef __LOG_H__
  2. #define __LOG_H__
  3. #include "type.h"
  4. #ifdef WIN32
  5. #define filename(x) strrchr(x,'\\')?strrchr(x,'\\')+1:x
  6. #else
  7. #if __GNUC__ >= 4
  8. #define filename(x) strrchr(x,'/')?strrchr(x,'/')+1:x
  9. #endif
  10. #endif
  11. EXPORT_API INT32 initLog(const char* loggerName, const char* fileName, int level, bool openConsole = true);
  12. EXPORT_API void logTrace(const char* pcLogStr);
  13. EXPORT_API void logDebug(const char* pcLogStr);
  14. EXPORT_API void logInfo(const char* pcLogStr);
  15. EXPORT_API void logWarn(const char* pcLogStr);
  16. EXPORT_API void logError(const char* pcLogStr);
  17. EXPORT_API void logCritical(const char* pcLogStr);
  18. EXPORT_API UINT64 gettid();
  19. #define CREATE_LOG_HEAD \
  20. char acLogStr[1024*4] = {0};\
  21. INT32 iHeadLength = snprintf(acLogStr, 1024*4, "[tid:%llu] : ", gettid());\
  22. //INT32 iHeadLength = snprintf(acLogStr, 1024*4, "[tid:%llu] [%s:%d] %s: ", gettid(), filename(__FILE__), __LINE__, __FUNCTION__);\
  23. #define LOG_TRACE(...) \
  24. {\
  25. CREATE_LOG_HEAD\
  26. snprintf(acLogStr+iHeadLength, sizeof(acLogStr)-iHeadLength, __VA_ARGS__);\
  27. logTrace(acLogStr);\
  28. }
  29. #define LOG_DEBUG(...) \
  30. {\
  31. CREATE_LOG_HEAD\
  32. snprintf(acLogStr+iHeadLength, sizeof(acLogStr)-iHeadLength, __VA_ARGS__);\
  33. logDebug(acLogStr);\
  34. }
  35. #define LOG_INFO(...) \
  36. {\
  37. CREATE_LOG_HEAD\
  38. snprintf(acLogStr+iHeadLength, sizeof(acLogStr)-iHeadLength, __VA_ARGS__);\
  39. logInfo(acLogStr);\
  40. }
  41. #define LOG_WARN(...) \
  42. {\
  43. CREATE_LOG_HEAD\
  44. snprintf(acLogStr+iHeadLength, sizeof(acLogStr)-iHeadLength, __VA_ARGS__);\
  45. logWarn(acLogStr);\
  46. }
  47. #define LOG_ERROR(...) \
  48. {\
  49. CREATE_LOG_HEAD\
  50. snprintf(acLogStr+iHeadLength, sizeof(acLogStr)-iHeadLength, __VA_ARGS__);\
  51. logError(acLogStr);\
  52. }
  53. #endif // __LOG_H__