-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogLevels.h
111 lines (84 loc) · 2.91 KB
/
logLevels.h
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
/******************************************************************************
* logLevels.h
*
* Definitions of the log levels as string tokens and numerical values.
*
* Copyright (C) 2012-2019 Pietro Mele
* Released under a GPL 3 license.
*
*
*****************************************************************************/
#ifndef LOGLEVELS_H
#define LOGLEVELS_H
#include <string>
#include <vector>
namespace log_viewer {
/// List of all the possible log tags with their corresponding log levels:
struct TagLevel {
std::string tag; // will be converted to uppercase
int level;
TagLevel() : tag(""), level(0) {}
TagLevel(const std::string &_tag, const int _level) : tag(_tag), level(_level) {}
};
class LogLevels
{
public:
static const int err_levelNotFound = -1,
err_fileNotFound = -2;
public:
LogLevels()
{
InitLogLevels();
multiLineLogs = false;
prevLevel = 0;
}
void SetMultiLineLogs(bool multiLine = true) { multiLineLogs = multiLine; }
int InitLogLevels();
int InitLogLevels(const std::vector<TagLevel> &_levels);
int AddLogLevels(const std::vector<TagLevel> &_levels);
int AddLogLevel(const TagLevel &_level);
int InitLogLevels(const std::string &_levelsFName);
int AddLogLevels(const std::string &_levelsFName);
int ClearLogLevels();
int GetVal(const std::string &_tag) const;
std::string GetTag(int _val) const;
std::string GetTags(int _val) const;
size_t size() const { return levels.size(); }
size_t NLevels() const; // number of distinct value log levels
int Indentation() const { return indentation; }
int LogLevelMapping(const std::string &_tag) const;
// Return log level tag and value in a log message;
// empty string/negative value if not found
int FindLogLevel(const std::string &_log, std::string &_levelTag,
bool _pickFirstTag = false,
int _column = -1);
// Return log level value in a log message;
// negative value if not found
int FindLogLevel(const std::string &_log,
bool _pickFirstTag = false,
int _column = -1);
// Return the log level tag in a log message; empty string if not found
std::string FindLogLevelTag(const std::string &_log,
bool _pickFirstTag = false,
int _column = -1) const;
// Return the log level value in a log message
int FindLogLevelVal(const std::string &_log,
bool _pickFirstTag = false,
int _column = -1) const;
void EnableWarnings(bool _enable = true) {
warnUnknownLogLevel = _enable;
}
void MakeAllUppercase();
int FindIndentation();
private:
std::vector<TagLevel> levels;
bool pickFirstTag = false; // pick the highest level tag if false
bool warnUnknownLogLevel = false;
int indentation = 0;
bool multiLineLogs = true; // log messages spanning multiple lines
int prevLevel = 0; // level of the multi-line log
static std::string ToUppercase(const std::string &_str);
};
} // log_viewer
#endif // LOGLEVELS_H