Switch to unified view

a/sc2src/log.h b/sc2src/log.h
...
...
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
16
 */
17
#ifndef _LOG_H_X_INCLUDED_
17
#ifndef _LOG_H_X_INCLUDED_
18
#define _LOG_H_X_INCLUDED_
18
#define _LOG_H_X_INCLUDED_
19
19
20
#include <fstream>                      // for ofstream
20
#ifdef HAVE_CONFIG_H
21
#include <iostream>                     // for cerr, ostream
21
#include "config.h"
22
#include <string>                       // for string
22
#endif
23
23
24
#include <fstream> 
25
#include <iostream>
26
#include <string>
27
#include <mutex>
24
28
25
    class Logger {
29
#ifndef LOGGER_THREADSAFE
26
    public:
30
#define LOGGER_THREADSAFE 1
27
        static Logger *getTheLog(const std::string& fn);
31
#endif
28
        std::ostream& getstream() {
29
            return m_tocerr ? std::cerr : m_stream;
30
        }
31
        enum LogLevel {LLNON, LLFAT, LLERR, LLINF, LLDEB, LLDEB1};
32
        void setLogLevel(LogLevel level) {
33
            m_loglevel = level;
34
        }
35
        int getloglevel() {
36
            return m_loglevel;
37
        }
38
        
39
    private:
40
        bool m_tocerr;
41
        int m_loglevel;
42
        std::ofstream m_stream;
43
        
44
        Logger(const std::string& fn);
45
  Logger(const Logger &);
46
  Logger& operator=(const Logger &);
47
    };
48
        
49
#define DEBOUT (Logger::getTheLog("")->getstream())
50
#define LOCAL_LOGINC 0
51
#define LOGLEVEL (Logger::getTheLog("")->getloglevel()+LOCAL_LOGINC)
52
32
33
#if LOGGER_THREADSAFE
34
#include <mutex>
35
#endif
36
37
// Can't use the symbolic Logger::LLXX names in preproc. 6 is LLDEB1
38
#ifndef LOGGER_STATICVERBOSITY
39
#define LOGGER_STATICVERBOSITY 6
40
#endif
41
42
class Logger {
43
public:
44
    /** Initialize logging to file name. Use "stderr" for stderr output */
45
    static Logger *getTheLog(const std::string& fn);
46
    std::ostream& getstream() {
47
        return m_tocerr ? std::cerr : m_stream;
48
    }
49
    enum LogLevel {LLNON=0, LLFAT=1, LLERR=2, LLINF=3, LLDEB=4,
50
                   LLDEB0=5, LLDEB1=6, LLDEB2=7};
51
    void setLogLevel(LogLevel level) {
52
        m_loglevel = level;
53
    }
54
    int getloglevel() {
55
        return m_loglevel;
56
    }
57
58
#if LOGGER_THREADSAFE
59
    std::mutex& getmutex() {
60
        return m_mutex;
61
    }
62
#endif
63
    
64
private:
65
    bool m_tocerr;
66
    int m_loglevel;
67
    std::ofstream m_stream;
68
#if LOGGER_THREADSAFE
69
    std::mutex m_mutex;
70
#endif
71
72
    Logger(const std::string& fn);
73
    Logger(const Logger &);
74
    Logger& operator=(const Logger &);
75
};
76
77
#define LOGGER_PRT (Logger::getTheLog("")->getstream())
78
79
#if LOGGER_THREADSAFE
80
#define LOGGER_LOCK \
81
    std::unique_lock<std::mutex> lock(Logger::getTheLog("")->getmutex())
82
#else
83
#define LOGGER_LOCK
84
#endif
85
86
#ifndef LOGGER_LOCAL_LOGINC
87
#define LOGGER_LOCAL_LOGINC 0
88
#endif
89
90
#define LOGGER_LEVEL (Logger::getTheLog("")->getloglevel() + \
91
                      LOGGER_LOCAL_LOGINC)
92
93
#define LOGGER_DOLOG(L,X) LOGGER_PRT << ":" << L << ":" <<            \
94
                                  __FILE__ << ":" << __LINE__ << "::" << X
95
96
#if LOGGER_STATICVERBOSITY >= 7
53
#define LOGDEB1(X) {                                                    \
97
#define LOGDEB2(X) {                                                    \
54
        if (LOGLEVEL >= Logger::LLDEB1)                     \
