ctkEALogTracker.cpp 4.2 KB

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