ctkLogService.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * Provides methods for plugins to write messages to the log.
  23. *
  24. * <p>
  25. * <code>ctkLogService</code> methods are provided to log messages; optionally with a
  26. * <code>ctkServiceReference</code> object or an exception.
  27. *
  28. * <p>
  29. * Plugins must log messages in the Plugin Framework with a severity level
  30. * according to the following hierarchy:
  31. * <ol>
  32. * <li>{@link #LOG_ERROR}
  33. * <li>{@link #LOG_WARNING}
  34. * <li>{@link #LOG_INFO}
  35. * <li>{@link #LOG_DEBUG}
  36. * </ol>
  37. *
  38. * @ThreadSafe
  39. */
  40. struct CTK_PLUGINFW_EXPORT ctkLogService
  41. {
  42. virtual ~ctkLogService() {}
  43. /**
  44. * An error message (Value 1).
  45. *
  46. * <p>
  47. * This log entry indicates the plugin or service may not be functional.
  48. */
  49. static const int LOG_ERROR; // = 1;
  50. /**
  51. * A warning message (Value 2).
  52. *
  53. * <p>
  54. * This log entry indicates a plugin or service is still functioning but may
  55. * experience problems in the future because of the warning condition.
  56. */
  57. static const int LOG_WARNING; // = 2;
  58. /**
  59. * An informational message (Value 3).
  60. *
  61. * <p>
  62. * This log entry may be the result of any change in the plugin or service
  63. * and does not indicate a problem.
  64. */
  65. static const int LOG_INFO; // = 3;
  66. /**
  67. * A debugging message (Value 4).
  68. *
  69. * <p>
  70. * This log entry is used for problem determination and may be irrelevant to
  71. * anyone but the plugin developer.
  72. */
  73. static const int LOG_DEBUG; // = 4;
  74. /**
  75. * Logs a message.
  76. *
  77. * <p>
  78. * The <code>ctkServiceReference</code> field and the <code>Exception</code> field
  79. * of the <code>ctkLogEntry</code> object will be set to <code>null</code>.
  80. *
  81. * @param level The severity of the message. This should be one of the
  82. * defined log levels but may be any integer that is interpreted in a
  83. * user defined way.
  84. * @param message Human readable string describing the condition.
  85. * @param exception The exception that reflects the condition or <code>null</code>.
  86. * @see #LOG_ERROR
  87. * @see #LOG_WARNING
  88. * @see #LOG_INFO
  89. * @see #LOG_DEBUG
  90. */
  91. virtual void log(int level, const QString& message, const std::exception* exception = 0,
  92. const char* file = 0, const char* function = 0, int line = -1) = 0;
  93. /**
  94. * Logs a message associated with a specific <code>ctkServiceReference</code>
  95. * object.
  96. *
  97. * <p>
  98. * The <code>Exception</code> field of the <code>ctkLogEntry</code> will be set to
  99. * <code>null</code>.
  100. *
  101. * @param sr The <code>ctkServiceReference</code> object of the service that this
  102. * message is associated with.
  103. * @param level The severity of the message. This should be one of the
  104. * defined log levels but may be any integer that is interpreted in a
  105. * user defined way.
  106. * @param message Human readable string describing the condition.
  107. * @param exception The exception that reflects the condition or
  108. * <code>null</code>.
  109. *
  110. * @see #LOG_ERROR
  111. * @see #LOG_WARNING
  112. * @see #LOG_INFO
  113. * @see #LOG_DEBUG
  114. */
  115. virtual void log(const ctkServiceReference& sr, int level, const QString& message,
  116. const std::exception* exception = 0,
  117. const char* file = 0, const char* function = 0, int line = -1) = 0;
  118. /**
  119. * Get the current log level. The log service discards log entries with a
  120. * level that is less severe than the current level. E.g. if the current
  121. * log level is LOG_WARNING then the log service will discard all log entries with
  122. * level LOG_INFO and LOG_DEBUG.
  123. *
  124. * @return The lowest severity level that is accepted into the log.
  125. */
  126. virtual int getLogLevel() const = 0;
  127. };
  128. Q_DECLARE_INTERFACE(ctkLogService, "org.commontk.service.log.LogService")
  129. class CTK_PLUGINFW_EXPORT ctkLogStreamWithServiceRef : public ctkLogStream
  130. {
  131. public:
  132. ctkLogStreamWithServiceRef(ctkLogService* logService, const ctkServiceReference& sr,
  133. int level, const std::exception* exc = 0,
  134. const char* file = 0, const char* function = 0, int line = -1);
  135. ctkLogStreamWithServiceRef(const ctkLogStreamWithServiceRef& logStreamWithRef);
  136. ~ctkLogStreamWithServiceRef();
  137. protected:
  138. ctkServiceReference sr;
  139. };
  140. class CTK_PLUGINFW_EXPORT ctkNullLogStream : public ctkLogStream
  141. {
  142. public:
  143. ctkNullLogStream();
  144. ~ctkNullLogStream();
  145. };
  146. #define CTK_DEBUG(logService) ((logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? ctkLogStream(logService, ctkLogService::LOG_DEBUG, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream())
  147. #define CTK_DEBUG_EXC(logService, exc) ((logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? ctkLogStream(logService, ctkLogService::LOG_DEBUG, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream())
  148. #define CTK_DEBUG_SR(logService, serviceRef) ((logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_DEBUG, 0, __FILE__, __FUNCTION__, __LINE__)) : static_cast<ctkLogStream>(ctkNullLogStream()))
  149. #define CTK_DEBUG_SR_EXC(logService, serviceRef, exc) ((logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_DEBUG, exc, __FILE__, __FUNCTION__, __LINE__)) : static_cast<ctkLogStream>(ctkNullLogStream()))
  150. #define CTK_INFO(logService) ((logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? ctkLogStream(logService, ctkLogService::LOG_INFO, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream())
  151. #define CTK_INFO_EXC(logService, exc) ((logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? ctkLogStream(logService, ctkLogService::LOG_INFO, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream())
  152. #define CTK_INFO_SR(logService, serviceRef) ((logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_INFO, 0, __FILE__, __FUNCTION__, __LINE__)) : static_cast<ctkLogStream>(ctkNullLogStream()))
  153. #define CTK_INFO_SR_EXC(logService, serviceRef, exc) ((logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_INFO, exc, __FILE__, __FUNCTION__, __LINE__)) : static_cast<ctkLogStream>(ctkNullLogStream()))
  154. #define CTK_WARN(logService) ((logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? ctkLogStream(logService, ctkLogService::LOG_WARNING, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream())
  155. #define CTK_WARN_EXC(logService, exc) ((logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? ctkLogStream(logService, ctkLogService::LOG_WARNING, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream())
  156. #define CTK_WARN_SR(logService, serviceRef) ((logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_WARNING, 0, __FILE__, __FUNCTION__, __LINE__)) : static_cast<ctkLogStream>(ctkNullLogStream()))
  157. #define CTK_WARN_SR_EXC(logService, serviceRef, exc) ((logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_WARNING, exc, __FILE__, __FUNCTION__, __LINE__)) : static_cast<ctkLogStream>(ctkNullLogStream()))
  158. #define CTK_ERROR(logService) ((logService && logService->getLogLevel() >= ctkLogService::LOG_ERROR) ? ctkLogStream(logService, ctkLogService::LOG_ERROR, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream())
  159. #define CTK_ERROR_EXC(logService, exc) ((logService && logService->getLogLevel() >= ctkLogService::LOG_ERROR) ? ctkLogStream(logService, ctkLogService::LOG_ERROR, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream())
  160. #define CTK_ERROR_SR(logService, serviceRef) ((logService && logService->getLogLevel() >= ctkLogService::LOG_ERRO) ? static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_ERROR, 0, __FILE__, __FUNCTION__, __LINE__)) : static_cast<ctkLogStream>(ctkNullLogStream()))
  161. #define CTK_ERROR_SR_EXC(logService, serviceRef, exc) ((logService && logService->getLogLevel() >= ctkLogService::LOG_ERROR) ? static_cast<ctkLogStream>(ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_ERROR, exc, __FILE__, __FUNCTION__, __LINE__)) : static_cast<ctkLogStream>(ctkNullLogStream()))
  162. #endif // CTKLOGSERVICE_H