ctkEALogEventAdapter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "ctkEALogEventAdapter_p.h"
  16. #include <ctkPluginContext.h>
  17. #include <ctkPluginConstants.h>
  18. #include <service/event/ctkEventConstants.h>
  19. #include <service/log/ctkLogService.h>
  20. _LogListener::_LogListener(ctkEALogEventAdapter* adapter)
  21. : adapter(adapter)
  22. {
  23. }
  24. void _LogListener::logged(ctkLogEntryPtr entry)
  25. {
  26. // This is where the assembly as specified in 133.6.6 OSGi R4
  27. // compendium is taking place (i.e., the log entry is adapted to
  28. // an event and posted via the ctkEventAdmin)
  29. ctkDictionary properties;
  30. QSharedPointer<ctkPlugin> plugin = entry->getPlugin();
  31. if (plugin)
  32. {
  33. properties.insert("plugin.id", QVariant::fromValue<long>(plugin->getPluginId()));
  34. const QString symbolicName = plugin->getSymbolicName();
  35. if (!symbolicName.isEmpty())
  36. {
  37. properties.insert(ctkEventConstants::PLUGIN_SYMBOLICNAME,
  38. symbolicName);
  39. }
  40. properties.insert("plugin", QVariant::fromValue(plugin));
  41. }
  42. properties.insert("log.level", entry->getLevel());
  43. properties.insert(ctkEventConstants::MESSAGE, entry->getMessage());
  44. properties.insert(ctkEventConstants::TIMESTAMP, entry->getTime());
  45. properties.insert("log.entry", QVariant::fromValue(entry));
  46. const std::exception* exc = entry->getException();
  47. if (exc)
  48. {
  49. //properties.insert(ctkEventConstants::EXCEPTION_CLASS,
  50. // exception.getClass().getName());
  51. const QString message(exc->what());
  52. if (!message.isEmpty())
  53. {
  54. properties.insert(ctkEventConstants::EXCEPTION_MESSAGE,
  55. message);
  56. }
  57. properties.insert(ctkEventConstants::EXCEPTION, exc);
  58. }
  59. ctkServiceReference service = entry->getServiceReference();
  60. if (service)
  61. {
  62. properties.insert(ctkEventConstants::SERVICE, QVariant::fromValue(service));
  63. QVariant id = service.getProperty(ctkEventConstants::SERVICE_ID);
  64. if (id.isValid())
  65. {
  66. properties.insert(ctkEventConstants::SERVICE_ID, id);
  67. QVariant pid = service.getProperty(ctkEventConstants::SERVICE_PID);
  68. if (pid.isValid())
  69. {
  70. properties.insert(ctkEventConstants::SERVICE_PID, pid);
  71. }
  72. QVariant objectClass = service.getProperty(ctkPluginConstants::OBJECTCLASS);
  73. if (objectClass.isValid())
  74. {
  75. properties.insert(ctkEventConstants::SERVICE_OBJECTCLASS, objectClass);
  76. }
  77. }
  78. }
  79. QString topic("org/commontk/service/log/LogEntry/");
  80. int level = entry->getLevel();
  81. if (level == ctkLogService::LOG_ERROR)
  82. {
  83. topic.append("LOG_ERROR");
  84. }
  85. else if (level == ctkLogService::LOG_WARNING)
  86. {
  87. topic.append("LOG_WARNING");
  88. }
  89. else if (level == ctkLogService::LOG_INFO)
  90. {
  91. topic.append("LOG_INFO");
  92. }
  93. else if (level == ctkLogService::LOG_DEBUG)
  94. {
  95. topic.append("LOG_DEBUG");
  96. }
  97. else
  98. {
  99. topic.append("LOG_OTHER");
  100. }
  101. try
  102. {
  103. adapter->getEventAdmin()->postEvent(ctkEvent(topic, properties));
  104. }
  105. catch(const std::logic_error&)
  106. {
  107. // This is o.k. - indicates that we are stopped.
  108. }
  109. }
  110. ctkEALogEventAdapter::ctkEALogEventAdapter(ctkPluginContext* context, ctkEventAdmin* admin)
  111. : ctkEAAbstractAdapter(admin), logListener(new _LogListener(this))
  112. {
  113. reg = context->registerService<ctkLogListener>(logListener);
  114. }
  115. ctkEALogEventAdapter::~ctkEALogEventAdapter()
  116. {
  117. delete logListener;
  118. }
  119. void ctkEALogEventAdapter::destroy(ctkPluginContext* context)
  120. {
  121. Q_UNUSED(context)
  122. reg.unregister();
  123. }