summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/logger/AbstractAppender.h
blob: df1df4957c72488b9012e73b7e74c49b120a4d31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
  Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com)

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1
  as published by the Free Software Foundation and appearing in the file
  LICENSE.LGPL included in the packaging of this file.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.
*/
#ifndef ABSTRACTAPPENDER_H
#define ABSTRACTAPPENDER_H

// Local
#include "CuteLogger_global.h"
#include <Logger.h>

// Qt
#include <QMutex>

//! The AbstractAppender class provides an abstract base class for writing a log entries.
/**
 * The AbstractAppender class is the base interface class for all log appenders that could be used with Logger.
 *
 * AbstractAppender provides a common implementation for the thread safe, mutex-protected logging of application
 * messages, such as ConsoleAppender, FileAppender or something else. AbstractAppender is abstract and can not be
 * instantiated, but you can use any of its subclasses or create a custom log appender at your choice.
 *
 * Appenders are the logical devices that is aimed to be attached to Logger object by calling
 * Logger::registerAppender(). On each log record call from the application Logger object sequentially calls write()
 * function on all the appenders registered in it.
 *
 * You can subclass AbstractAppender to implement a logging target of any kind you like. It may be the external logging
 * subsystem (for example, syslog in *nix), XML file, SQL database entries, D-Bus messages or anything else you can
 * imagine.
 *
 * For the simple non-structured plain text logging (for example, to a plain text file or to the console output) you may
 * like to subclass the AbstractStringAppender instead of AbstractAppender, which will give you a more convinient way to
 * control the format of the log output.
 *
 * \sa AbstractStringAppender
 * \sa Logger::registerAppender()
 */
class CUTELOGGERSHARED_EXPORT AbstractAppender
{
  public:
    //! Constructs a AbstractAppender object.
    AbstractAppender();

    //! Destructs the AbstractAppender object.
    virtual ~AbstractAppender();

    //! Returns the current details level of appender.
    /**
     * Log records with a log level lower than a current detailsLevel() will be silently ignored by appender and would not
     * be sent to its append() function.
     *
     * It provides additional logging flexibility, allowing you to set the different severity levels for different types
     * of logs.
     *
     * \note This function is thread safe.
     *
     * \sa setDetailsLevel()
     * \sa Logger::LogLevel
     */
    Logger::LogLevel detailsLevel() const;

    //! Sets the current details level of appender.
    /**
     * \note This function is thread safe.
     *
     * \sa detalsLevel()
     * \sa Logger::LogLevel
     */
    void setDetailsLevel(Logger::LogLevel level);

    //! Sets the current details level of appender
    /**
     * This function is provided for convinience, it behaves like an above function.
     *
     * \sa detalsLevel()
     * \sa Logger::LogLevel
     */
    void setDetailsLevel(const QString& level);

    //! Tries to write the log record to this logger
    /**
     * This is the function called by Logger object to write a log message to the appender.
     *
     * \note This function is thread safe.
     *
     * \sa Logger::write()
     * \sa detailsLevel()
     */
    void write(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, const char* function,
               const QString& message);

  protected:
    //! Writes the log record to the logger instance
    /**
     * This function is called every time when user tries to write a message to this AbstractAppender instance using
     * the write() function. Write function works as proxy and transfers only the messages with log level more or equal
     * to the current logLevel().
     *
     * Overload this function when you are implementing a custom appender.
     *
     * \note This function is not needed to be thread safe because it is never called directly by Logger object. The
     * write() function works as a proxy and protects this function from concurrent access.
     *
     * \sa Logger::write()
     */
    virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line,
                        const char* function, const QString& message) = 0;

  private:
    QMutex m_writeMutex;

    Logger::LogLevel m_detailsLevel;
    mutable QMutex m_detailsLevelMutex;
};

#endif // ABSTRACTAPPENDER_H