QsLog.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2013, Razvan Petru
  2. // All rights reserved.
  3. // Redistribution and use in source and binary forms, with or without modification,
  4. // are permitted provided that the following conditions are met:
  5. // * Redistributions of source code must retain the above copyright notice, this
  6. // list of conditions and the following disclaimer.
  7. // * Redistributions in binary form must reproduce the above copyright notice, this
  8. // list of conditions and the following disclaimer in the documentation and/or other
  9. // materials provided with the distribution.
  10. // * The name of the contributors may not be used to endorse or promote products
  11. // derived from this software without specific prior written permission.
  12. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. // IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  16. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  17. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  20. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  21. // OF THE POSSIBILITY OF SUCH DAMAGE.
  22. #ifndef QSLOG_H
  23. #define QSLOG_H
  24. #include "QsLogLevel.h"
  25. #include "QsLogDest.h"
  26. #include <QDebug>
  27. #include <QString>
  28. #include <QFileInfo>
  29. #define QS_LOG_VERSION "2.0b3"
  30. namespace QsLogging
  31. {
  32. class Destination;
  33. class LoggerImpl; // d pointer
  34. typedef void (*ProcessingCallback)(QString& message);
  35. class QSLOG_SHARED_OBJECT Logger
  36. {
  37. public:
  38. static Logger& instance();
  39. static void destroyInstance();
  40. static Level levelFromLogMessage(const QString& logMessage, bool* conversionSucceeded = 0);
  41. ~Logger();
  42. //! Adds a log message destination. Don't add null destinations.
  43. void addDestination(DestinationPtr destination);
  44. //! Logging at a level < 'newLevel' will be ignored
  45. void setLoggingLevel(Level newLevel);
  46. //! The default level is INFO
  47. Level loggingLevel() const;
  48. //! Set callback for changing message contents before writing to output
  49. void setProcessingCallback(ProcessingCallback cb);
  50. //! The helper forwards the streaming to QDebug and builds the final
  51. //! log message.
  52. class QSLOG_SHARED_OBJECT Helper
  53. {
  54. public:
  55. explicit Helper(Level logLevel) :
  56. level(logLevel),
  57. qtDebug(&buffer) {}
  58. ~Helper();
  59. QDebug& stream(){ return qtDebug; }
  60. private:
  61. void writeToLog();
  62. Level level;
  63. QString buffer;
  64. QDebug qtDebug;
  65. };
  66. private:
  67. Logger();
  68. Logger(const Logger&); // not available
  69. Logger& operator=(const Logger&); // not available
  70. void enqueueWrite(QString message, Level level);
  71. void write(const QString& message, Level level);
  72. LoggerImpl* d;
  73. friend class LogWriterRunnable;
  74. };
  75. } // end namespace
  76. //! Logging macros: define QS_LOG_LINE_NUMBERS to get the file and line number
  77. //! in the log output.
  78. #ifndef QS_LOG_LINE_NUMBERS
  79. #define QLOG_TRACE() \
  80. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::TraceLevel) {} \
  81. else QsLogging::Logger::Helper(QsLogging::TraceLevel).stream()
  82. #define QLOG_DEBUG() \
  83. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::DebugLevel) {} \
  84. else QsLogging::Logger::Helper(QsLogging::DebugLevel).stream()
  85. #define QLOG_INFO() \
  86. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::InfoLevel) {} \
  87. else QsLogging::Logger::Helper(QsLogging::InfoLevel).stream()
  88. #define QLOG_WARN() \
  89. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::WarnLevel) {} \
  90. else QsLogging::Logger::Helper(QsLogging::WarnLevel).stream()
  91. #define QLOG_ERROR() \
  92. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::ErrorLevel) {} \
  93. else QsLogging::Logger::Helper(QsLogging::ErrorLevel).stream()
  94. #define QLOG_FATAL() \
  95. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::FatalLevel) {} \
  96. else QsLogging::Logger::Helper(QsLogging::FatalLevel).stream()
  97. #else
  98. #define QLOG_TRACE() \
  99. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::TraceLevel) {} \
  100. else QsLogging::Logger::Helper(QsLogging::TraceLevel).stream() << qPrintable(QFileInfo(__FILE__).fileName()) << '@' << __LINE__ << "-"
  101. #define QLOG_DEBUG() \
  102. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::DebugLevel) {} \
  103. else QsLogging::Logger::Helper(QsLogging::DebugLevel).stream() << qPrintable(QFileInfo(__FILE__).fileName()) << '@' << __LINE__ << "-"
  104. #define QLOG_INFO() \
  105. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::InfoLevel) {} \
  106. else QsLogging::Logger::Helper(QsLogging::InfoLevel).stream() << qPrintable(QFileInfo(__FILE__).fileName()) << '@' << __LINE__ << "-"
  107. #define QLOG_WARN() \
  108. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::WarnLevel) {} \
  109. else QsLogging::Logger::Helper(QsLogging::WarnLevel).stream() << qPrintable(QFileInfo(__FILE__).fileName()) << '@' << __LINE__ << "-"
  110. #define QLOG_ERROR() \
  111. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::ErrorLevel) {} \
  112. else QsLogging::Logger::Helper(QsLogging::ErrorLevel).stream() << qPrintable(QFileInfo(__FILE__).fileName()) << '@' << __LINE__ << "-"
  113. #define QLOG_FATAL() \
  114. if (QsLogging::Logger::instance().loggingLevel() > QsLogging::FatalLevel) {} \
  115. else QsLogging::Logger::Helper(QsLogging::FatalLevel).stream() << qPrintable(QFileInfo(__FILE__).fileName()) << '@' << __LINE__ << "-"
  116. #endif
  117. #ifdef QS_LOG_DISABLE
  118. #include "QsLogDisableForThisFile.h"
  119. #endif
  120. #endif // QSLOG_H