|
| 1 | +// Copyright (c) 2013, Razvan Petru |
| 2 | +// All rights reserved. |
| 3 | + |
| 4 | +// Redistribution and use in source and binary forms, with or without modification, |
| 5 | +// are permitted provided that the following conditions are met: |
| 6 | + |
| 7 | +// * Redistributions of source code must retain the above copyright notice, this |
| 8 | +// list of conditions and the following disclaimer. |
| 9 | +// * Redistributions in binary form must reproduce the above copyright notice, this |
| 10 | +// list of conditions and the following disclaimer in the documentation and/or other |
| 11 | +// materials provided with the distribution. |
| 12 | +// * The name of the contributors may not be used to endorse or promote products |
| 13 | +// derived from this software without specific prior written permission. |
| 14 | + |
| 15 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | +// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 19 | +// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 20 | +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 22 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 23 | +// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 24 | +// OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + |
| 26 | +#include "QsLog.h" |
| 27 | +#include "QsLogDest.h" |
| 28 | +#ifdef QS_LOG_SEPARATE_THREAD |
| 29 | +#include <QThreadPool> |
| 30 | +#include <QRunnable> |
| 31 | +#else |
| 32 | +#include <QMutex> |
| 33 | +#endif |
| 34 | +#include <QVector> |
| 35 | +#include <QDateTime> |
| 36 | +#include <QtGlobal> |
| 37 | +#include <cassert> |
| 38 | +#include <cstdlib> |
| 39 | +#include <stdexcept> |
| 40 | + |
| 41 | +namespace QsLogging |
| 42 | +{ |
| 43 | +typedef QVector<DestinationPtr> DestinationList; |
| 44 | + |
| 45 | +static const char TraceString[] = "TRACE"; |
| 46 | +static const char DebugString[] = "DEBUG"; |
| 47 | +static const char InfoString[] = "INFO"; |
| 48 | +static const char WarnString[] = "WARN"; |
| 49 | +static const char ErrorString[] = "ERROR"; |
| 50 | +static const char FatalString[] = "FATAL"; |
| 51 | + |
| 52 | +// not using Qt::ISODate because we need the milliseconds too |
| 53 | +static const QString fmtDateTime("yyyy-MM-ddThh:mm:ss.zzz"); |
| 54 | + |
| 55 | +static const char* LevelToText(Level theLevel) |
| 56 | +{ |
| 57 | + switch (theLevel) { |
| 58 | + case TraceLevel: |
| 59 | + return TraceString; |
| 60 | + case DebugLevel: |
| 61 | + return DebugString; |
| 62 | + case InfoLevel: |
| 63 | + return InfoString; |
| 64 | + case WarnLevel: |
| 65 | + return WarnString; |
| 66 | + case ErrorLevel: |
| 67 | + return ErrorString; |
| 68 | + case FatalLevel: |
| 69 | + return FatalString; |
| 70 | + case OffLevel: |
| 71 | + return ""; |
| 72 | + default: { |
| 73 | + assert(!"bad log level"); |
| 74 | + return InfoString; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#ifdef QS_LOG_SEPARATE_THREAD |
| 80 | +class LogWriterRunnable : public QRunnable |
| 81 | +{ |
| 82 | +public: |
| 83 | + LogWriterRunnable(const QString &message, Level level) |
| 84 | + : mMessage(message) |
| 85 | + , mLevel(level) {} |
| 86 | + |
| 87 | + virtual void run() |
| 88 | + { |
| 89 | + Logger::instance().write(mMessage, mLevel); |
| 90 | + } |
| 91 | + |
| 92 | +private: |
| 93 | + QString mMessage; |
| 94 | + Level mLevel; |
| 95 | +}; |
| 96 | +#endif |
| 97 | + |
| 98 | +class LoggerImpl |
| 99 | +{ |
| 100 | +public: |
| 101 | + LoggerImpl() : |
| 102 | + level(InfoLevel) |
| 103 | + { |
| 104 | + // assume at least file + console |
| 105 | + destList.reserve(2); |
| 106 | +#ifdef QS_LOG_SEPARATE_THREAD |
| 107 | + threadPool.setMaxThreadCount(1); |
| 108 | + threadPool.setExpiryTimeout(-1); |
| 109 | +#endif |
| 110 | + } |
| 111 | +#ifdef QS_LOG_SEPARATE_THREAD |
| 112 | + QThreadPool threadPool; |
| 113 | +#else |
| 114 | + QMutex logMutex; |
| 115 | +#endif |
| 116 | + Level level; |
| 117 | + DestinationList destList; |
| 118 | +}; |
| 119 | + |
| 120 | +Logger::Logger() : |
| 121 | + d(new LoggerImpl) |
| 122 | +{ |
| 123 | +} |
| 124 | + |
| 125 | +Logger::~Logger() |
| 126 | +{ |
| 127 | + delete d; |
| 128 | +} |
| 129 | + |
| 130 | +void Logger::addDestination(DestinationPtr destination) |
| 131 | +{ |
| 132 | + assert(destination.data()); |
| 133 | + d->destList.push_back(destination); |
| 134 | +} |
| 135 | + |
| 136 | +void Logger::setLoggingLevel(Level newLevel) |
| 137 | +{ |
| 138 | + d->level = newLevel; |
| 139 | +} |
| 140 | + |
| 141 | +Level Logger::loggingLevel() const |
| 142 | +{ |
| 143 | + return d->level; |
| 144 | +} |
| 145 | + |
| 146 | +//! creates the complete log message and passes it to the logger |
| 147 | +void Logger::Helper::writeToLog() |
| 148 | +{ |
| 149 | + const char* const levelName = LevelToText(level); |
| 150 | + const QString completeMessage(QString("%1 %2 %3") |
| 151 | + .arg(levelName, 5) |
| 152 | + .arg(QDateTime::currentDateTime().toString(fmtDateTime)) |
| 153 | + .arg(buffer) |
| 154 | + ); |
| 155 | + |
| 156 | + Logger::instance().enqueueWrite(completeMessage, level); |
| 157 | +} |
| 158 | + |
| 159 | +Logger::Helper::~Helper() |
| 160 | +{ |
| 161 | + try { |
| 162 | + writeToLog(); |
| 163 | + } |
| 164 | + catch(std::exception&) { |
| 165 | + // you shouldn't throw exceptions from a sink |
| 166 | + assert(!"exception in logger helper destructor"); |
| 167 | + throw; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +//! directs the message to the task queue or writes it directly |
| 172 | +void Logger::enqueueWrite(const QString& message, Level level) |
| 173 | +{ |
| 174 | +#ifdef QS_LOG_SEPARATE_THREAD |
| 175 | + LogWriterRunnable *r = new LogWriterRunnable(message, level); |
| 176 | + d->threadPool.start(r); |
| 177 | +#else |
| 178 | + QMutexLocker lock(&d->logMutex); |
| 179 | + write(message, level); |
| 180 | +#endif |
| 181 | +} |
| 182 | + |
| 183 | +//! Sends the message to all the destinations. The level for this message is passed in case |
| 184 | +//! it's useful for processing in the destination. |
| 185 | +void Logger::write(const QString& message, Level level) |
| 186 | +{ |
| 187 | + for (DestinationList::iterator it = d->destList.begin(), |
| 188 | + endIt = d->destList.end();it != endIt;++it) { |
| 189 | + (*it)->write(message, level); |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +} // end namespace |
0 commit comments