Debian Codegraph @

src/cdogs/log.c

raw symbols SCIP
1 /*
2 Copyright (c) 2015-2017, 2019 Cong Xu
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 POSSIBILITY OF SUCH DAMAGE.
25 */
26 #include "log.h"
27
28 #include <errno.h>
29 #include <stdarg.h>
30 #include <time.h>
31
32 #include "rlutil/rlutil.h"
33 #include "utils.h"
34
35 FILE *gLogFile;
36
37 typedef struct
38 {
39 LogLevel Level;
40 const char *Name;
41 } LogModuleInfo;
42 // Log modules and default logging levels; indexed by LogModule
43 static LogModuleInfo sModuleInfo[] =
44 {
45 { LL_INFO, "MAIN" },
46 { LL_INFO, "NET" },
47 { LL_INFO, "INPUT" },
48 { LL_INFO, "ACTOR" },
49 { LL_INFO, "SOUND" },
50 { LL_INFO, "GFX" },
51 { LL_WARN, "MAP" },
52 { LL_INFO, "EDIT" },
53 { LL_INFO, "PATH" },
54 };
55
56
57 const char *LogModuleName(const LogModule m)
58 {
59 if ((int)m >= 0 && (int)m < LM_COUNT) return sModuleInfo[(int)m].Name;
60 CASSERT(false, "Unknown log module");
61 return "";
62 }
63 LogModule StrLogModule(const char *s)
64 {
65 for (int i = 0; i < (int)LM_COUNT; i++)
66 {
67 if (strcmp(s, sModuleInfo[i].Name) == 0) return (LogModule)i;
68 }
69 return LM_MAIN;
70 }
71
72 LogLevel LogModuleGetLevel(const LogModule m)
73 {
74 if ((int)m >= 0 && (int)m < LM_COUNT) return sModuleInfo[(int)m].Level;
75 CASSERT(false, "Unknown log module");
76 return LL_ERROR;
77 }
78 void LogModuleSetLevel(const LogModule m, const LogLevel l)
79 {
80 if ((int)m >= 0 && (int)m < LM_COUNT)
81 {
82 sModuleInfo[(int)m].Level = l;
83 }
84 else
85 {
86 CASSERT(false, "Unknown log module");
87 }
88 }
89 const char *LogLevelName(const LogLevel l)
90 {
91 switch (l)
92 {
93 T2S(LL_TRACE, "TRACE");
94 T2S(LL_DEBUG, "DEBUG");
95 T2S(LL_INFO, "INFO");
96 T2S(LL_WARN, "WARN");
97 T2S(LL_ERROR, "ERROR");
98 default:
99 return "";
100 }
101 }
102 LogLevel StrLogLevel(const char *s)
103 {
104 S2T(LL_TRACE, "TRACE");
105 S2T(LL_DEBUG, "DEBUG");
106 S2T(LL_INFO, "INFO");
107 S2T(LL_WARN, "WARN");
108 S2T(LL_ERROR, "ERROR");
109 return LL_ERROR;
110 }
111
112 #ifdef __APPLE__
113 #include <os/log.h>
114 #endif
115 void LogInit(void)
116 {
117 saveStreamDefaultColor(stderr);
118 }
119 void LogOpenFile(const char *filename)
120 {
121 // Optionally log to file
122 gLogFile = fopen(filename, "a+");
123 if (gLogFile == NULL)
124 {
125 LOG(LM_MAIN, LL_ERROR, "Error opening log filename (%s): %s",
126 filename, strerror(errno));
127 }
128 }
129 void LogTerminate(void)
130 {
131 if (gLogFile != NULL)
132 {
133 fclose(gLogFile);
134 }
135 }
136
137 static void LogSetColor(const int color)
138 {
139 #ifdef __EMSCRIPTEN__
140 UNUSED(color);
141 return;
142 #else
143 setStreamColor(stderr, color);
144 #endif
145 }
146
147 static void LogSetLevelColor(const LogLevel l)
148 {
149 switch (l)
150 {
151 case LL_TRACE: LogSetColor(GREY); break;
152 case LL_DEBUG: LogSetColor(WHITE); break;
153 case LL_INFO: LogSetColor(GREEN); break;
154 case LL_WARN: LogSetColor(YELLOW); break;
155 case LL_ERROR: LogSetColor(RED); break;
156 default: CASSERT(false, "Unknown log level"); break;
157 }
158 }
159 static void LogSetModuleColor(void)
160 {
161 LogSetColor(LIGHTBLUE);
162 }
163 static void LogSetFileColor(void)
164 {
165 LogSetColor(BROWN);
166 }
167 static void LogSetFuncColor(void)
168 {
169 LogSetColor(CYAN);
170 }
171 static void LogResetColor(void)
172 {
173 #ifndef __EMSCRIPTEN__
174 resetStreamColor(stderr);
175 #endif
176 }
177
178 // LOG_STR / LOG_VSTR
179 #if defined(__EMSCRIPTEN__)
180
181 #define LOG_STR(_level, _stream, _fmt, ...)\
182 if (_level >= LL_WARN)\
183 {\
184 fprintf(_stream, _fmt, ##__VA_ARGS__);\
185 }\
186 else\
187 {\
188 printf(_fmt, ##__VA_ARGS__);\
189 }
190
191 #define LOG_VSTR(_level, _stream, _fmt, _args)\
192 if (_level >= LL_WARN)\
193 {\
194 vfprintf(_stream, _fmt, _args);\
195 }\
196 else\
197 {\
198 vprintf(_fmt, _args);\
199 }
200
201 #else
202
203 #define LOG_STR(_level, _stream, ...)\
204 fprintf(_stream, ##__VA_ARGS__)
205 #define LOG_VSTR(_level, _stream, _fmt, _args)\
206 vfprintf(_stream, _fmt, _args)
207
208 #endif
209 void LogLine(
210 FILE *stream, const LogModule m, const LogLevel l, const char *filename,
211 const int line, const char *function, const char *fmt, ...)
212 {
213 if (stream == NULL
214 #if defined(__APPLE__) && !defined(NDEBUG)
215 // Prevent SIGPIPE when writing to stderr
216 || stream == stderr
217 #endif
218 )
219 {
220 return;
221 }
222 time_t now;
223 time(&now);
224 struct tm *ti = localtime(&now);
225 LOG_STR(l, stream, "%d%02d%02d-%02d%02d%02d ",
226 ti->tm_year + 1900, ti->tm_mon + 1, ti->tm_mday,
227 ti->tm_hour, ti->tm_min, ti->tm_sec);
228 LogSetLevelColor(l);
229 LOG_STR(l, stream, "%-5s ", LogLevelName(l));
230 LogResetColor();
231 LOG_STR(l, stream, "[");
232 LogSetModuleColor();
233 LOG_STR(l, stream, "%-5s", LogModuleName(m));
234 LogResetColor();
235 LOG_STR(l, stream, "] [");
236 LogSetFileColor();
237 LOG_STR(l, stream, "%s:%d", filename, line);
238 LogResetColor();
239 LOG_STR(l, stream, "] ");
240 LogSetFuncColor();
241 LOG_STR(l, stream, "%s()", function);
242 LogResetColor();
243 LOG_STR(l, stream, ": ");
244 LogSetLevelColor(l);
245 va_list args;
246 va_start(args, fmt);
247 LOG_VSTR(l, stream, fmt, args);
248 va_end(args);
249 LogResetColor();
250 LOG_STR(l, stream, "\n");
251 if (l >= LL_WARN)
252 {
253 fflush(stream);
254 }
255 }
256
257 void LogFlush(FILE *stream)
258 {
259 fflush(stream);
260 }