ctkCMLogTracker.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include "ctkCMLogTracker_p.h"
  16. #include <QDateTime>
  17. ctkCMLogTracker::ctkCMLogTracker(ctkPluginContext* context, QIODevice* out)
  18. : ctkServiceTracker<ctkLogService*>(context), out(out), logLevel(std::numeric_limits<int>::max())
  19. {
  20. }
  21. int ctkCMLogTracker::getLogLevel() const
  22. {
  23. return logLevel;
  24. }
  25. ctkLogService* ctkCMLogTracker::addingService(const ctkServiceReference& reference)
  26. {
  27. ctkLogService* service = getService(reference);
  28. if (logLevel == std::numeric_limits<int>::max() ||
  29. service->getLogLevel() > logLevel)
  30. {
  31. logLevel = service->getLogLevel();
  32. }
  33. return service;
  34. }
  35. void ctkCMLogTracker::removedService(const ctkServiceReference& reference, ctkLogService* service)
  36. {
  37. logLevel = std::numeric_limits<int>::max();
  38. QList<ctkLogService*> services = getServices();
  39. foreach (ctkLogService* s, services)
  40. {
  41. if (s == service) continue;
  42. if (logLevel == std::numeric_limits<int>::max() ||
  43. s->getLogLevel() > logLevel)
  44. {
  45. logLevel = s->getLogLevel();
  46. }
  47. }
  48. ctkServiceTracker<ctkLogService*>::removedService(reference, service);
  49. }
  50. void ctkCMLogTracker::log(int level, const QString& message, const std::exception* exception,
  51. const char* file, const char* function, int line)
  52. {
  53. logToAll(ctkServiceReference(), level, message, exception, file, function, line);
  54. }
  55. void ctkCMLogTracker::log(const ctkServiceReference& reference, int level, const QString& message,
  56. const std::exception* exception, const char* file, const char* function, int line)
  57. {
  58. logToAll(reference, level, message, exception, file, function, line);
  59. }
  60. void ctkCMLogTracker::logToAll(const ctkServiceReference& reference, int level, const QString& message,
  61. const std::exception* exception, const char* file, const char* function, int line)
  62. {
  63. QMutexLocker lock(&mutex);
  64. QList<ctkServiceReference> references = getServiceReferences();
  65. if (!references.isEmpty())
  66. {
  67. foreach (ctkServiceReference ref, references)
  68. {
  69. ctkLogService* service = getService(ref);
  70. if (service != 0)
  71. {
  72. try
  73. {
  74. service->log(reference, level, message, exception, file, function, line);
  75. }
  76. catch (...)
  77. {
  78. // TODO: consider printing to System Error
  79. }
  80. }
  81. }
  82. return;
  83. }
  84. noLogService(reference, level, message, exception, file, function, line);
  85. }
  86. void ctkCMLogTracker::noLogService(const ctkServiceReference& reference, int level, const QString& message,
  87. const std::exception* exc, const char* file, const char* function, int line)
  88. {
  89. Q_UNUSED(reference)
  90. QString timestamp = QDateTime::currentDateTime().toString();
  91. out << timestamp;
  92. if (level == ctkLogService::LOG_DEBUG)
  93. {
  94. out << " Debug";
  95. }
  96. else if (level == LOG_INFO)
  97. {
  98. out << " Info";
  99. }
  100. else if (level == LOG_WARNING)
  101. {
  102. out << " Warning";
  103. }
  104. else if (level == LOG_ERROR)
  105. {
  106. out << " Error";
  107. }
  108. else
  109. {
  110. out << "[Unknown Log Level]";
  111. }
  112. out << ": " << message << '\n';
  113. if (reference)
  114. {
  115. out << reference << '\n';
  116. }
  117. if (exc)
  118. {
  119. out << exc->what() << '\n';
  120. }
  121. if (file || function)
  122. {
  123. out << "in " << function << ", " << file << ":" << line << '\n';
  124. }
  125. out.flush();
  126. }