Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 andreas 1
/*
21 andreas 2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
2 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
#ifndef __TERROR_H__
19
#define __TERROR_H__
20
 
21
#include <iostream>
22
#include <iomanip>
23
#include <iostream>
24
#include <sstream>
25
#include <string>
35 andreas 26
#include <chrono>
287 andreas 27
#include <mutex>
304 andreas 28
#include <thread>
2 andreas 29
 
22 andreas 30
#define LPATH_FILE          1   //!< Creates a log file and protocolls there
31
#define LPATH_SYSLOG        2   //!< Writes to the syslog.
23 andreas 32
#define LOGPATH             LPATH_FILE
22 andreas 33
 
23 andreas 34
#define HLOG_NONE           0x0000
35
#define HLOG_INFO           0x0001
36
#define HLOG_WARNING        0x0002
37
#define HLOG_ERROR          0x0004
38
#define HLOG_TRACE          0x0008
39
#define HLOG_DEBUG          0x0010
59 andreas 40
#define HLOG_PROTOCOL       (HLOG_INFO | HLOG_WARNING | HLOG_ERROR)
41
#define HLOG_ALL            (HLOG_INFO | HLOG_WARNING | HLOG_ERROR | HLOG_TRACE | HLOG_DEBUG)
2 andreas 42
 
14 andreas 43
#define SLOG_NONE           "NONE"
44
#define SLOG_INFO           "INFO"
45
#define SLOG_WARNING        "WARNING"
46
#define SLOG_ERROR          "ERROR"
47
#define SLOG_TRACE          "TRACE"
48
#define SLOG_DEBUG          "DEBUG"
49
#define SLOG_PROTOCOL       "PROTOCOL"
50
#define SLOG_ALL            "ALL"
2 andreas 51
 
52
typedef enum terrtype_t
53
{
14 andreas 54
    TERRNONE,
55
    TERRINFO,
56
    TERRWARNING,
57
    TERRERROR,
58
    TERRTRACE,
59
    TERRDEBUG
2 andreas 60
}terrtype_t;
61
 
306 andreas 62
#ifdef __APPLE__
63
typedef uint64_t threadID_t;
64
#else
65
typedef std::thread::id threadID_t;
66
#endif
67
 
68
threadID_t _getThreadID();
2 andreas 69
std::ostream& indent(std::ostream& os);
290 andreas 70
void _msg(int lv, std::ostream& os);
305 andreas 71
void _lock();
72
void _unlock();
2 andreas 73
 
74
class TStreamError
75
{
14 andreas 76
    public:
77
        TStreamError(const std::string& logFile, const std::string& logLevel);
78
        ~TStreamError();
2 andreas 79
 
23 andreas 80
        static void setLogFile(const std::string& lf);
81
        static void setLogFileOnly(const std::string& lf) { mLogfile = lf; }
14 andreas 82
        static std::string& getLogFile() { return mLogfile; }
83
        static void setLogLevel(const std::string& slv);
23 andreas 84
        static void setLogLevel(unsigned int ll) { mLogLevel = ll; }
14 andreas 85
        static unsigned int getLogLevel() { return mLogLevel; }
260 andreas 86
//        static void logMsg(std::ostream& str);
14 andreas 87
        static bool checkFilter(terrtype_t err);
369 andreas 88
        static bool checkFilter(unsigned int lv);
14 andreas 89
        friend std::ostream& indent(std::ostream& os);
90
        static void incIndent() { mIndent++; }
91
        static void decIndent();
92
        static int getIndent() { return mIndent; }
242 andreas 93
        static std::ostream *getStream();
14 andreas 94
        static std::string getTime();
23 andreas 95
        static std::ostream *resetFlags(std::ostream *os);
247 andreas 96
        static void resetFlags();
238 andreas 97
        static bool isStreamValid();
98
        static bool isStreamValid(std::ostream& os);
2 andreas 99
 
250 andreas 100
        static void startTemporaryLogLevel(unsigned int l);
101
        static void endTemporaryLogLevel();
102
 
14 andreas 103
    private:
104
        static unsigned int _getLevel(const std::string& slv);
242 andreas 105
        static void _init(bool reinit=false);
2 andreas 106
 
14 andreas 107
        const TStreamError& operator=(const TStreamError& ref);
2 andreas 108
 
14 andreas 109
        static bool mInitialized;
110
        static std::string mLogfile;
111
        static unsigned int mLogLevel;
250 andreas 112
        static unsigned int mLogLevelOld;
113
        static bool haveTemporaryLogLevel;
14 andreas 114
        static int mIndent;
115
        static std::ostream *mStream;
242 andreas 116
        static std::filebuf mOfStream;
117
        static char *mBuffer;
2 andreas 118
};
119
 
120
class TTracer
121
{
14 andreas 122
    public:
306 andreas 123
        TTracer(const std::string msg, int line, char *file, threadID_t tid);
14 andreas 124
        ~TTracer();
2 andreas 125
 
14 andreas 126
    private:
127
        std::string mHeadMsg;
35 andreas 128
        int mLine{0};
14 andreas 129
        std::string mFile;
35 andreas 130
        std::chrono::steady_clock::time_point mTimePoint;
315 andreas 131
#ifdef __ANDROID__
132
        threadID_t mThreadID;
133
#else
306 andreas 134
        threadID_t mThreadID{0};
315 andreas 135
#endif
2 andreas 136
};
137
 
138
class TError : public std::ostream
139
{
14 andreas 140
    public:
141
        static void setErrorMsg(const std::string& msg);
142
        static void setErrorMsg(terrtype_t t, const std::string& msg);
143
        static void setError() { mHaveError = true; }
144
        static std::string& getErrorMsg() { return msError; }
145
        static bool isError() { return mHaveError; }
23 andreas 146
        static bool haveErrorMsg() { return !msError.empty(); }
14 andreas 147
        static terrtype_t getErrorType() { return mErrType; }
23 andreas 148
        static void setErrorType(terrtype_t et) { mErrType = et; }
14 andreas 149
        static std::ostream& append(int lv, std::ostream& os);
242 andreas 150
        static std::string append(int lv);
14 andreas 151
        static TStreamError* Current();
306 andreas 152
        static TStreamError* Current(threadID_t tid);
14 andreas 153
        static void clear() { mHaveError = false; msError.clear(); mErrType = TERRNONE; }
154
        static void logHex(char *str, size_t size);
155
        const TError& operator=(const TError& ref);
22 andreas 156
        static void displayMessage(const std::string& msg);
2 andreas 157
 
21 andreas 158
    protected:
159
        static std::string strToHex(const char *str, size_t size, int width, bool format, int indent);
160
 
14 andreas 161
    private:
21 andreas 162
        static std::string toHex(int num, int width);
14 andreas 163
        TError() {};
164
        ~TError();
2 andreas 165
 
14 andreas 166
        static std::string msError;
167
        static bool mHaveError;
168
        static terrtype_t mErrType;
169
        static TStreamError *mCurrent;
306 andreas 170
        static threadID_t mThreadID;
14 andreas 171
        std::string mHeadMsg;
2 andreas 172
};
173
 
306 andreas 174
#define MSG_INFO(msg)       { if (TStreamError::checkFilter(HLOG_INFO)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_INFO) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
175
#define MSG_WARNING(msg)    { if (TStreamError::checkFilter(HLOG_WARNING)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_WARNING) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
176
#define MSG_ERROR(msg)      { if (TStreamError::checkFilter(HLOG_ERROR)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_ERROR) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
177
#define MSG_TRACE(msg)      { if (TStreamError::checkFilter(HLOG_TRACE)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_TRACE) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
178
#define MSG_DEBUG(msg)      { if (TStreamError::checkFilter(HLOG_DEBUG)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_DEBUG) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
179
#define MSG_PROTOCOL(msg)   { if (TStreamError::checkFilter(HLOG_PROTOCOL)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_PROTOCOL) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
23 andreas 180
 
306 andreas 181
#define DECL_TRACER(msg)    TTracer _hidden_tracer(msg, __LINE__, (char *)__FILE__, _getThreadID());
2 andreas 182
 
210 andreas 183
#define IS_LOG_INFO()       TStreamError::checkFilter(HLOG_INFO)
184
#define IS_LOG_WARNING()    TStreamError::checkFilter(HLOG_WARNING)
185
#define IS_LOG_ERROR()      TStreamError::checkFilter(HLOG_ERROR)
186
#define IS_LOG_TRACE()      TStreamError::checkFilter(HLOG_TRACE)
187
#define IS_LOG_DEBUG()      TStreamError::checkFilter(HLOG_DEBUG)
188
#define IS_LOG_PROTOCOL()   TStreamError::checkFilter(HLOG_PROTOCOL)
189
#define IS_LOG_ALL()        TStreamError::checkFilter(HLOG_ALL)
190
 
273 andreas 191
#if defined(QT_DEBUG) || defined(DEBUG)
260 andreas 192
#define START_TEMPORARY_TRACE()     TStreamError::startTemporaryLogLevel(HLOG_TRACE)
193
#define START_TEMPORARY_DEBUG()     TStreamError::startTemporaryLogLevel(HLOG_DEBUG)
194
#define START_TEMPORARY_LOG(level)  TStreamError::startTemporaryLogLevel(level)
195
#define END_TEMPORARY_LOG()         TStreamError::endTemporaryLogLevel()
273 andreas 196
#else
197
#define START_TEMPORARY_TRACE()
198
#define START_TEMPORARY_DEBUG()
199
#define START_TEMPORARY_LOG(level)
200
#define END_TEMPORARY_LOG()
201
#endif
250 andreas 202
 
2 andreas 203
#endif