configuratorhelper.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: configuratorhelper.cpp
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * Copyright 2007 Martin Heinrich
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************/
  24. /******************************************************************************
  25. * Dependencies
  26. ******************************************************************************/
  27. #include "log4qt/helpers/configuratorhelper.h"
  28. #include <QtCore/QDebug>
  29. #include <QtCore/QFileSystemWatcher>
  30. #include "log4qt/helpers/initialisationhelper.h"
  31. namespace Log4Qt
  32. {
  33. /**************************************************************************
  34. * Declarations
  35. **************************************************************************/
  36. /**************************************************************************
  37. * C helper functions
  38. **************************************************************************/
  39. /**************************************************************************
  40. * Class implementation: ConfiguratorHelper
  41. **************************************************************************/
  42. ConfiguratorHelper::ConfiguratorHelper() :
  43. mObjectGuard(),
  44. mConfigurationFile(),
  45. mpConfigureFunc(0),
  46. mpConfigurationFileWatch(0),
  47. mConfigureError()
  48. {
  49. }
  50. ConfiguratorHelper::~ConfiguratorHelper()
  51. {
  52. delete mpConfigurationFileWatch;
  53. }
  54. LOG4QT_IMPLEMENT_INSTANCE(ConfiguratorHelper)
  55. void ConfiguratorHelper::doConfigurationFileChanged(const QString &rFileName)
  56. {
  57. QMutexLocker locker(&mObjectGuard);
  58. if (!mpConfigureFunc)
  59. return;
  60. mpConfigureFunc(rFileName);
  61. // Shall we hold the lock while emitting the signal?
  62. emit configurationFileChanged(rFileName, mConfigureError.count() > 0);
  63. }
  64. void ConfiguratorHelper::doSetConfigurationFile(const QString &rFileName,
  65. ConfigureFunc pConfigureFunc)
  66. {
  67. QMutexLocker locker(&mObjectGuard);
  68. mConfigurationFile.clear();
  69. mpConfigureFunc = 0;
  70. delete mpConfigurationFileWatch;
  71. if (rFileName.isEmpty())
  72. return;
  73. mConfigurationFile = rFileName;
  74. mpConfigureFunc = pConfigureFunc;
  75. mpConfigurationFileWatch = new QFileSystemWatcher();
  76. mpConfigurationFileWatch->addPath(rFileName);
  77. connect(mpConfigurationFileWatch,
  78. SIGNAL(fileChanged(const QString &)),
  79. SLOT(configurationFileChanged(const QString &)));
  80. }
  81. /**************************************************************************
  82. * Implementation: Operators, Helper
  83. **************************************************************************/
  84. #ifndef QT_NO_DEBUG_STREAM
  85. QDebug operator<<(QDebug debug,
  86. const ConfiguratorHelper &rConfiguratorHelper)
  87. {
  88. debug.nospace() << "ConfiguratorHelper("
  89. << "configurationfile:" << ConfiguratorHelper::configurationFile()
  90. << "configurefunc:" << rConfiguratorHelper.mpConfigureFunc
  91. << "filesystemwatcher:" << rConfiguratorHelper.mpConfigurationFileWatch
  92. << ")";
  93. return debug.space();
  94. }
  95. #endif // QT_NO_DEBUG_STREAM
  96. } // namespace Log4Qt