m4l.debug.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef __M4L_DEBUG_H
  18. #define __M4L_DEBUG_H
  19. #include <string>
  20. #include <kopano/Trace.h>
  21. #include <kopano/ECLogger.h>
  22. extern ECLogger *m4l_lpLogger;
  23. /*
  24. * We want the debug output for Mapi4Linux to go into 2 directions,
  25. * first it should go to the console using Trace.h, secondly it needs
  26. * to go to the m4l_lpLogger so it can be written into a file as well.
  27. *
  28. * We are going to override the macros from Trace.h here and make the
  29. * m4l_lpLogger interaction happening using new versions on the define.
  30. */
  31. /* Format string for function and message combination */
  32. #define LOG_PREFIX(__type) \
  33. ( ((__type) == TRACE_ENTRY) ? "Call" : \
  34. (((__type) == TRACE_WARNING) ? "Warning" : \
  35. ((((__type) == TRACE_RETURN) ? "Ret " : \
  36. "Unknown" ))) )
  37. #define LOG_FORMAT(__type, __func, __msg) \
  38. ( std::string("%lu 0x%lx MAPILIB") + LOG_PREFIX(__type) + ": " + __func + "(" + __msg + ")").c_str(), GetTickCount(), kc_threadid()
  39. /*
  40. * For Exchange redirector (WIN32) we allow the log message to be send to a logfile.
  41. * For Linux debug build we allow the log message to be send to a logfile.
  42. */
  43. #if (defined(LINUX) && defined(DEBUG))
  44. #ifdef USE_VARIADIC_MACRO
  45. #define TRACE_TO_FILE(__level, __message, __args...) \
  46. if (m4l_lpLogger) \
  47. m4l_lpLogger->Log(__level, __message, ##__args);
  48. #define TRACE_TO_FILE1 TRACE_TO_FILE
  49. #define TRACE_TO_FILE2 TRACE_TO_FILE
  50. #define TRACE_TO_FILE3 TRACE_TO_FILE
  51. #define TRACE_TO_FILE4 TRACE_TO_FILE
  52. #define TRACE_TO_FILE5 TRACE_TO_FILE
  53. #else /* USE_VARIADIC_MACRO */
  54. #define TRACE_TO_FILE(__level, __message) \
  55. if (m4l_lpLogger) \
  56. m4l_lpLogger->Log(__level, __message);
  57. #define TRACE_TO_FILE1(__level, __message, __arg1) \
  58. if (m4l_lpLogger) \
  59. m4l_lpLogger->Log(__level, __message, __arg1);
  60. #define TRACE_TO_FILE2(__level, __message, __arg1, __arg2) \
  61. if (m4l_lpLogger) \
  62. m4l_lpLogger->Log(__level, __message, __arg1, __arg2);
  63. #define TRACE_TO_FILE3(__level, __message, __arg1, __arg2, __arg3) \
  64. if (m4l_lpLogger) \
  65. m4l_lpLogger->Log(__level, __message, __arg1, __arg2, __arg3);
  66. #define TRACE_TO_FILE4(__level, __message, __arg1, __arg2, __arg3, __arg4) \
  67. if (m4l_lpLogger) \
  68. m4l_lpLogger->Log(__level, __message, __arg1, __arg2, __arg3, __arg4);
  69. #define TRACE_TO_FILE5(__level, __message, __arg1, __arg2, __arg3, __arg4, __arg5) \
  70. if (m4l_lpLogger) \
  71. m4l_lpLogger->Log(__level, __message, __arg1, __arg2, __arg3, __arg4, __arg5);
  72. #endif /* USE_VARIADIC_MACRO */
  73. #else /* (defined(LINUX) && defined(DEBUG)) */
  74. #define TRACE_TO_FILE(__level, __message) {}
  75. #define TRACE_TO_FILE1(__level, __message, __arg1) {}
  76. #define TRACE_TO_FILE2(__level, __message, __arg1, __arg2) {}
  77. #define TRACE_TO_FILE3(__level, __message, __arg1, __arg2, __arg3) {}
  78. #define TRACE_TO_FILE4(__level, __message, __arg1, __arg2, __arg3, __arg4) {}
  79. #define TRACE_TO_FILE5(__level, __message, __arg1, __arg2, __arg3, __arg4, __arg5) {}
  80. #endif /* (defined(LINUX) && defined(DEBUG)) */
  81. /*
  82. * If only we could nicely rename the TRACE_MAPILIB macro and undef it
  83. * that would have been so cool, but now we are stuck with this solution.
  84. */
  85. #ifdef DEBUG
  86. #define ORIG_TRACE_MAPILIB TraceMapiLib
  87. #else
  88. #define ORIG_TRACE_MAPILIB(...)
  89. #endif
  90. #undef TRACE_MAPILIB
  91. #ifdef USE_VARIADIC_MACRO
  92. #define TRACE_MAPILIB(__type, __function, __msg, __args...) \
  93. do { \
  94. ORIG_TRACE_MAPILIB(__type, __function, __msg, ##__args); \
  95. TRACE_TO_FILE(EC_LOGLEVEL_DEBUG, LOG_FORMAT(__type, __function, __msg), ##__args); \
  96. } while(0)
  97. #define TRACE_MAPILIB1 TRACE_MAPILIB
  98. #define TRACE_MAPILIB2 TRACE_MAPILIB
  99. #define TRACE_MAPILIB3 TRACE_MAPILIB
  100. #define TRACE_MAPILIB4 TRACE_MAPILIB
  101. #define TRACE_MAPILIB5 TRACE_MAPILIB
  102. #else /* USE_VARIADIC_MACRO */
  103. #define TRACE_MAPILIB(__type, __function, __msg) \
  104. do { \
  105. ORIG_TRACE_MAPILIB(__type, __function, __msg); \
  106. TRACE_TO_FILE(EC_LOGLEVEL_DEBUG, LOG_FORMAT(__type, __function, __msg)); \
  107. } while (0)
  108. #define TRACE_MAPILIB1(__type, __function, __msg, __arg1) \
  109. do { \
  110. ORIG_TRACE_MAPILIB(__type, __function, __msg, __arg1); \
  111. TRACE_TO_FILE1(EC_LOGLEVEL_DEBUG, LOG_FORMAT(__type, __function, __msg), __arg1); \
  112. } while (0)
  113. #define TRACE_MAPILIB2(__type, __function, __msg, __arg1, __arg2) \
  114. do { \
  115. ORIG_TRACE_MAPILIB(__type, __function, __msg, __arg1, __arg2); \
  116. TRACE_TO_FILE2(EC_LOGLEVEL_DEBUG, LOG_FORMAT(__type, __function, __msg), __arg1, __arg2); \
  117. } while (0)
  118. #define TRACE_MAPILIB3(__type, __function, __msg, __arg1, __arg2, __arg3) \
  119. do { \
  120. ORIG_TRACE_MAPILIB(__type, __function, __msg, __arg1, __arg2, __arg3); \
  121. TRACE_TO_FILE3(EC_LOGLEVEL_DEBUG, LOG_FORMAT(__type, __function, __msg), __arg1, __arg2, __arg3); \
  122. } while(0)
  123. #define TRACE_MAPILIB4(__type, __function, __msg, __arg1, __arg2, __arg3, __arg4) \
  124. do { \
  125. ORIG_TRACE_MAPILIB(__type, __function, __msg, __arg1, __arg2, __arg3, __arg4); \
  126. TRACE_TO_FILE4(EC_LOGLEVEL_DEBUG, LOG_FORMAT(__type, __function, __msg), __arg1, __arg2, __arg3, __arg4); \
  127. } while(0)
  128. #define TRACE_MAPILIB5(__type, __function, __msg, __arg1, __arg2, __arg3, __arg4, __arg5) \
  129. do { \
  130. ORIG_TRACE_MAPILIB(__type, __function, __msg, __arg1, __arg2, __arg3, __arg4, __arg5); \
  131. TRACE_TO_FILE5(EC_LOGLEVEL_DEBUG, LOG_FORMAT(__type, __function, __msg), __arg1, __arg2, __arg3, __arg4, __arg5); \
  132. } while(0)
  133. #endif /* USE_VARIADIC_MACRO */
  134. #endif /* __M4L_DEBUG_H */