Debian Codegraph @

src/io/log.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file log.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par Licence:
9 * Copyright (c) 2015-2026 Carsten Teibes
10 *
11 * OpenJazz is distributed under the terms of
12 * the GNU General Public License, version 2.0
13 *
14 * @par Description:
15 * Logs to console and file.
16 *
17 */
18
19 #include "log.h"
20 #include <cstdarg>
21 #include <ctime>
22 #include <cstring>
23 #include <cstdlib>
24 #include <unistd.h>
25
26 #ifdef __vita__
27 #include <psp2/kernel/clib.h>
28 #endif
29
30 #ifdef __ANDROID__
31 #include <android/log.h>
32 #endif
33
34 static struct level_info {
35 const char *name;
36 const char *color;
37 } levels[7] = {
38 { "MAX", "\x1b[95m" },
39 { "TRACE", "\x1b[94m" },
40 { "DEBUG", "\x1b[36m" },
41 { "INFO", "\x1b[32m" },
42 { "WARN", "\x1b[33m" },
43 { "ERROR", "\x1b[31m" },
44 { "FATAL", "\x1b[35m" }
45 };
46
47 /**
48 * Create logfile, set defaults
49 */
50 Log::Log () {
51
52 level = LL_DEBUG;
53 quiet = false;
54 logfile = fopen("openjazz.log", "w");
55 color_stdout = false;
56 color_stderr = false;
57
58 // these systems may use colored terminal output
59 #if defined(__linux__) || defined(__APPLE__)
60 // allow users to disable all colors (see https://no-color.org/)
61 if (!getenv("NO_COLOR")) {
62 color_stdout = !!isatty(fileno(stdout));
63 color_stderr = !!isatty(fileno(stderr));
64 }
65 #endif
66
67 #ifdef NDEBUG
68 // be more quiet in release builds
69 level = LL_INFO;
70 #endif
71
72 }
73
74 /**
75 * Close logfile
76 */
77 Log::~Log () {
78
79 if (logfile) fclose(logfile);
80
81 }
82
83 /**
84 * Set new loglevel.
85 *
86 * @param new_level The loglevel
87 */
88
89 void Log::setLevel(int new_level) {
90
91 if (new_level > LL_FATAL)
92 level = LL_FATAL;
93 else if (new_level < LL_MAX)
94 level = LL_MAX;
95 else
96 level = new_level;
97
98 }
99
100 /**
101 * Get the loglevel.
102 *
103 * @return The loglevel
104 */
105
106 int Log::getLevel() {
107
108 return level;
109
110 }
111
112 /**
113 * Enable/Disable quiet logging.
114 *
115 * @param enable Whether to enable quiet logging
116 */
117
118 void Log::setQuiet(bool enable) {
119
120 quiet = enable;
121
122 }
123
124 /**
125 * Add a message to the log.
126 *
127 * @param lvl Verbosity level
128 * @param file Source file
129 * @param line Source line
130 * @param fmt printf formatted message
131 */
132
133 void Log::log(int lvl, const char *file, int line, const char *fmt, ...) {
134 #ifdef __ANDROID__
135 // on android write only to journal, and do not spew for release builds
136 int maxlvl = LL_TRACE; //level;
137 #ifdef NDEBUG
138 maxlvl = LL_WARN;
139 #endif
140
141 // log up to set verbosity
142 if(lvl < maxlvl) return;
143
144 // use an immediate buffer for output
145 char outbuffer[1024];
146 va_list args;
147 va_start(args, fmt);
148 vsnprintf(outbuffer, sizeof(outbuffer), fmt, args);
149 va_end(args);
150
151 __android_log_write((lvl == LL_ERROR) ? ANDROID_LOG_ERROR :
152 (lvl == LL_WARN) ? ANDROID_LOG_WARN :
153 (lvl == LL_INFO) ? ANDROID_LOG_INFO : ANDROID_LOG_DEBUG,
154 "OpenJazz", outbuffer);
155 #else
156 // skip if nothing to write
157 if (!logfile && (lvl < level || quiet)) return;
158
159 // extract file name (like basename)
160 const char *src = strrchr(file, '\\');
161 if (!src) src = strrchr(file, '/');
162 if (!src)
163 src = file;
164 else
165 src++;
166
167 // get current time
168 time_t t = time(NULL);
169 struct tm *now = localtime(&t);
170
171 // log to console, up to set verbosity
172 if (!quiet && lvl >= level) {
173 // choose stderr/stdout depending on loglevel, use color if available
174 FILE *stream = stdout;
175 bool color = color_stdout;
176 if (lvl < LL_INFO) {
177 stream = stderr;
178 color = color_stderr;
179 }
180 #ifdef __vita__
181 // on vita we can only read stdout easily (with psp2shell)
182 #define LOG(...) sceClibPrintf(__VA_ARGS__)
183 #else
184 #define LOG(...) fprintf(stream, __VA_ARGS__)
185 #endif
186 char timebuf[9];
187 strftime(timebuf, 9, "%H:%M:%S", now);
188
189 if (color)
190 LOG("%s %s%-5s\x1b[0m ", timebuf, levels[lvl].color, levels[lvl].name);
191 else
192 LOG("%s %-5s ", timebuf, levels[lvl].name);
193
194 va_list args;
195 va_start(args, fmt);
196 #ifdef __vita__
197 // use an immediate buffer for output
198 char outbuffer[1024];
199 vsnprintf(outbuffer, sizeof(outbuffer), fmt, args);
200 LOG(outbuffer);
201 #else
202 vfprintf(stream, fmt, args);
203 #endif
204 va_end(args);
205
206 // only include source information if doing debug logs
207 if (level > LL_DEBUG)
208 LOG("\n");
209 else if (color)
210 LOG(" \x1b[90m(%s:%d)\x1b[0m\n", src, line);
211 else
212 LOG(" (%s:%d)\n", src, line);
213
214 fflush(stream);
215 }
216
217 // Log to file, do debug logs by default, higher if wanted
218 if (logfile && (lvl >= LL_DEBUG || level <= lvl)) {
219 char timebuf[20];
220 strftime(timebuf, 20, "%Y-%m-%d %H:%M:%S", now);
221
222 fprintf(logfile, "%s %-5s %s:%d: ", timebuf, levels[lvl].name, src, line);
223
224 va_list args;
225 va_start(args, fmt);
226 vfprintf(logfile, fmt, args);
227 va_end(args);
228
229 fprintf(logfile, "\n");
230
231 fflush(logfile);
232 }
233 #endif
234 }