initialisationhelper.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: initialisationhelper.cpp
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * changes Feb 2009, Martin Heinrich
  10. * - Fixed VS 2008 unreferenced formal parameter warning by using
  11. * Q_UNUSED in operator<<.
  12. *
  13. *
  14. * Copyright 2007 - 2009 Martin Heinrich
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. ******************************************************************************/
  29. /******************************************************************************
  30. * Dependencies
  31. ******************************************************************************/
  32. #include "log4qt/helpers/initialisationhelper.h"
  33. #include <QtCore/QCoreApplication>
  34. #include <QtCore/QDebug>
  35. #include <QtCore/QMutex>
  36. #include <QtCore/QProcess>
  37. #include <QtCore/QSettings>
  38. #ifndef QT_NO_DATASTREAM
  39. #include <QtCore/QDataStream>
  40. #endif
  41. #include "log4qt/helpers/datetime.h"
  42. #include "log4qt/helpers/logerror.h"
  43. #include "log4qt/loggingevent.h"
  44. namespace Log4Qt
  45. {
  46. /**************************************************************************
  47. *Declarations
  48. **************************************************************************/
  49. /**************************************************************************
  50. * C helper functions
  51. **************************************************************************/
  52. /**************************************************************************
  53. * Class implementation: InitialisationHelper
  54. **************************************************************************/
  55. InitialisationHelper::InitialisationHelper() :
  56. mStartTime(DateTime::currentDateTime().toMilliSeconds()),
  57. mEnvironmentSettings()
  58. {
  59. doRegisterTypes();
  60. doInitialiseEnvironmentSettings();
  61. }
  62. InitialisationHelper::~InitialisationHelper()
  63. {
  64. Q_ASSERT_X(false, "InitialisationHelper::~InitialisationHelper()", "Unexpected destruction of singleton object");
  65. }
  66. LOG4QT_IMPLEMENT_INSTANCE(InitialisationHelper)
  67. void InitialisationHelper::doInitialiseEnvironmentSettings()
  68. {
  69. // Is Process::systemEnvironment() safe to be used before a QCoreApplication
  70. // object has been created?
  71. QStringList setting_keys;
  72. setting_keys << QLatin1String("Debug");
  73. setting_keys << QLatin1String("DefaultInitOverride");
  74. setting_keys << QLatin1String("Configuration");
  75. setting_keys << QLatin1String("ConfiguratorClass");
  76. QHash<QString, QString> env_keys;
  77. QString entry;
  78. Q_FOREACH(entry, setting_keys)
  79. env_keys.insert(QString::fromLatin1("log4qt_").append(entry).toUpper(), entry);
  80. QStringList sys_env = QProcess::systemEnvironment();
  81. Q_FOREACH(entry, sys_env)
  82. {
  83. int i = entry.indexOf(QLatin1Char('='));
  84. if (i == -1)
  85. continue;
  86. QString key = entry.left(i);
  87. QString value = entry.mid(i + 1).trimmed();
  88. if (env_keys.contains(key))
  89. mEnvironmentSettings.insert(env_keys.value(key), value);
  90. }
  91. }
  92. void InitialisationHelper::doRegisterTypes()
  93. {
  94. qRegisterMetaType<Log4Qt::LogError>("Log4Qt::LogError");
  95. qRegisterMetaType<Log4Qt::Level>("Log4Qt::Level");
  96. qRegisterMetaType<Log4Qt::LoggingEvent>("Log4Qt::LoggingEvent");
  97. #ifndef QT_NO_DATASTREAM
  98. qRegisterMetaTypeStreamOperators<Log4Qt::LogError>("Log4Qt::LogError");
  99. qRegisterMetaTypeStreamOperators<Log4Qt::Level>("Log4Qt::Level");
  100. qRegisterMetaTypeStreamOperators<LoggingEvent>("Log4Qt::LoggingEvent");
  101. #endif
  102. }
  103. QString InitialisationHelper::doSetting(const QString &rKey,
  104. const QString &rDefault) const
  105. {
  106. if (mEnvironmentSettings.contains(rKey))
  107. return mEnvironmentSettings.value(rKey);
  108. if (QCoreApplication::instance())
  109. {
  110. QSettings s;
  111. s.beginGroup(QLatin1String("Log4Qt"));
  112. return s.value(rKey, rDefault).toString().trimmed();
  113. }
  114. else
  115. return rDefault;
  116. }
  117. bool InitialisationHelper::staticInitialisation()
  118. {
  119. instance();
  120. return true;
  121. }
  122. bool InitialisationHelper::msStaticInitialisation = staticInitialisation();
  123. /**************************************************************************
  124. * Implementation: Operators, Helper
  125. **************************************************************************/
  126. #ifndef QT_NO_DEBUG_STREAM
  127. QDebug operator<<(QDebug debug,
  128. const InitialisationHelper &rInitialisationHelper)
  129. {
  130. Q_UNUSED(rInitialisationHelper);
  131. debug.nospace() << "InitialisationHelper("
  132. << "starttime:" << InitialisationHelper::startTime()
  133. << "(" << DateTime::fromMilliSeconds(InitialisationHelper::startTime()) << ")"
  134. << "environmentsettings:" << InitialisationHelper::environmentSettings()
  135. << ")";
  136. return debug.space();
  137. }
  138. #endif // QT_NO_DEBUG_STREAM
  139. } // namespace Log4Qt