Mercurial > public > ostc_companion
comparison Utils/LogAppender.cpp @ 1:0b3630a29ad8
Initial version based on previous repository.
Project was ported to QT6 and in now cmake based.
| author | Ideenmodellierer <tiefenrauscher@web.de> |
|---|---|
| date | Thu, 27 Nov 2025 18:40:28 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:76ccd6ce50c0 | 1:0b3630a29ad8 |
|---|---|
| 1 ////////////////////////////////////////////////////////////////////////////// | |
| 2 /// \file LogAppender.cpp | |
| 3 /// \brief Put Log message somewhere. | |
| 4 /// \author JD Gascuel. | |
| 5 /// \copyright (c) 2011-2016 JD Gascuel. All rights reserved. | |
| 6 /// $Id$ | |
| 7 ////////////////////////////////////////////////////////////////////////////// | |
| 8 // | |
| 9 // BSD 2-Clause License: | |
| 10 // | |
| 11 // Redistribution and use in source and binary forms, with or without | |
| 12 // modification, are permitted provided that the following conditions | |
| 13 // are met: | |
| 14 // | |
| 15 // 1. Redistributions of source code must retain the above copyright notice, | |
| 16 // this list of conditions and the following disclaimer. | |
| 17 // | |
| 18 // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 19 // this list of conditions and the following disclaimer in the documentation | |
| 20 // and/or other materials provided with the distribution. | |
| 21 // | |
| 22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 23 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 24 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 25 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 26 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 27 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 28 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 29 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 30 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 31 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 32 // THE POSSIBILITY OF SUCH DAMAGE. | |
| 33 // | |
| 34 ////////////////////////////////////////////////////////////////////////////// | |
| 35 | |
| 36 #include "Utils/LogAppender.h" | |
| 37 | |
| 38 #include <QDateTime> | |
| 39 #include <QDir> | |
| 40 #include <QSettings> | |
| 41 | |
| 42 ////////////////////////////////////////////////////////////////////////////// | |
| 43 /// List of all currently defined appenders | |
| 44 /// NOTE: Use singleton to avoid messing-up with DLL on windows. | |
| 45 | |
| 46 QList<LogAppender *>& LogAppender::list() | |
| 47 { | |
| 48 static QList<LogAppender *> appenderList; | |
| 49 return appenderList; | |
| 50 } | |
| 51 | |
| 52 ////////////////////////////////////////////////////////////////////////////// | |
| 53 // Add new instance to the list. | |
| 54 // TODO: Lock for threads-safe ? | |
| 55 LogAppender::LogAppender(int, char *[]) | |
| 56 : _minLevel(Log::LEVEL_INFO) | |
| 57 { | |
| 58 //---- Manage list of all appenders. | |
| 59 list().push_back(this); | |
| 60 } | |
| 61 | |
| 62 ////////////////////////////////////////////////////////////////////////////// | |
| 63 // Remove defunct instance from the list. | |
| 64 // TODO: Lock for threads-safe ? | |
| 65 LogAppender::~LogAppender() { | |
| 66 list().removeAll(this); | |
| 67 } | |
| 68 | |
| 69 void LogAppender::setMinLevel(Log::Level level) | |
| 70 { | |
| 71 _minLevel = level; | |
| 72 if( level < Log::minLevel ) | |
| 73 Log::minLevel = level; | |
| 74 } | |
| 75 | |
| 76 void LogAppender::setFormat(const char *format) | |
| 77 { | |
| 78 _format = format; | |
| 79 } | |
| 80 | |
| 81 QString LogAppender::format(const Log &log) const | |
| 82 { | |
| 83 QString line = _format.isEmpty() | |
| 84 ? QString::fromUtf8(defaultFormat()) | |
| 85 : QString::fromUtf8(_format); | |
| 86 | |
| 87 line.replace("%d", QDate::currentDate().toString("yyyy/MM/dd").toLatin1()); | |
| 88 line.replace("%t", QTime::currentTime().toString("hh:mm:ss.zzz").toLatin1()); | |
| 89 line.replace("%o", (log.level == Log::LEVEL_TRACE ) ? "TRACE " : | |
| 90 (log.level == Log::LEVEL_DEBUG ) ? "DEBUG " : | |
| 91 (log.level == Log::LEVEL_INFO ) ? "INFO " : | |
| 92 (log.level == Log::LEVEL_WARNING) ? "WARNING" : | |
| 93 (log.level == Log::LEVEL_THROW ) ? "THROW " : | |
| 94 (log.level == Log::LEVEL_ERROR ) ? "ERROR " : "-------"); | |
| 95 line.replace("%l", QString::number(log.line).toLatin1()); | |
| 96 line.replace("%f", log.file); | |
| 97 line.replace("%F", log.file.section('/', -1)); | |
| 98 line.replace("%p", log.function); | |
| 99 line.replace("%m", log.message); | |
| 100 | |
| 101 return line; | |
| 102 } | |
| 103 | |
| 104 ////////////////////////////////////////////////////////////////////////////// | |
| 105 // Send to all instanciated appenders. | |
| 106 // TODO: Lock for threads-safe ? | |
| 107 void LogAppender::all(const Log& log) | |
| 108 { | |
| 109 foreach(LogAppender* i, list()) | |
| 110 { | |
| 111 if( log.level >= i->_minLevel ) | |
| 112 (*i)(log); | |
| 113 } | |
| 114 } |
