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>
406 andreas 22
//#include <iomanip>
2 andreas 23
#include <iostream>
406 andreas 24
//#include <sstream>
2 andreas 25
#include <string>
35 andreas 26
#include <chrono>
406 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.
386 andreas 32
#ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
33
#define LOGPATH             LPATH_SYSLOG
34
#else
23 andreas 35
#define LOGPATH             LPATH_FILE
386 andreas 36
#endif
23 andreas 37
#define HLOG_NONE           0x0000
38
#define HLOG_INFO           0x0001
39
#define HLOG_WARNING        0x0002
40
#define HLOG_ERROR          0x0004
41
#define HLOG_TRACE          0x0008
42
#define HLOG_DEBUG          0x0010
59 andreas 43
#define HLOG_PROTOCOL       (HLOG_INFO | HLOG_WARNING | HLOG_ERROR)
44
#define HLOG_ALL            (HLOG_INFO | HLOG_WARNING | HLOG_ERROR | HLOG_TRACE | HLOG_DEBUG)
2 andreas 45
 
14 andreas 46
#define SLOG_NONE           "NONE"
47
#define SLOG_INFO           "INFO"
48
#define SLOG_WARNING        "WARNING"
49
#define SLOG_ERROR          "ERROR"
50
#define SLOG_TRACE          "TRACE"
51
#define SLOG_DEBUG          "DEBUG"
52
#define SLOG_PROTOCOL       "PROTOCOL"
53
#define SLOG_ALL            "ALL"
2 andreas 54
 
406 andreas 55
#define T_UNUSED(x) (void)x;
56
 
2 andreas 57
typedef enum terrtype_t
58
{
14 andreas 59
    TERRNONE,
60
    TERRINFO,
61
    TERRWARNING,
62
    TERRERROR,
63
    TERRTRACE,
64
    TERRDEBUG
2 andreas 65
}terrtype_t;
66
 
306 andreas 67
#ifdef __APPLE__
68
typedef uint64_t threadID_t;
69
#else
70
typedef std::thread::id threadID_t;
71
#endif
72
 
73
threadID_t _getThreadID();
2 andreas 74
std::ostream& indent(std::ostream& os);
290 andreas 75
void _msg(int lv, std::ostream& os);
305 andreas 76
void _lock();
77
void _unlock();
2 andreas 78
 
79
class TStreamError
80
{
14 andreas 81
    public:
82
        TStreamError(const std::string& logFile, const std::string& logLevel);
83
        ~TStreamError();
2 andreas 84
 
23 andreas 85
        static void setLogFile(const std::string& lf);
86
        static void setLogFileOnly(const std::string& lf) { mLogfile = lf; }
14 andreas 87
        static std::string& getLogFile() { return mLogfile; }
88
        static void setLogLevel(const std::string& slv);
23 andreas 89
        static void setLogLevel(unsigned int ll) { mLogLevel = ll; }
14 andreas 90
        static unsigned int getLogLevel() { return mLogLevel; }
260 andreas 91
//        static void logMsg(std::ostream& str);
14 andreas 92
        static bool checkFilter(terrtype_t err);
369 andreas 93
        static bool checkFilter(unsigned int lv);
14 andreas 94
        friend std::ostream& indent(std::ostream& os);
95
        static void incIndent() { mIndent++; }
96
        static void decIndent();
97
        static int getIndent() { return mIndent; }
242 andreas 98
        static std::ostream *getStream();
14 andreas 99
        static std::string getTime();
23 andreas 100
        static std::ostream *resetFlags(std::ostream *os);
247 andreas 101
        static void resetFlags();
238 andreas 102
        static bool isStreamValid();
103
        static bool isStreamValid(std::ostream& os);
2 andreas 104
 
250 andreas 105
        static void startTemporaryLogLevel(unsigned int l);
106
        static void endTemporaryLogLevel();
385 andreas 107
        static void setLogFileEnabled(bool s) { mLogFileEnabled = s; }
414 andreas 108
        static bool isInitialized() { return (mInitialized && mStream != nullptr); }
250 andreas 109
 
14 andreas 110
    private:
111
        static unsigned int _getLevel(const std::string& slv);
242 andreas 112
        static void _init(bool reinit=false);
2 andreas 113
 
14 andreas 114
        const TStreamError& operator=(const TStreamError& ref);
2 andreas 115
 
14 andreas 116
        static bool mInitialized;
117
        static std::string mLogfile;
118
        static unsigned int mLogLevel;
250 andreas 119
        static unsigned int mLogLevelOld;
120
        static bool haveTemporaryLogLevel;
385 andreas 121
        static bool mLogFileEnabled;
14 andreas 122
        static int mIndent;
123
        static std::ostream *mStream;
242 andreas 124
        static std::filebuf mOfStream;
125
        static char *mBuffer;
2 andreas 126
};
127
 
