forked from apiel/zicBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.h
More file actions
163 lines (138 loc) · 4.48 KB
/
log.h
File metadata and controls
163 lines (138 loc) · 4.48 KB
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
/** Description:
This file serves as a core control system, often called a "logging header," dedicated to managing and reporting internal status messages generated by an application. Its primary function is to help users and developers track the program’s behavior, diagnose issues, and monitor operations.
**The Core Mechanism: Severity Levels**
The system defines five levels of message severity, ranked from least critical to most critical:
1. **Trace** (most detailed)
2. **Debug**
3. **Info**
4. **Warn** (potential problems)
5. **Error** (critical failures)
By default, the system is set to show messages at the **Debug** level or higher.
**How it Works (Filtering)**
The genius of this header lies in its efficiency through conditional filtering. When the software is compiled, a specific logging level is chosen. If the active level is set to **Warn**, the code automatically ignores and skips over all messages tagged as **Trace**, **Debug**, or **Info**. This filtering prevents the system from spending time creating and processing unnecessary messages, making the application run faster when high verbosity is not required.
**Key Features**
Every logging function allows flexible message creation, letting the user insert variable data (like numbers or status codes) directly into the message text.
For clarity and rapid identification, all output is **color-coded** when displayed on a terminal (e.g., Errors appear red, Warnings appear yellow). A utility function is also provided to report exactly what the current active severity level is.
sha: 9c0d03c88d3979c402f3efc12a7eb851d209726dd6ee3a313e67ab3c5c959faa
*/
#ifndef _ZIC_LOG_H_
#define _ZIC_LOG_H_
#include <stdarg.h>
#include <string>
#define ZIC_LOG_TRACE 1
#define ZIC_LOG_DEBUG 2
#define ZIC_LOG_INFO 3
#define ZIC_LOG_WARN 4
#define ZIC_LOG_ERROR 5
#ifndef ZIC_LOG_LEVEL
#define ZIC_LOG_LEVEL ZIC_LOG_DEBUG
#endif
void showLogLevel()
{
if (ZIC_LOG_LEVEL == ZIC_LOG_TRACE)
printf("\033[34m[info]\033[0m log level \033[35mTRACE\033[0m\n");
if (ZIC_LOG_LEVEL == ZIC_LOG_DEBUG)
printf("\033[34m[info]\033[0m log level \033[32mDEBUG\033[0m\n");
if (ZIC_LOG_LEVEL == ZIC_LOG_INFO)
printf("\033[34m[info]\033[0m log level \033[34INFO\033[0m\n");
if (ZIC_LOG_LEVEL == ZIC_LOG_WARN)
printf("\033[34m[info]\033[0m log level \033[33mWARN\033[0m\n");
if (ZIC_LOG_LEVEL == ZIC_LOG_ERROR)
printf("\033[34m[info]\033[0m log level \033[31mERROR\033[0m\n");
}
void logTrace(std::string message)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_TRACE
printf("\033[35m[trace]\033[0m %s\n", message.c_str());
fflush(stdout);
#endif
}
void logTrace(const char* message, ...)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_TRACE
va_list args;
va_start(args, message);
printf("\033[35m[trace]\033[0m ");
vprintf(message, args);
printf("\n");
va_end(args);
fflush(stdout);
#endif
}
void logDebug(std::string message)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_DEBUG
printf("\033[32m[debug]\033[0m %s\n", message.c_str());
fflush(stdout);
#endif
}
void logDebug(const char* message, ...)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_DEBUG
va_list args;
va_start(args, message);
printf("\033[32m[debug]\033[0m ");
vprintf(message, args);
printf("\n");
va_end(args);
fflush(stdout);
#endif
}
void logInfo(std::string message)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_INFO
printf("\033[34m[info]\033[0m %s\n", message.c_str());
fflush(stdout);
#endif
}
void logInfo(const char* message, ...)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_INFO
va_list args;
va_start(args, message);
printf("\033[34m[info]\033[0m ");
vprintf(message, args);
printf("\n");
va_end(args);
fflush(stdout);
#endif
}
void logWarn(std::string message)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_WARN
printf("\033[33m[warn]\033[0m %s\n", message.c_str());
fflush(stdout);
#endif
}
void logWarn(const char* message, ...)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_WARN
va_list args;
va_start(args, message);
printf("\033[33m[warn]\033[0m ");
vprintf(message, args);
printf("\n");
va_end(args);
fflush(stdout);
#endif
}
void logError(std::string message)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_ERROR
printf("\033[31m[error]\033[0m %s\n", message.c_str());
fflush(stdout);
#endif
}
void logError(const char* message, ...)
{
#if ZIC_LOG_LEVEL <= ZIC_LOG_ERROR
va_list args;
va_start(args, message);
printf("\033[31m[error]\033[0m ");
vprintf(message, args);
printf("\n");
va_end(args);
fflush(stdout);
#endif
}
#endif