File: log.h

package info (click to toggle)
recoll 1.43.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,956 kB
  • sloc: cpp: 104,864; python: 9,923; xml: 7,324; ansic: 6,447; sh: 1,252; perl: 166; makefile: 73
file content (260 lines) | stat: -rw-r--r-- 8,191 bytes parent folder | download | duplicates (3)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* Copyright (C) 2014-2019 J.F.Dockes
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the
 *  Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#ifndef _LOG_H_X_INCLUDED_
#define _LOG_H_X_INCLUDED_

#include <string.h>  // for strerror
#include <fstream> 
#include <iostream>
#include <string>

#ifndef LOGGER_THREADSAFE
#define LOGGER_THREADSAFE 1
#endif

#if LOGGER_THREADSAFE
#include <mutex>
#endif

// Can't use the symbolic Logger::LLXX names in preproc. 6 is LLDEB1
// STATICVERBOSITY is the level above which logging statements are
// preproc'ed out (can't be dynamically turned on).
#ifndef LOGGER_STATICVERBOSITY
#define LOGGER_STATICVERBOSITY 5
#endif

#define LOGGER_DATESIZE 100

/** @brief This is a singleton class. The logger pointer is obtained
 * when needed by calls to @ref getTheLog(), only the first of which
 * actually creates the object and initializes the output. */
class Logger {
public:
    Logger(const Logger&) = delete;
    Logger& operator=(const Logger&) = delete;

    /** Initialize logging to file name. Use "stderr" for stderr
     * output. Creates the singleton logger object. Only the first
     * call changes the state, further ones just return the Logger
     * pointer. */
    static Logger *getTheLog(const std::string& fn = std::string());
    static void cleanup();

    /** Close and reopen the output file. For rotating the log: rename
     * then reopen. */
    bool reopen(const std::string& fn);

    /** Retrieve the output stream in case you need to write directly
     * to it. In a multithreaded program, you probably also need to obtain
     * the mutex with @ref getmutex, and lock it. */
    std::ostream& getstream() {
        return m_tocerr ? std::cerr : m_stream;
    }

    /** @brief Log level values. Messages at level above the current will
     * not be printed. Messages at a level above
     * LOGGER_STATICVERBOSITY will not even be compiled in. */
    enum LogLevel {LLNON=0, LLFAT=1, LLERR=2, LLINF=3, LLDEB=4,
                   LLDEB0=5, LLDEB1=6, LLDEB2=7};

    /** @brief Set the log dynamic verbosity level */
    void setLogLevel(LogLevel level) {
        m_loglevel = level;
    }
    /** @brief Set the log dynamic verbosity level */
    void setloglevel(LogLevel level) {
        m_loglevel = level;
    }

    /** @brief Retrieve the current log level */
    int getloglevel() const {
        return m_loglevel;
    }
    /** @brief Retrieve current log file name */
    const std::string& getlogfilename() const {
        return m_fn;
    }
    /** @brief Logging to stderr ? */
    bool logisstderr() const {
        return m_tocerr;
    }

    /** @brief turn date logging on or off (default is off) */
    void logthedate(bool onoff) {
        m_logdate = onoff;
    }

    bool loggingdate() const {
        return m_logdate;
    }
    
    /** @brief Set the date format, as an strftime() format string. 
     * Default: "%Y%m%d-%H%M%S" . */
    void setdateformat(const std::string fmt) {
        m_datefmt = fmt;
    }

    /** Call with log locked */
    const char *datestring();
    
#if LOGGER_THREADSAFE
    std::recursive_mutex& getmutex() {
        return m_mutex;
    }
#endif
    
private:
    bool m_tocerr{false};
    bool m_logdate{false};
    int m_loglevel{LLERR};
    std::string m_datefmt{"%Y%m%d-%H%M%S"};
    std::string m_fn;
    std::ofstream m_stream;
#if LOGGER_THREADSAFE
    std::recursive_mutex m_mutex;
#endif
    char m_datebuf[LOGGER_DATESIZE];
    Logger(const std::string& fn);
    ~Logger();
};

#define LOGGER_PRT (Logger::getTheLog()->getstream())

#if LOGGER_THREADSAFE
#define LOGGER_LOCK                                                     \
    std::unique_lock<std::recursive_mutex> lock(Logger::getTheLog()->getmutex())
