ctkFileLogger.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QFile>
  16. #include <QTextStream>
  17. // CTK includes
  18. #include "ctkFileLogger.h"
  19. // --------------------------------------------------------------------------
  20. // ctkFileLoggerPrivate
  21. // --------------------------------------------------------------------------
  22. class ctkFileLoggerPrivate
  23. {
  24. Q_DECLARE_PUBLIC(ctkFileLogger);
  25. protected:
  26. ctkFileLogger* const q_ptr;
  27. public:
  28. ctkFileLoggerPrivate(ctkFileLogger& object);
  29. ~ctkFileLoggerPrivate();
  30. void init();
  31. bool Enabled;
  32. QString FilePath;
  33. // int NumberOfFilesToKeep;
  34. };
  35. // --------------------------------------------------------------------------
  36. ctkFileLoggerPrivate::ctkFileLoggerPrivate(ctkFileLogger& object)
  37. : q_ptr(&object)
  38. {
  39. this->Enabled = true;
  40. // this->NumberOfFilesToKeep = 10;
  41. }
  42. // --------------------------------------------------------------------------
  43. ctkFileLoggerPrivate::~ctkFileLoggerPrivate()
  44. {
  45. }
  46. // --------------------------------------------------------------------------
  47. void ctkFileLoggerPrivate::init()
  48. {
  49. }
  50. // --------------------------------------------------------------------------
  51. // ctkFileLogger
  52. // --------------------------------------------------------------------------
  53. ctkFileLogger::ctkFileLogger(QObject* parentObject)
  54. : Superclass(parentObject)
  55. , d_ptr(new ctkFileLoggerPrivate(*this))
  56. {
  57. Q_D(ctkFileLogger);
  58. d->init();
  59. }
  60. // --------------------------------------------------------------------------
  61. ctkFileLogger::~ctkFileLogger()
  62. {
  63. }
  64. // --------------------------------------------------------------------------
  65. bool ctkFileLogger::enabled()const
  66. {
  67. Q_D(const ctkFileLogger);
  68. return d->Enabled;
  69. }
  70. // --------------------------------------------------------------------------
  71. void ctkFileLogger::setEnabled(bool value)
  72. {
  73. Q_D(ctkFileLogger);
  74. d->Enabled = value;
  75. }
  76. // --------------------------------------------------------------------------
  77. QString ctkFileLogger::filePath()const
  78. {
  79. Q_D(const ctkFileLogger);
  80. return d->FilePath;
  81. }
  82. // --------------------------------------------------------------------------
  83. void ctkFileLogger::setFilePath(const QString& filePath)
  84. {
  85. Q_D(ctkFileLogger);
  86. d->FilePath = filePath;
  87. }
  88. //// --------------------------------------------------------------------------
  89. //int ctkFileLogger::numberOfFilesToKeep()const
  90. //{
  91. // Q_D(const ctkFileLogger);
  92. // return d->NumberOfFilesToKeep;
  93. //}
  94. //// --------------------------------------------------------------------------
  95. //void ctkFileLogger::setNumberOfFilesToKeep(int value)
  96. //{
  97. // Q_D(ctkFileLogger);
  98. // d->NumberOfFilesToKeep = value;
  99. //}
  100. // --------------------------------------------------------------------------
  101. void ctkFileLogger::logMessage(const QString& msg)
  102. {
  103. Q_D(ctkFileLogger);
  104. if (!d->Enabled)
  105. {
  106. return;
  107. }
  108. QFile f(d->FilePath);
  109. if (!f.open(QFile::Append))
  110. {
  111. return;
  112. }
  113. QTextStream s(&f);
  114. s << msg << "\n";
  115. f.close();
  116. }