Mercurial > public > ostc_companion
comparison Utils/LogAppender.h @ 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.h | |
| 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 // HISTORY | |
| 36 // 2013-11-15 jDG: Creation. | |
| 37 // 2016-05-24 jDG: BSD-2 version. | |
| 38 | |
| 39 #include "Utils/Log.h" | |
| 40 | |
| 41 #ifndef LOGAPPENDER_H | |
| 42 #define LOGAPPENDER_H | |
| 43 | |
| 44 #include <QByteArray> | |
| 45 #include <QString> | |
| 46 | |
| 47 #include <QList> | |
| 48 | |
| 49 class LogFilter; | |
| 50 | |
| 51 ////////////////////////////////////////////////////////////////////////////// | |
| 52 | |
| 53 class EXPORT LogAppender | |
| 54 { | |
| 55 /// Send a message to this appender. | |
| 56 virtual void operator()(const Log& log) = 0; | |
| 57 | |
| 58 /// Log::close() needs an access to the list, to remove all appenders. | |
| 59 friend void Log::close(); | |
| 60 | |
| 61 /// Manage a list of all existing appenders. | |
| 62 static QList<LogAppender *> &list(); | |
| 63 | |
| 64 protected: | |
| 65 /// How to dislay logged data ? | |
| 66 QByteArray _format; | |
| 67 Log::Level _minLevel; | |
| 68 | |
| 69 /// Default formating string for that appender. | |
| 70 virtual const char* type() const = 0; | |
| 71 virtual const char* defaultFormat() const = 0; | |
| 72 virtual Log::Level defaultMinLevel() const = 0; | |
| 73 | |
| 74 /// Do the substtutions | |
| 75 /// Use the string set by setFormat() to format the \p log event. | |
| 76 QString format(const Log& log) const; | |
| 77 | |
| 78 public: | |
| 79 LogAppender(int argc, char *argv[]); | |
| 80 virtual ~LogAppender(); | |
| 81 | |
| 82 //------------------------------------------------------------------------ | |
| 83 /// Filter messages by error level. | |
| 84 /// Sets the minimum level to use for this appender. | |
| 85 /// \p level is one of: | |
| 86 /// * Log::LEVEL_DEBUG : The most verbose one, used only during debug session. | |
| 87 /// * Log::LEVEL_TRACE : Messages to be put in silent log files, for post-mortem analysis. | |
| 88 /// * Log::LEVEL_INFO : General information send to the final user. | |
| 89 /// * Log::LEVEL_WARNING : Error reported to the user (eg. with a Qt warning box. \sa LogAppenderWindow). | |
| 90 /// * Log::LEVEL_THROW : Error that is mean to be catched, and corrected automatically. | |
| 91 /// * Log::LEVEL_ERROR : Fatal error that cannot be recovered. The program should halt. | |
| 92 /// | |
| 93 /// The default depends from the appender. | |
| 94 void setMinLevel(Log::Level level); | |
| 95 | |
| 96 //------------------------------------------------------------------------ | |
| 97 /// Set current formating. | |
| 98 /// | |
| 99 /// How to format log line once appended. | |
| 100 /// Default depends of the appender used (\sa defaultFormat()). | |
| 101 /// Setting a null string returns to the appender's default. | |
| 102 /// | |
| 103 /// Valide substitutions are: | |
| 104 /// * %o : log level (one of DEBUG, TRACE, INFO, THROW, WARNING, ERROR). | |
| 105 /// * %d : log's date. | |
| 106 /// * %t : log's time. | |
| 107 /// * %f : source file where the error is logged (just trailling filename) | |
| 108 /// * %F : source file where the error is logged (full path) | |
| 109 /// * %l : source line where the error is logged. | |
| 110 /// * %p : the function that fired the log message. | |
| 111 /// * %m : the log message itself. | |
| 112 /// | |
| 113 /// Typical usage: | |
| 114 /// \code | |
| 115 /// LogAppenderMemory mem(0,0); | |
| 116 /// mem.setFormat("[%o] %f:%l %m"); | |
| 117 /// ... | |
| 118 /// LOG_TRACE("Here"); | |
| 119 /// \endcode | |
| 120 void setFormat(const char* format); | |
| 121 | |
| 122 //------------------------------------------------------------------------ | |
| 123 /// Send a message to all existing appenders. | |
| 124 static void all(const Log& log); | |
| 125 }; | |
| 126 | |
| 127 #endif // LOGAPPENDER_H |