#else
#define LOGGER_LOCK
#endif

#ifndef LOGGER_LOCAL_LOGINC
#define LOGGER_LOCAL_LOGINC 0
#endif

#define LOGGER_LEVEL (Logger::getTheLog()->getloglevel() +    \
                      LOGGER_LOCAL_LOGINC)

#define LOGGER_DATE (Logger::getTheLog()->loggingdate() ? \
                     Logger::getTheLog()->datestring() : "")

#define LOGGER_DOLOG(L,X) LOGGER_PRT << LOGGER_DATE << ":" << L << ":" << \
                             __FILE__ << ":" << __LINE__ << "::" << X \
    << std::flush


#if LOGGER_STATICVERBOSITY >= 7
#define LOGDEB2(X) {                            \
        if (LOGGER_LEVEL >= Logger::LLDEB2) {   \
            LOGGER_LOCK;                        \
            LOGGER_DOLOG(Logger::LLDEB2, X);    \
        }                                       \
    }
#else
#define LOGDEB2(X)
#endif

#if LOGGER_STATICVERBOSITY >= 6
#define LOGDEB1(X) {                            \
        if (LOGGER_LEVEL >= Logger::LLDEB1) {   \
            LOGGER_LOCK;                        \
            LOGGER_DOLOG(Logger::LLDEB1, X);    \
        }                                       \
    }
#else
#define LOGDEB1(X)
#endif

#if LOGGER_STATICVERBOSITY >= 5
#define LOGDEB0(X) {                            \
        if (LOGGER_LEVEL >= Logger::LLDEB0) {   \
            LOGGER_LOCK;                        \
            LOGGER_DOLOG(Logger::LLDEB0, X);    \
        }                                       \
    }
#else
#define LOGDEB0(X)
#endif

#if LOGGER_STATICVERBOSITY >= 4
#define LOGDEB(X) {                             \
        if (LOGGER_LEVEL >= Logger::LLDEB) {    \
            LOGGER_LOCK;                        \
            LOGGER_DOLOG(Logger::LLDEB, X);     \
        }                                       \
    }
#else
#define LOGDEB(X)
#endif

#if LOGGER_STATICVERBOSITY >= 3
/** Log a message at level INFO. Other macros exist for other levels (LOGFAT,
 * LOGERR, LOGINF, LOGDEB, LOGDEB0... Use as: 
 * LOGINF("some text" << other stuff << ... << "\n");
 */
#define LOGINF(X) {                             \
        if (LOGGER_LEVEL >= Logger::LLINF) {    \
            LOGGER_LOCK;                        \
            LOGGER_DOLOG(Logger::LLINF, X);     \
        }                                       \
    }
#else
#define LOGINF(X)
#endif
#define LOGINFO LOGINF

#if LOGGER_STATICVERBOSITY >= 2
#define LOGERR(X) {                             \
        if (LOGGER_LEVEL >= Logger::LLERR) {    \
            LOGGER_LOCK;                        \
            LOGGER_DOLOG(Logger::LLERR, X);     \
        }                                       \
    }
#else
#define LOGERR(X)
#endif

#if LOGGER_STATICVERBOSITY >= 1
#define LOGFAT(X) {                             \
        if (LOGGER_LEVEL >= Logger::LLFAT) {    \
            LOGGER_LOCK;                        \
            LOGGER_DOLOG(Logger::LLFAT, X);     \
        }                                       \
    }
#else
#define LOGFAT(X)
#endif
#define LOGFATAL LOGFAT

#if defined(sun) || defined(_WIN32)
#define LOGSYSERR(who, what, arg) {                                     \
        LOGERR(who << ": " << what << "("  << arg << "): errno " << errno << \
               ": " << strerror(errno) << '\n');                        \
    }
#else // not WINDOWS or sun

inline char *_log_check_strerror_r(int, char *errbuf) {return errbuf;}
inline char *_log_check_strerror_r(char *cp, char *){return cp;}

#define LOGSYSERR(who, what, arg) {                                     \
        char buf[200]; buf[0] = 0;                                      \
        LOGERR(who << ": " << what << "("  << arg << "): errno " << errno << ": " << \
               _log_check_strerror_r(strerror_r(errno, buf, 200), buf) << '\n'); \
    }

#endif // not windows

#endif /* _LOG_H_X_INCLUDED_ */