view 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
line wrap: on
line source

//////////////////////////////////////////////////////////////////////////////
/// \file   LogAppender.h
/// \brief  Put Log message somewhere.
/// \author JD Gascuel.
/// \copyright (c) 2011-2016 JD Gascuel. All rights reserved.
/// $Id$
//////////////////////////////////////////////////////////////////////////////
//
// BSD 2-Clause License:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////
// HISTORY
//  2013-11-15  jDG: Creation.
//  2016-05-24  jDG: BSD-2 version.

#include "Utils/Log.h"

#ifndef LOGAPPENDER_H
#define LOGAPPENDER_H

#include <QByteArray>
#include <QString>

#include <QList>

class LogFilter;

//////////////////////////////////////////////////////////////////////////////

class EXPORT LogAppender
{
    /// Send a message to this appender.
    virtual void operator()(const Log& log) = 0;

    /// Log::close() needs an access to the list, to remove all appenders.
    friend void Log::close();

    /// Manage a list of all existing appenders.
    static QList<LogAppender *> &list();

protected:
    /// How to dislay logged data ?
    QByteArray _format;
    Log::Level _minLevel;

    /// Default formating string for that appender.
    virtual const char* type() const = 0;
    virtual const char* defaultFormat() const = 0;
    virtual Log::Level defaultMinLevel() const = 0;

    /// Do the substtutions
    /// Use the string set by setFormat() to format the \p log event.
    QString format(const Log& log) const;

public:
    LogAppender(int argc, char *argv[]);
    virtual ~LogAppender();

    //------------------------------------------------------------------------
    /// Filter messages by error level.
    /// Sets the minimum level to use for this appender.
    /// \p level is one of:
    /// * Log::LEVEL_DEBUG   : The most verbose one, used only during debug session.
    /// * Log::LEVEL_TRACE   : Messages to be put in silent log files, for post-mortem analysis.
    /// * Log::LEVEL_INFO    : General information send to the final user.
    /// * Log::LEVEL_WARNING : Error reported to the user (eg. with a Qt warning box. \sa LogAppenderWindow).
    /// * Log::LEVEL_THROW   : Error that is mean to be catched, and corrected automatically.
    /// * Log::LEVEL_ERROR   : Fatal error that cannot be recovered. The program should halt.
    ///
    /// The default depends from the appender.
    void setMinLevel(Log::Level level);

    //------------------------------------------------------------------------
    /// Set current formating.
    ///
    /// How to format log line once appended.
    /// Default depends of the appender used (\sa defaultFormat()).
    /// Setting a null string returns to the appender's default.
    ///
    /// Valide substitutions are:
    /// * %o : log level (one of DEBUG, TRACE, INFO, THROW, WARNING, ERROR).
    /// * %d : log's date.
    /// * %t : log's time.
    /// * %f : source file where the error is logged (just trailling filename)
    /// * %F : source file where the error is logged (full path)
    /// * %l : source line where the error is logged.
    /// * %p : the function that fired the log message.
    /// * %m : the log message itself.
    ///
    /// Typical usage:
    /// \code
    ///     LogAppenderMemory mem(0,0);
    ///     mem.setFormat("[%o] %f:%l %m");
    ///     ...
    ///     LOG_TRACE("Here");
    /// \endcode
    void setFormat(const char* format);

    //------------------------------------------------------------------------
    /// Send a message to all existing appenders.
    static void all(const Log& log);
};

#endif // LOGAPPENDER_H