ctkLogService.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKLOGSERVICE_H
  16. #define CTKLOGSERVICE_H
  17. #include <QString>
  18. #include <stdexcept>
  19. #include "ctkLogStream.h"
  20. #include <ctkServiceReference.h>
  21. /**
  22. * \ingroup LogService
  23. * Provides methods for plugins to write messages to the log.
  24. *
  25. * <p>
  26. * <code>ctkLogService</code> methods are provided to log messages; optionally with a
  27. * <code>ctkServiceReference</code> object or an exception.
  28. *
  29. * <p>
  30. * Plugins must log messages in the Plugin Framework with a severity level
  31. * according to the following hierarchy:
  32. * <ol>
  33. * <li>#LOG_ERROR
  34. * <li>#LOG_WARNING
  35. * <li>#LOG_INFO
  36. * <li>#LOG_DEBUG
  37. * </ol>
  38. *
  39. * \remarks This class is thread safe.
  40. */
  41. struct CTK_PLUGINFW_EXPORT ctkLogService
  42. {
  43. virtual ~ctkLogService() {}
  44. /**
  45. * An error message (Value 1).
  46. *
  47. * <p>
  48. * This log entry indicates the plugin or service may not be functional.
  49. */
  50. static const int LOG_ERROR; // = 1;
  51. /**
  52. * A warning message (Value 2).
  53. *
  54. * <p>
  55. * This log entry indicates a plugin or service is still functioning but may
  56. * experience problems in the future because of the warning condition.
  57. */
  58. static const int LOG_WARNING; // = 2;
  59. /**
  60. * An informational message (Value 3).
  61. *
  62. * <p>
  63. * This log entry may be the result of any change in the plugin or service
  64. * and does not indicate a problem.
  65. */
  66. static const int LOG_INFO; // = 3;
  67. /**
  68. * A debugging message (Value 4).
  69. *
  70. * <p>
  71. * This log entry is used for problem determination and may be irrelevant to
  72. * anyone but the plugin developer.
  73. */
  74. static const int LOG_DEBUG; // = 4;
  75. /**
  76. * Logs a message.
  77. *
  78. * <p>
  79. * The <code>ctkServiceReference</code> field and the <code>Exception</code> field
  80. * of the <code>ctkLogEntry</code> object will be set to <code>null</code>.
  81. *
  82. * \param level The severity of the message. This should be one of the
  83. * defined log levels but may be any integer that is interpreted in a
  84. * user defined way.
  85. * \param message Human readable string describing the condition.
  86. * \param exception The exception that reflects the condition or <code>null</code>.
  87. * \param file The current file name.
  88. * \param function The current function name.
  89. * \param line The current line number.
  90. *
  91. * \see #LOG_ERROR
  92. * \see #LOG_WARNING
  93. * \see #LOG_INFO
  94. * \see #LOG_DEBUG
  95. */
  96. virtual void log(int level, const QString& message, const std::exception* exception = 0,
  97. const char* file = 0, const char* function = 0, int line = -1) = 0;
  98. /**
  99. * Logs a message associated with a specific <code>ctkServiceReference</code>
  100. * object.
  101. *
  102. * <p>
  103. * The <code>Exception</code> field of the <code>ctkLogEntry</code> will be set to
  104. * <code>null</code>.
  105. *
  106. * \param sr The <code>ctkServiceReference</code> object of the service that this
  107. * message is associated with.
  108. * \param level The severity of the message. This should be one of the
  109. * defined log levels but may be any integer that is interpreted in a
  110. * user defined way.
  111. * \param message Human readable string describing the condition.
  112. * \param exception The exception that reflects the condition or
  113. * <code>null</code>.
  114. * \param file The current file name.
  115. * \param function The current function name.
  116. * \param line The current line number.
  117. *
  118. * \see #LOG_ERROR
  119. * \see #LOG_WARNING
  120. * \see #LOG_INFO
  121. * \see #LOG_DEBUG
  122. */
  123. virtual void log(const ctkServiceReference& sr, int level, const QString& message,
  124. const std::exception* exception = 0,
  125. const char* file = 0, const char* function = 0, int line = -1) = 0;
  126. /**
  127. * Get the current log level. The log service discards log entries with a
  128. * level that is less severe than the current level. E.g. if the current
  129. * log level is LOG_WARNING then the log service will discard all log entries with
  130. * level LOG_INFO and LOG_DEBUG.
  131. *
  132. * \return The lowest severity level that is accepted into the log.
  133. */
  134. virtual int getLogLevel() const = 0;
  135. };
  136. Q_DECLARE_INTERFACE(ctkLogService, "org.commontk.service.log.LogService")
  137. /**
  138. * \ingroup LogService
  139. */
  140. class CTK_PLUGINFW_EXPORT ctkLogStreamWithServiceRef : public ctkLogStream
  141. {
  142. public:
  143. ctkLogStreamWithServiceRef(ctkLogService* logService, const ctkServiceReference& sr,
  144. int level, const std::exception* exc = 0,
  145. const char* file = 0, const char* function = 0, int line = -1);
  146. ctkLogStreamWithServiceRef(const ctkLogStreamWithServiceRef& logStreamWithRef);
  147. ~ctkLogStreamWithServiceRef();
  148. protected:
  149. ctkServiceReference sr;
  150. };
  151. /**
  152. * \ingroup LogService
  153. */
  154. class CTK_PLUGINFW_EXPORT ctkNullLogStream : public ctkLogStream
  155. {
  156. public:
  157. ctkNullLogStream();
  158. ~ctkNullLogStream();
  159. };
  160. /**
  161. * \ingroup LogService
  162. * @{
  163. */
  164. #define CTK_DEBUG(logService) \
  165. ((logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? \
  166. ctkLogStream(logService, ctkLogService::LOG_DEBUG, 0, __FILE__, __FUNCTION__, __LINE__) : \
  167. ctkNullLogStream())
  168. #define CTK_DEBUG_EXC(logService, exc) \
  169. ((logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? \
  170. ctkLogStream(logService, ctkLogService::LOG_DEBUG, exc, __FILE__, __FUNCTION__, __LINE__) : \
  171. ctkNullLogStream())
  172. #define CTK_DEBUG_SR(logService, serviceRef) \
  173. ((logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? \
  174. static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_DEBUG, 0, __FILE__, __FUNCTION__, __LINE__)) : \
  175. static_cast<ctkLogStream>(ctkNullLogStream()))
  176. #define CTK_DEBUG_SR_EXC(logService, serviceRef, exc) \
  177. ((logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? \
  178. static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_DEBUG, exc, __FILE__, __FUNCTION__, __LINE__)) : \
  179. static_cast<ctkLogStream>(ctkNullLogStream()))
  180. #define CTK_INFO(logService) \
  181. ((logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? \
  182. ctkLogStream(logService, ctkLogService::LOG_INFO, 0, __FILE__, __FUNCTION__, __LINE__) : \
  183. ctkNullLogStream())
  184. #define CTK_INFO_EXC(logService, exc) \
  185. ((logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? \
  186. ctkLogStream(logService, ctkLogService::LOG_INFO, exc, __FILE__, __FUNCTION__, __LINE__) : \
  187. ctkNullLogStream())
  188. #define CTK_INFO_SR(logService, serviceRef) \
  189. ((logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? \
  190. static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_INFO, 0, __FILE__, __FUNCTION__, __LINE__)) : \
  191. static_cast<ctkLogStream>(ctkNullLogStream()))
  192. #define CTK_INFO_SR_EXC(logService, serviceRef, exc) \
  193. ((logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? \
  194. static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_INFO, exc, __FILE__, __FUNCTION__, __LINE__)) : \
  195. static_cast<ctkLogStream>(ctkNullLogStream()))
  196. #define CTK_WARN(logService) \
  197. ((logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? \
  198. ctkLogStream(logService, ctkLogService::LOG_WARNING, 0, __FILE__, __FUNCTION__, __LINE__) : \
  199. ctkNullLogStream())
  200. #define CTK_WARN_EXC(logService, exc) \
  201. ((logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? \
  202. ctkLogStream(logService, ctkLogService::LOG_WARNING, exc, __FILE__, __FUNCTION__, __LINE__) : \
  203. ctkNullLogStream())
  204. #define CTK_WARN_SR(logService, serviceRef) \
  205. ((logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? \
  206. static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_WARNING, 0, __FILE__, __FUNCTION__, __LINE__)) : \
  207. static_cast<ctkLogStream>(ctkNullLogStream()))
  208. #define CTK_WARN_SR_EXC(logService, serviceRef, exc) \
  209. ((logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? \
  210. static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_WARNING, exc, __FILE__, __FUNCTION__, __LINE__)) : \
  211. static_cast<ctkLogStream>(ctkNullLogStream()))
  212. #define CTK_ERROR(logService) \
  213. ((logService && logService->getLogLevel() >= ctkLogService::LOG_ERROR) ? \
  214. ctkLogStream(logService, ctkLogService::LOG_ERROR, 0, __FILE__, __FUNCTION__, __LINE__) : \
  215. ctkNullLogStream())
  216. #define CTK_ERROR_EXC(logService, exc) \
  217. ((logService && logService->getLogLevel() >= ctkLogService::LOG_ERROR) ? \
  218. ctkLogStream(logService, ctkLogService::LOG_ERROR, exc, __FILE__, __FUNCTION__, __LINE__) : \
  219. ctkNullLogStream())
  220. #define CTK_ERROR_SR(logService, serviceRef) \
  221. ((logService && logService->getLogLevel() >= ctkLogService::LOG_ERRO) ? \
  222. static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_ERROR, 0, __FILE__, __FUNCTION__, __LINE__)) : \
  223. static_cast<ctkLogStream>(ctkNullLogStream()))
  224. #define CTK_ERROR_SR_EXC(logService, serviceRef, exc) \
  225. ((logService && logService->getLogLevel() >= ctkLogService::LOG_ERROR) ? \
  226. static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_ERROR, exc, __FILE__, __FUNCTION__, __LINE__)) : \
  227. static_cast<ctkLogStream>(ctkNullLogStream()))
  228. /** @}*/
  229. #endif // CTKLOGSERVICE_H