1 /*
2 	Copyright (c) 2015-2016, 2019-2020 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 #pragma once
27 
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <string.h>
31 
32 #include "sys_specifics.h"
33 
34 typedef enum
35 {
36 	LM_MAIN,
37 	LM_NET,
38 	LM_INPUT,
39 	LM_ACTOR,
40 	LM_SOUND,
41 	LM_GFX,
42 	LM_MAP,
43 	LM_EDIT,
44 	LM_PATH,
45 	LM_COUNT
46 } LogModule;
47 const char *LogModuleName(const LogModule m);
48 LogModule StrLogModule(const char *s);
49 typedef enum
50 {
51 	LL_TRACE,
52 	LL_DEBUG,
53 	LL_INFO,
54 	LL_WARN,
55 	LL_ERROR,
56 	LL_COUNT
57 } LogLevel;
58 LogLevel LogModuleGetLevel(const LogModule m);
59 void LogModuleSetLevel(const LogModule m, const LogLevel l);
60 const char *LogLevelName(const LogLevel l);
61 LogLevel StrLogLevel(const char *s);
62 
63 extern FILE *gLogFile;
64 void LogInit(void);
65 void LogOpenFile(const char *filename);
66 void LogTerminate(void);
67 
68 // Only log the file base name
69 #ifdef _WIN32
70 #define __FILENAME__                                                          \
71 	(strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
72 #else
73 #define __FILENAME__                                                          \
74 	(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
75 #endif
76 #ifndef _WIN32
77 #define __pragma(x)
78 #endif
79 #define LOG(_module, _level, ...)                                             \
80 	do                                                                        \
81 	{                                                                         \
82 		if (_level >= LogModuleGetLevel(_module))                             \
83 		{                                                                     \
84 			LogLine(                                                          \
85 				stderr, _module, _level, __FILENAME__, __LINE__, __func__,    \
86 				##__VA_ARGS__);                                               \
87 			LogLine(                                                          \
88 				gLogFile, _module, _level, __FILENAME__, __LINE__, __func__,  \
89 				##__VA_ARGS__);                                               \
90 		}                                                                     \
91 		__pragma(warning(suppress : 4127))                                    \
92 	} while (false)
93 
94 #define LOG_FLUSH()                                                           \
95 	do                                                                        \
96 	{                                                                         \
97 		LogFlush(stderr);                                                     \
98 		LogFlush(gLogFile);                                                   \
99 		__pragma(warning(suppress : 4127))                                    \
100 	} while (false)
101 
102 void LogLine(
103 	FILE *stream, const LogModule m, const LogLevel l, const char *filename,
104 	const int line, const char *function, const char *fmt, ...);
105 void LogFlush(FILE *stream);
106