98
        if (LOGGER_LEVEL >= Logger::LLDEB2) {                           \
55
        {                                                               \
99
            LOGGER_LOCK;                                                \
56
            DEBOUT << __FILE__ << ":" << __LINE__<< "::"; DEBOUT << X;  \
100
            LOGGER_DOLOG(Logger::LLDEB2, X);                            \
57
        }                                                               \
101
        }                                                               \
58
    }
102
    }
103
#else
104
#define LOGDEB2(X)
105
#endif
59
106
107
#if LOGGER_STATICVERBOSITY >= 6
60
#define LOGDEB(X) {                                                     \
108
#define LOGDEB1(X) {                                                    \
61
        if (LOGLEVEL >= Logger::LLDEB)                      \
109
        if (LOGGER_LEVEL >= Logger::LLDEB1) {                           \
62
        {                                                               \
110
            LOGGER_LOCK;                                                \
63
            DEBOUT << __FILE__ << ":" << __LINE__<< "::"; DEBOUT << X;  \
111
            LOGGER_DOLOG(Logger::LLDEB1, X);                            \
64
        }                                                               \
112
        }                                                               \
65
    }
113
    }
114
#else
115
#define LOGDEB1(X)
116
#endif
66
117
67
#define LOGINF(X) {                                                     \
118
#if LOGGER_STATICVERBOSITY >= 5
68
        if (LOGLEVEL >= Logger::LLINF)                      \
69
        {                                                               \
70
            DEBOUT << __FILE__ << ":" << __LINE__<< "::"; DEBOUT << X;  \
71
        }                                                               \
72
    }                                                                   
73
74
#define LOGERR(X) {                                                     \
119
#define LOGDEB0(X) {                                                    \
75
        if (LOGLEVEL >= Logger::LLERR)                      \
120
        if (LOGGER_LEVEL >= Logger::LLDEB0) {                           \
76
        {                                                               \
121
            LOGGER_LOCK;                                                \
77
            DEBOUT << __FILE__ << ":" << __LINE__<< "::"; DEBOUT << X;  \
122
            LOGGER_DOLOG(Logger::LLDEB0, X);                            \
78
        }                                                               \
123
        }                                                               \
79
    }
124
    }
125
#else
126
#define LOGDEB0(X)
127
#endif
80
128
129
#if LOGGER_STATICVERBOSITY >= 4
81
#define LOGFAT(X) {                                                     \
130
#define LOGDEB(X) {                                                     \
82
        if (LOGLEVEL >= Logger::LLFAT)                      \
131
        if (LOGGER_LEVEL >= Logger::LLDEB) {                            \
83
        {                                                               \
132
            LOGGER_LOCK;                                                \
84
            DEBOUT << __FILE__ << ":" << __LINE__<< "::"; DEBOUT << X;  \
133
            LOGGER_DOLOG(Logger::LLDEB, X);                             \
85
        }                                                               \
134
        }                                                               \
86
    }
135
    }
136
#else
137
#define LOGDEB(X)
138
#endif
87
139
140
#if LOGGER_STATICVERBOSITY >= 3
141
#define LOGINF(X) {                                                     \
142
        if (LOGGER_LEVEL >= Logger::LLINF) {                            \
143
            LOGGER_LOCK;                                                \
144
            LOGGER_DOLOG(Logger::LLINF, X);                             \
145
        }                                                               \
146
    }
147
#else
148
#define LOGINF(X)
149
#endif
150
#define LOGINFO LOGINF
88
151
152
#if LOGGER_STATICVERBOSITY >= 2
153
#define LOGERR(X) {                                                     \
154
        if (LOGGER_LEVEL >= Logger::LLERR) {                            \
155
            LOGGER_LOCK;                                                \
156
            LOGGER_DOLOG(Logger::LLERR, X);                             \
157
        }                                                               \
158
    }
159
#else
160
#define LOGERR(X)
161
#endif
162
163
#if LOGGER_STATICVERBOSITY >= 1
164
#define LOGFAT(X) {                                                     \
165
        if (LOGGER_LEVEL >= Logger::LLFAT) {                            \
166
            LOGGER_LOCK;                                                \
167
            LOGGER_DOLOG(Logger::LLFAT, X);                             \
168
        }                                                               \
169
    }
170
#else
171
#define LOGFAT(X)
172
#endif
89
173
90
#endif /* _LOG_H_X_INCLUDED_ */
174
#endif /* _LOG_H_X_INCLUDED_ */