128
class TTracer
129
{
14 andreas 130
    public:
398 andreas 131
        TTracer(const std::string& msg, int line, const char *file, threadID_t tid);
14 andreas 132
        ~TTracer();
2 andreas 133
 
14 andreas 134
    private:
135
        std::string mHeadMsg;
35 andreas 136
        int mLine{0};
14 andreas 137
        std::string mFile;
35 andreas 138
        std::chrono::steady_clock::time_point mTimePoint;
315 andreas 139
#ifdef __ANDROID__
140
        threadID_t mThreadID;
141
#else
306 andreas 142
        threadID_t mThreadID{0};
315 andreas 143
#endif
2 andreas 144
};
145
 
146
class TError : public std::ostream
147
{
14 andreas 148
    public:
149
        static void setErrorMsg(const std::string& msg);
150
        static void setErrorMsg(terrtype_t t, const std::string& msg);
151
        static void setError() { mHaveError = true; }
152
        static std::string& getErrorMsg() { return msError; }
153
        static bool isError() { return mHaveError; }
23 andreas 154
        static bool haveErrorMsg() { return !msError.empty(); }
14 andreas 155
        static terrtype_t getErrorType() { return mErrType; }
23 andreas 156
        static void setErrorType(terrtype_t et) { mErrType = et; }
14 andreas 157
        static std::ostream& append(int lv, std::ostream& os);
242 andreas 158
        static std::string append(int lv);
14 andreas 159
        static TStreamError* Current();
306 andreas 160
        static TStreamError* Current(threadID_t tid);
14 andreas 161
        static void clear() { mHaveError = false; msError.clear(); mErrType = TERRNONE; }
162
        static void logHex(char *str, size_t size);
163
        const TError& operator=(const TError& ref);
22 andreas 164
        static void displayMessage(const std::string& msg);
2 andreas 165
 
21 andreas 166
    protected:
167
        static std::string strToHex(const char *str, size_t size, int width, bool format, int indent);
168
 
14 andreas 169
    private:
21 andreas 170
        static std::string toHex(int num, int width);
398 andreas 171
        TError() {}
14 andreas 172
        ~TError();
2 andreas 173
 
14 andreas 174
        static std::string msError;
175
        static bool mHaveError;
176
        static terrtype_t mErrType;
177
        static TStreamError *mCurrent;
306 andreas 178
        static threadID_t mThreadID;
14 andreas 179
        std::string mHeadMsg;
2 andreas 180
};
181
 
306 andreas 182
#define MSG_INFO(msg)       { if (TStreamError::checkFilter(HLOG_INFO)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_INFO) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
183
#define MSG_WARNING(msg)    { if (TStreamError::checkFilter(HLOG_WARNING)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_WARNING) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
184
#define MSG_ERROR(msg)      { if (TStreamError::checkFilter(HLOG_ERROR)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_ERROR) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
185
#define MSG_TRACE(msg)      { if (TStreamError::checkFilter(HLOG_TRACE)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_TRACE) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
401 andreas 186
#ifndef NDEBUG
306 andreas 187
#define MSG_DEBUG(msg)      { if (TStreamError::checkFilter(HLOG_DEBUG)) { _lock(); *TError::Current(_getThreadID())->getStream() << TError::append(HLOG_DEBUG) << msg << std::endl; TStreamError::resetFlags(); _unlock(); }}
401 andreas 188
#else
189
#define MSG_DEBUG(msg)
190
#endif
306 andreas 191
#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 192
 
398 andreas 193
#define DECL_TRACER(msg)    TTracer _hidden_tracer(msg, __LINE__, static_cast<const char *>(__FILE__), _getThreadID());
2 andreas 194
 
210 andreas 195
#define IS_LOG_INFO()       TStreamError::checkFilter(HLOG_INFO)
196
#define IS_LOG_WARNING()    TStreamError::checkFilter(HLOG_WARNING)
197
#define IS_LOG_ERROR()      TStreamError::checkFilter(HLOG_ERROR)
198
#define IS_LOG_TRACE()      TStreamError::checkFilter(HLOG_TRACE)
199
#define IS_LOG_DEBUG()      TStreamError::checkFilter(HLOG_DEBUG)
200
#define IS_LOG_PROTOCOL()   TStreamError::checkFilter(HLOG_PROTOCOL)
201
#define IS_LOG_ALL()        TStreamError::checkFilter(HLOG_ALL)
202
 
401 andreas 203
#ifndef NDEBUG
260 andreas 204
#define START_TEMPORARY_TRACE()     TStreamError::startTemporaryLogLevel(HLOG_TRACE)
205
#define START_TEMPORARY_DEBUG()     TStreamError::startTemporaryLogLevel(HLOG_DEBUG)
206
#define START_TEMPORARY_LOG(level)  TStreamError::startTemporaryLogLevel(level)
207
#define END_TEMPORARY_LOG()         TStreamError::endTemporaryLogLevel()
273 andreas 208
#else
209
#define START_TEMPORARY_TRACE()
210
#define START_TEMPORARY_DEBUG()
211
#define START_TEMPORARY_LOG(level)
212
#define END_TEMPORARY_LOG()
213
#endif
250 andreas 214
 
2 andreas 215
#endif