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>
|
|
|
26 |
|
22 |
andreas |
27 |
#define LPATH_FILE 1 //!< Creates a log file and protocolls there
|
|
|
28 |
#define LPATH_SYSLOG 2 //!< Writes to the syslog.
|
23 |
andreas |
29 |
#define LOGPATH LPATH_FILE
|
22 |
andreas |
30 |
|
23 |
andreas |
31 |
#define HLOG_NONE 0x0000
|
|
|
32 |
#define HLOG_INFO 0x0001
|
|
|
33 |
#define HLOG_WARNING 0x0002
|
|
|
34 |
#define HLOG_ERROR 0x0004
|
|
|
35 |
#define HLOG_TRACE 0x0008
|
|
|
36 |
#define HLOG_DEBUG 0x0010
|
|
|
37 |
#define HLOG_PROTOCOL HLOG_INFO | HLOG_ERROR
|
|
|
38 |
#define HLOG_ALL HLOG_INFO | HLOG_WARNING | HLOG_ERROR | HLOG_TRACE | HLOG_DEBUG
|
2 |
andreas |
39 |
|
14 |
andreas |
40 |
#define SLOG_NONE "NONE"
|
|
|
41 |
#define SLOG_INFO "INFO"
|
|
|
42 |
#define SLOG_WARNING "WARNING"
|
|
|
43 |
#define SLOG_ERROR "ERROR"
|
|
|
44 |
#define SLOG_TRACE "TRACE"
|
|
|
45 |
#define SLOG_DEBUG "DEBUG"
|
|
|
46 |
#define SLOG_PROTOCOL "PROTOCOL"
|
|
|
47 |
#define SLOG_ALL "ALL"
|
2 |
andreas |
48 |
|
|
|
49 |
typedef enum terrtype_t
|
|
|
50 |
{
|
14 |
andreas |
51 |
TERRNONE,
|
|
|
52 |
TERRINFO,
|
|
|
53 |
TERRWARNING,
|
|
|
54 |
TERRERROR,
|
|
|
55 |
TERRTRACE,
|
|
|
56 |
TERRDEBUG
|
2 |
andreas |
57 |
}terrtype_t;
|
|
|
58 |
|
|
|
59 |
std::ostream& indent(std::ostream& os);
|
|
|
60 |
|
|
|
61 |
class TStreamError
|
|
|
62 |
{
|
14 |
andreas |
63 |
public:
|
|
|
64 |
TStreamError(const std::string& logFile, const std::string& logLevel);
|
|
|
65 |
~TStreamError();
|
2 |
andreas |
66 |
|
23 |
andreas |
67 |
static void setLogFile(const std::string& lf);
|
|
|
68 |
static void setLogFileOnly(const std::string& lf) { mLogfile = lf; }
|
14 |
andreas |
69 |
static std::string& getLogFile() { return mLogfile; }
|
|
|
70 |
static void setLogLevel(const std::string& slv);
|
23 |
andreas |
71 |
static void setLogLevel(unsigned int ll) { mLogLevel = ll; }
|
14 |
andreas |
72 |
static unsigned int getLogLevel() { return mLogLevel; }
|
|
|
73 |
static void logMsg(std::ostream& str);
|
|
|
74 |
static bool checkFilter(terrtype_t err);
|
|
|
75 |
static bool checkFilter(int lv);
|
|
|
76 |
friend std::ostream& indent(std::ostream& os);
|
|
|
77 |
static void incIndent() { mIndent++; }
|
|
|
78 |
static void decIndent();
|
|
|
79 |
static int getIndent() { return mIndent; }
|
|
|
80 |
static std::ostream *getStream() { return mStream; }
|
|
|
81 |
static std::string getTime();
|
23 |
andreas |
82 |
static std::ostream *resetFlags(std::ostream *os);
|
2 |
andreas |
83 |
|
14 |
andreas |
84 |
private:
|
|
|
85 |
static unsigned int _getLevel(const std::string& slv);
|
|
|
86 |
static void _init();
|
2 |
andreas |
87 |
|
14 |
andreas |
88 |
const TStreamError& operator=(const TStreamError& ref);
|
2 |
andreas |
89 |
|
14 |
andreas |
90 |
static bool mInitialized;
|
|
|
91 |
static std::string mLogfile;
|
|
|
92 |
static unsigned int mLogLevel;
|
|
|
93 |
static int mIndent;
|
|
|
94 |
static std::ostream *mStream;
|
2 |
andreas |
95 |
};
|
|
|
96 |
|
|
|
97 |
class TTracer
|
|
|
98 |
{
|
14 |
andreas |
99 |
public:
|
|
|
100 |
TTracer(const std::string& msg, int line, char *file);
|
|
|
101 |
~TTracer();
|
2 |
andreas |
102 |
|
14 |
andreas |
103 |
private:
|
|
|
104 |
std::string mHeadMsg;
|
|
|
105 |
int mLine;
|
|
|
106 |
std::string mFile;
|
2 |
andreas |
107 |
};
|
|
|
108 |
|
|
|
109 |
class TError : public std::ostream
|
|
|
110 |
{
|
14 |
andreas |
111 |
public:
|
|
|
112 |
static void setErrorMsg(const std::string& msg);
|
|
|
113 |
static void setErrorMsg(terrtype_t t, const std::string& msg);
|
|
|
114 |
static void setError() { mHaveError = true; }
|
|
|
115 |
static std::string& getErrorMsg() { return msError; }
|
|
|
116 |
static bool isError() { return mHaveError; }
|
23 |
andreas |
117 |
static bool haveErrorMsg() { return !msError.empty(); }
|
14 |
andreas |
118 |
static terrtype_t getErrorType() { return mErrType; }
|
23 |
andreas |
119 |
static void setErrorType(terrtype_t et) { mErrType = et; }
|
14 |
andreas |
120 |
static std::ostream& append(int lv, std::ostream& os);
|
|
|
121 |
static TStreamError* Current();
|
|
|
122 |
static void clear() { mHaveError = false; msError.clear(); mErrType = TERRNONE; }
|
|
|
123 |
static void logHex(char *str, size_t size);
|
|
|
124 |
const TError& operator=(const TError& ref);
|
|
|
125 |
static void lock();
|
|
|
126 |
static void unlock();
|
22 |
andreas |
127 |
#if defined(__linux__) || defined(Q_OS_ANDROID)
|
|
|
128 |
static void displayMessage(const std::string& msg);
|
|
|
129 |
#endif
|
2 |
andreas |
130 |
|
21 |
andreas |
131 |
protected:
|
|
|
132 |
static std::string strToHex(const char *str, size_t size, int width, bool format, int indent);
|
|
|
133 |
|
14 |
andreas |
134 |
private:
|
21 |
andreas |
135 |
static std::string toHex(int num, int width);
|
14 |
andreas |
136 |
TError() {};
|
|
|
137 |
~TError();
|
2 |
andreas |
138 |
|
14 |
andreas |
139 |
static std::string msError;
|
|
|
140 |
static bool mHaveError;
|
|
|
141 |
static terrtype_t mErrType;
|
|
|
142 |
static TStreamError *mCurrent;
|
|
|
143 |
std::string mHeadMsg;
|
2 |
andreas |
144 |
};
|
|
|
145 |
|
23 |
andreas |
146 |
#define MSG_INFO(msg) { TError::lock(); if (TStreamError::checkFilter(HLOG_INFO)) { TError::Current()->logMsg(TError::append(HLOG_INFO, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
|
|
|
147 |
#define MSG_WARNING(msg) { TError::lock(); if (TStreamError::checkFilter(HLOG_WARNING)) { TError::Current()->logMsg(TError::append(HLOG_WARNING, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
|
|
|
148 |
#define MSG_ERROR(msg) { TError::lock(); if (TStreamError::checkFilter(HLOG_ERROR)) { TError::Current()->logMsg(TError::append(HLOG_ERROR, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
|
|
|
149 |
#define MSG_TRACE(msg) { TError::lock(); if (TStreamError::checkFilter(HLOG_TRACE)) { TError::Current()->logMsg(TError::append(HLOG_TRACE, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
|
|
|
150 |
#define MSG_DEBUG(msg) { TError::lock(); if (TStreamError::checkFilter(HLOG_DEBUG)) { TError::Current()->logMsg(TError::append(HLOG_DEBUG, *TStreamError::getStream()) << msg << std::endl); } TError::unlock(); }
|
|
|
151 |
|
14 |
andreas |
152 |
#define DECL_TRACER(msg) TTracer _hidden_tracer(msg, __LINE__, (char *)__FILE__);
|
2 |
andreas |
153 |
|
|
|
154 |
#endif
|