ctkLogStream.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 CTKLOGSTREAM_H
  16. #define CTKLOGSTREAM_H
  17. #include <org_commontk_log_Export.h>
  18. #include <ctkServiceReference.h>
  19. #include <QTextStream>
  20. class ctkLogService;
  21. class org_commontk_log_EXPORT ctkLogStream
  22. {
  23. public:
  24. ctkLogStream(ctkLogService* logService, int level, const std::exception* exc = 0,
  25. const char* file = 0, const char* function = 0, int line = -1);
  26. ctkLogStream(const ctkLogStream& logStream);
  27. virtual ~ctkLogStream();
  28. template<class T>
  29. ctkLogStream& operator <<(const T& t)
  30. {
  31. ts << t;
  32. return *this;
  33. }
  34. ctkLogStream& operator <<(const char* c)
  35. {
  36. ts << c;
  37. return *this;
  38. }
  39. protected:
  40. QString msg;
  41. QTextStream ts;
  42. bool logged;
  43. ctkLogService* logService;
  44. int level;
  45. const std::exception* exc;
  46. const char* file;
  47. const char* function;
  48. const int line;
  49. };
  50. class org_commontk_log_EXPORT ctkLogStreamWithServiceRef : public ctkLogStream
  51. {
  52. public:
  53. ctkLogStreamWithServiceRef(ctkLogService* logService, const ctkServiceReference& sr,
  54. int level, const std::exception* exc = 0,
  55. const char* file = 0, const char* function = 0, int line = -1);
  56. ctkLogStreamWithServiceRef(const ctkLogStreamWithServiceRef& logStreamWithRef);
  57. ~ctkLogStreamWithServiceRef();
  58. protected:
  59. ctkServiceReference sr;
  60. };
  61. class org_commontk_log_EXPORT ctkNullLogStream : public ctkLogStream
  62. {
  63. public:
  64. ctkNullLogStream();
  65. ~ctkNullLogStream();
  66. };
  67. #define CTK_DEBUG(logService) ((logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? ctkLogStream(logService, ctkLogService::LOG_DEBUG, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream())
  68. #define CTK_DEBUG_EXC(logService, exc) (logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? ctkLogStream(logService, ctkLogService::LOG_DEBUG, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  69. #define CTK_DEBUG_SR(logService, serviceRef) (logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_DEBUG, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  70. #define CTK_DEBUG_SR_EXC(logService, serviceRef, exc) (logService && logService->getLogLevel() >= ctkLogService::LOG_DEBUG) ? ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_DEBUG, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  71. #define CTK_INFO(logService) (logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? ctkLogStream(logService, ctkLogService::LOG_INFO, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  72. #define CTK_INFO_EXC(logService, exc) (logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? ctkLogStream(logService, ctkLogService::LOG_INFO, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  73. #define CTK_INFO_SR(logService, serviceRef) (logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_INFO, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  74. #define CTK_INFO_SR_EXC(logService, serviceRef, exc) (logService && logService->getLogLevel() >= ctkLogService::LOG_INFO) ? ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_INFO, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  75. #define CTK_WARN(logService) (logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? ctkLogStream(logService, ctkLogService::LOG_WARNING, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  76. #define CTK_WARN_EXC(logService, exc) (logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? ctkLogStream(logService, ctkLogService::LOG_WARNING, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  77. #define CTK_WARN_SR(logService, serviceRef) (logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_WARNING, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  78. #define CTK_WARN_SR_EXC(logService, serviceRef, exc) (logService && logService->getLogLevel() >= ctkLogService::LOG_WARNING) ? ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_WARNING, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  79. #define CTK_ERROR(logService) (logService && logService->getLogLevel() >= ctkLogService::LOG_ERROR) ? ctkLogStream(logService, ctkLogService::LOG_ERROR, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  80. #define CTK_ERROR_EXC(logService, exc) (logService && logService->getLogLevel() >= ctkLogService::LOG_ERROR) ? ctkLogStream(logService, ctkLogService::LOG_ERROR, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  81. #define CTK_ERROR_SR(logService, serviceRef) (logService && logService->getLogLevel() >= ctkLogService::LOG_ERRO) ? ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_ERROR, 0, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  82. #define CTK_ERROR_SR_EXC(logService, serviceRef, exc) (logService && logService->getLogLevel() >= ctkLogService::LOG_ERROR) ? ctkLogStreamWithServiceRef(logService, serviceRef, ctkLogService::LOG_ERROR, exc, __FILE__, __FUNCTION__, __LINE__) : ctkNullLogStream()
  83. #endif // CTKLOGSTREAM_H