ctkLogQDebug.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "ctkLogQDebug_p.h"
  16. #include <QDateTime>
  17. #include <QDebug>
  18. #include <QStringList>
  19. #include <ctkPluginConstants.h>
  20. ctkLogQDebug::ctkLogQDebug()
  21. : logLevel(ctkLogService::LOG_DEBUG)
  22. {
  23. }
  24. void ctkLogQDebug::log(int level, const QString& message, const std::exception* exception,
  25. const char* file, const char* function, int line)
  26. {
  27. Q_UNUSED(function)
  28. QString s = QDateTime::currentDateTime().toString(Qt::ISODate)
  29. .append(" - ").append(message);
  30. if (exception != 0)
  31. {
  32. s.append(" (").append(exception->what()).append(")");
  33. }
  34. if (file)
  35. {
  36. s.append(" [at ").append(file).append(":").append(QString::number(line)).append("]");
  37. }
  38. if (level == ctkLogService::LOG_WARNING)
  39. {
  40. qWarning() << s;
  41. }
  42. else if (level == ctkLogService::LOG_ERROR)
  43. {
  44. qCritical() << s;
  45. }
  46. else
  47. {
  48. qDebug() << s;
  49. }
  50. }
  51. void ctkLogQDebug::log(const ctkServiceReference& sr, int level, const QString& message,
  52. const std::exception* exception,
  53. const char* file, const char* function, int line)
  54. {
  55. Q_UNUSED(function)
  56. QString s = QDateTime::currentDateTime().toString(Qt::ISODate)
  57. .append(" - [");
  58. s.append(sr.getProperty(ctkPluginConstants::SERVICE_ID).toString());
  59. s.append(";");
  60. QStringList clazzes = sr.getProperty(ctkPluginConstants::OBJECTCLASS).toStringList();
  61. int i = 0;
  62. foreach (QString clazz, clazzes)
  63. {
  64. if (i > 0) s.append(",");
  65. s.append(clazz);
  66. }
  67. s.append(message);
  68. if(exception != 0)
  69. {
  70. s.append(" (").append(exception->what()).append(")");
  71. }
  72. if (file)
  73. {
  74. s.append(" [at ").append(file).append(":").append(QString::number(line)).append("]");
  75. }
  76. if (level == ctkLogService::LOG_WARNING)
  77. {
  78. qWarning() << s;
  79. }
  80. else if (level == ctkLogService::LOG_ERROR)
  81. {
  82. qCritical() << s;
  83. }
  84. else
  85. {
  86. qDebug() << s;
  87. }
  88. }
  89. int ctkLogQDebug::getLogLevel() const
  90. {
  91. return logLevel;
  92. }