-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogviewer.hpp
201 lines (142 loc) · 5.62 KB
/
logviewer.hpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/******************************************************************************
* logviewer.hpp
*
* Utility to display log files in real time on the console.
*
* Copyright (C) 2012-2019 Pietro Mele
* Released under a GPL 3 license.
*
*
*****************************************************************************/
#ifndef LOGVIEWER_HPP
#define LOGVIEWER_HPP
#include "LogContext.hpp"
#include "LogFormatter.hpp"
#include "logLevels.h"
#include "progArgs.h"
#include "ReadKeyboard.h"
#include <chrono>
#include <fstream>
#include <string>
#ifdef _WIN32
#include <Windows.h>
#endif
namespace log_viewer {
struct Compare {
std::string value;
int column;
bool comparison; // false = less than; true = greater than
};
struct ResetDefaults;
class LogViewer
{
public:
LogViewer();
LogViewer(const std::string &_logFile, int _minLogLevel = 0);
LogViewer(int argc, char* argv[]);
~LogViewer();
int Run();
int SetDefaultValues();
int SetLogFileName(const std::string &_logFile);
int SetMinLogLevel(int _minLogLevel);
int SetCommandFile(const std::string &_cmdFile);
std::string GetLogDate(const std::string &_logFile);
int CheckLogFilesDiagnostic() const;
void PrintHelp(const Utilities::ProgArgs &_args, const char* _progName, LogLevels *_logLevels = 0);
void PrintVersion(const char* _progName);
void PrintLogFilesDiagnostic(const std::string &_msg) const;
static const int version = 6, subversion = 6, subsubversion = 0;
/* Versioning conventions:
* - Even subversion number: stable version.
* - Odd subversion number: unstable/development version.
*/
static const int MSG_MISSING_COMMAND_FILE = 1,
WRN_HTML_OUTPUT_CORRUPTED = -5;
private:
int SetCommandLineParams();
int ReadCommandLineParams(int argc, char *argv[]);
int WriteHeader();
int WriteHeader_html();
int WriteLog(const std::string &_log, int _level, const std::string &_file, char _tag = ' ', int _logNumber = -1);
int WriteFooter();
int WriteFooter_html();
int GenerateLogHeader();
int MoveBackToEndLogsBlock();
int MoveBackToEndLogsBlock_html();
int PrintExtraInfo();
int ReadKeyboard(std::ifstream &ifs, std::streamoff &pos);
int ReadExternalCommands(std::ifstream &ifs, std::streamoff &pos);
int AddHtmlControls();
private:
Utilities::ProgArgs progArgs; // command line arguments
// Files' details
std::string logFile; // input log file name
std::ifstream inLogFs; // input file stream where the logs come from
bool logToFile; // (default = false)
std::string outLogFile; // file name for the output stream to redirect the logs (extensions added by logviewer)
std::string outLogFileFormat; // OS shell highlighting, HTML, markdown, ...
std::fstream textOutStream; // text output stream
std::fstream htmlOutStream; // HTML input/output stream
bool externalCtrl;
std::string cmdFile; // command file name, to control the program from outside
// Logs' details
std::string delimiters; // Specify custom delimiters for the messages (default = new line)
std::string logHeader;
bool printLogFile; // Print the log file name for each message (useful if multiple log files are shown simultaneously)
std::string logFileField; // log file name to be printed for each log message
int logNumber; // log/line numbers
int logNumberField; // log/line numbers to be printed for each log message
bool printLogNumber; // print the log/line numbers
bool multiLineLogs; // log messages spanning multiple lines
bool textParsing; // parse the input file as normal text, not as a log file
// Log levels
LogLevels logLevels; // custom log levels
int levelColumn; // ID of the column which contains the log level (default = -1, i.e. dynamic)
int minLevel; // minimum level a log must have to be shown
int beepLevel; // minimum level to get an audio signal (disabled if < 0)
bool warnUnknownLogLevel; // warning for missing level in a log
// Output details
int verbose; // amount of extra information to print
LogFormatter logFormatter;
bool consoleOutput,
textFileOutput,
htmlOutput,
markdownOutput;
// Filter details
const int printAll = -1;
bool newLogsOnly; // only print logs generated from now on
int nLatest; // number of latest logs to be printed (-1 = all)
int nLatestChars; // number of latest characters to be printed (-1 = all)
std::vector<std::string> includeStrings, // must contain the specified substring
excludeStrings; // must not contain the specified substring
std::string tempStr;
bool incStrFlag,
excStrFlag; // flags to decide whether to check for substrings
std::vector<Compare> compare; // set of comparisons to be done
LogContext context; // logs belonging to the current context
// Timing and user interaction
std::chrono::milliseconds pause; // pause among a check of the log file and the next (default = 1000)
utilities::ReadKeyboard rdKb;
int key;
int nLogsReload; // number of logs to reload when the 'r' key is pressed
std::string cmdLineParams; // command line parameters
ResetDefaults *rd;
};
struct ResetDefaults
{
ResetDefaults() {
// Reset console colors
#ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
#endif
}
~ResetDefaults() {
// Reset console colors
#ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
#endif
}
};
} // log_viewer
#endif // LOGVIEWER_HPP