~ken-vandine/buteo-syncfw/0.8.5

« back to all changes in this revision

Viewing changes to libsynccommon/Logger.h

  • Committer: Sergey Gerasimenko
  • Date: 2010-06-29 12:51:21 UTC
  • Revision ID: git-v1:cd8dab07b102ac96752ece4f3cde5fc62697d717
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of buteo-syncfw package
 
3
 *
 
4
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 
5
 *
 
6
 * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public License
 
10
 * version 2.1 as published by the Free Software Foundation.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
 * 02110-1301 USA
 
21
 *
 
22
 */
 
23
 
 
24
 
 
25
 
 
26
#ifndef LOGGER_H
 
27
#define LOGGER_H
 
28
 
 
29
#include <QBitArray>
 
30
#include <QDebug>
 
31
#include <QFile>
 
32
#include <QList>
 
33
#include <QMutex>
 
34
#include <QString>
 
35
#include <QTextStream>
 
36
 
 
37
 
 
38
namespace Buteo {
 
39
    
 
40
/*!
 
41
 * \brief A logger singleton class.
 
42
 *
 
43
 * Using the logger is thread safe, but creating the instance is not.
 
44
 * Make sure that the instance is created before any threads that use the
 
45
 * logger are started. This can be done by calling createInstance explicitly,
 
46
 * or by simply logging some message, which will automatically create the
 
47
 * logger instance with default parameters.
 
48
 */
 
49
class Logger
 
50
{
 
51
public:
 
52
    //! Possible logging levels.
 
53
    enum LogLevel
 
54
    {
 
55
        // These are already defined in QtMsgType:
 
56
        //QtDebugMsg,
 
57
        //QtWarningMsg,
 
58
        //QtCriticalMsg,
 
59
        //QtFatalMsg,
 
60
 
 
61
        FIRST_CUSTOM_LEVEL = 4,
 
62
 
 
63
        LEVEL_FATAL = FIRST_CUSTOM_LEVEL,
 
64
        LEVEL_CRITICAL,
 
65
        LEVEL_WARNING,
 
66
        LEVEL_PROTOCOL,
 
67
        LEVEL_INFO,
 
68
        LEVEL_DEBUG,
 
69
        LEVEL_TRACE,
 
70
 
 
71
        NUM_LEVELS
 
72
    };
 
73
 
 
74
    //! Default indent size.
 
75
    static const int DEFAULT_INDENT_SIZE;
 
76
 
 
77
    /*!
 
78
     * \brief Returns the logger instance.
 
79
     *
 
80
     * If the instance is not yet created, creates it with default parameters.
 
81
     * @return The instance.
 
82
     */
 
83
    static Logger *instance();
 
84
 
 
85
    //! Destructor.
 
86
    ~Logger();
 
87
 
 
88
    /*!
 
89
     * \brief Creates a logger instance.
 
90
     *
 
91
     * If an instance already exists, deletes the old instance first.
 
92
     * This function should be called in the beginning of the program
 
93
     * before any threads using the logger are created, because creating
 
94
     * the log instance is not thread safe.
 
95
     * @param aLogFileName Name of the file where log messages are written.
 
96
     *          If this is empty, messages are not written to a file.
 
97
     * @param aUseStdOut Should messages be written to standard output.
 
98
     * @param aIndentSize Number of spaces that each indent level inserts.
 
99
     */
 
100
    static void createInstance(const QString &aLogFileName = "",
 
101
                               bool aUseStdOut = true,
 
102
                               int aIndentSize = DEFAULT_INDENT_SIZE);
 
103
 
 
104
    //! Deletes the logger instance. Closes the log file in a controlled way.
 
105
    static void deleteInstance();
 
106
 
 
107
    /*!
 
108
     * \brief Enables given log levels.
 
109
     * @param aLevels Log levels to enable. Default enables all levels.
 
110
     */
 
111
    void enable(const QBitArray &aLevels = QBitArray(NUM_LEVELS, true));
 
112
 
 
113
    /*!
 
114
     * \brief Disables given log levels.
 
115
     * @param aLevels Log levels to disable. Default disables all levels.
 
116
     */
 
117
    void disable(const QBitArray &aLevels = QBitArray(NUM_LEVELS, true));
 
118
 
 
119
    /*!
 
120
     * \brief Adds one indent level.
 
121
     */
 
122
    void push();
 
123
 
 
124
    /*!
 
125
     * \brief Removes one indent level.
 
126
     */
 
127
    void pop();
 
128
 
 
129
    /*!
 
130
     * \brief Writes a message to the log.
 
131
     *
 
132
     * @param aLevel Message level.
 
133
     * @param aMsg Message.
 
134
     */
 
135
    void write(int aLevel, const char *aMsg);
 
136
 
 
137
    /*!
 
138
     * \brief Sets logging level.
 
139
     *
 
140
     * Messages with the given level and levels more severe than it will be
 
141
     * enabled. Qt built-in log levels will also be enabled.
 
142
     * @param aLevel Logging level.
 
143
     */
 
144
    bool setLogLevel(int aLevel);
 
145
 
 
146
    /*!
 
147
     * \brief Gets logging level BitArray
 
148
     *
 
149
     * Use this API to count the levels the BitArray has been set to true
 
150
     */
 
151
    QBitArray getLogLevelArray();
 
152
 
 
153
private:
 
154
    Logger(const QString &aLogFileName, bool aUseStdOut, int aIndentSize);
 
155
 
 
156
    static Logger *sInstance;
 
157
 
 
158
    QBitArray   iEnabledLevels;
 
159
 
 
160
    int         iIndentLevel;
 
161
 
 
162
    int         iIndentSize;
 
163
 
 
164
    QFile       iFile;
 
165
 
 
166
    QTextStream *iFileStream;
 
167
 
 
168
    QTextStream *iStdOutStream;
 
169
 
 
170
    QTextStream *iStdErrStream;
 
171
 
 
172
    QMutex      iMutex;
 
173
 
 
174
};
 
175
 
 
176
}
 
177
 
 
178
#endif // LOGGER_H