AppTLSLog.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include "String.Format.tlh"
  3. #include "SystemLog.hpp"
  4. #ifndef XDWLog_EXPORTS
  5. #define AppTLSLog_API _declspec(dllimport)
  6. #else
  7. #define AppTLSLog_API _declspec(dllexport)
  8. #endif
  9. #pragma warning (disable : 4996)
  10. //warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead.
  11. //To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
  12. namespace ECOM
  13. {
  14. namespace Log
  15. {
  16. AppTLSLog_API void Flush (CSystemLog * pLog, const char * str);
  17. AppTLSLog_API void NewLine (CSystemLog * pLog);
  18. class AppTLSLog_API TLSLog
  19. {
  20. static const int MAXLEN = 8192 + 1; // Log 一行的最大长度
  21. protected:
  22. TLSLog () = delete;
  23. protected:
  24. static char * TlsGetBuffer ();
  25. public: // 改成 public, 作为测试用, 本来是可以 protected
  26. template <typename... Args>
  27. static inline char * Format (const char * src, Args... args)
  28. {
  29. auto buf_org = TlsGetBuffer ();
  30. auto len = ECOM::Utility::StringFormat (buf_org, MAXLEN).Format (src, args...);
  31. return buf_org;
  32. }
  33. public:
  34. static void Flush (CSystemLog * pLog, CSystemLog::Severity s);
  35. #if 0
  36. protected:
  37. template <typename... Args>
  38. static void Log (CSystemLog * pLog, const char * format, Args... args)
  39. {
  40. Format ((char *) format, args...);
  41. Flush (pLog, CSystemLog::STY_DEBUG);
  42. }
  43. template <typename... Args>
  44. static void Info (CSystemLog * pLog, const char * format, Args... args)
  45. {
  46. Format ((char *) format, args...);
  47. Flush (pLog, CSystemLog::STY_INFO);
  48. }
  49. template <typename... Args>
  50. static void Warning (CSystemLog * pLog, const char * format, Args... args)
  51. {
  52. Format ((char *) format, args...);
  53. Flush (pLog, CSystemLog::STY_WARNING);
  54. }
  55. //< 不写到错误日志中
  56. template <typename... Args>
  57. static void Error (CSystemLog * pLog, const char * format, Args... args)
  58. {
  59. Format ((char *) format, args...);
  60. Flush (pLog, CSystemLog::STY_ERROR);
  61. }
  62. template <typename... Args>
  63. static void Fatal (CSystemLog * pLog, const char * format, Args... args)
  64. {
  65. Format ((char *) format, args...);
  66. Flush (pLog, CSystemLog::STY_CRITICAL);
  67. }
  68. //>
  69. //< 写到错误日志中
  70. template <typename... Args>
  71. static void Error (CSystemLog * pLog, CSystemLog * pErr, const char * format, Args... args)
  72. {
  73. Format ((char *) format, args...);
  74. Flush (pLog, CSystemLog::STY_ERROR);
  75. Flush (pErr, CSystemLog::STY_ERROR);
  76. }
  77. template <typename... Args>
  78. static void Fatal (CSystemLog * pLog, CSystemLog * pErr, const char * format, Args... args)
  79. {
  80. Format ((char *) format, args...);
  81. Flush (pLog, CSystemLog::STY_CRITICAL);
  82. Flush (pErr, CSystemLog::STY_CRITICAL);
  83. }
  84. //>
  85. #endif
  86. };
  87. }
  88. }