simplelogger.h
Go to the documentation of this file.
1 /*
2  * Simple logging framework.
3  *
4  * License: Unlicense(http://unlicense.org)
5  *
6  * Find the latest version at https://github.com/rm5248/simplelogger
7  *
8  * To use: Copy this file into your source directory and define storage for
9  * simplelogger_global_log_function in a source file. Then set your
10  * simplelogger_global_log_function to be a function pointer to a function
11  * that you control.
12  */
13 #ifndef SIMPLELOGGER_H
14 #define SIMPLELOGGER_H
15 
16 #include "simplelogger_defs.h"
17 
18 #ifndef SIMPLELOGGER_FUNCTION
19  #ifdef _MSC_VER
20  #define SIMPLELOGGER_FUNCTION __FUNCSIG__
21  #else
22  #define SIMPLELOGGER_FUNCTION __func__
23  #endif
24 #endif
25 
29 #ifndef SIMPLELOGGER_LOG_FUNCTION_NAME
30  #define SIMPLELOGGER_LOG_FUNCTION_NAME simplelogger_global_log_function
31 #endif
33 
34 #define SIMPLELOGGER_LOG_CSTR( logger, message, level ) do{\
35  if( !SIMPLELOGGER_LOG_FUNCTION_NAME ) break;\
36  struct SL_LogLocation location;\
37  location.line_number = __LINE__;\
38  location.file = __FILE__;\
39  location.function = SIMPLELOGGER_FUNCTION;\
40  SIMPLELOGGER_LOG_FUNCTION_NAME( logger, &location, level, message );\
41  } while(0)
42 
43 #define SIMPLELOGGER_TRACE_CSTR( logger, message ) do{\
44  SIMPLELOGGER_LOG_CSTR( logger, message, SL_TRACE);\
45  } while(0)
46 #define SIMPLELOGGER_DEBUG_CSTR( logger, message ) do{\
47  SIMPLELOGGER_LOG_CSTR( logger, message, SL_DEBUG);\
48  } while(0)
49 #define SIMPLELOGGER_INFO_CSTR( logger, message ) do{\
50  SIMPLELOGGER_LOG_CSTR( logger, message, SL_INFO);\
51  } while(0)
52 #define SIMPLELOGGER_WARN_CSTR( logger, message ) do{\
53  SIMPLELOGGER_LOG_CSTR( logger, message, SL_WARN);\
54  } while(0)
55 #define SIMPLELOGGER_ERROR_CSTR( logger, message ) do{\
56  SIMPLELOGGER_LOG_CSTR( logger, message, SL_ERROR);\
57  } while(0)
58 #define SIMPLELOGGER_FATAL_CSTR( logger, message ) do{\
59  SIMPLELOGGER_LOG_CSTR( logger, message, SL_FATAL);\
60  } while(0)
61 
62 #ifdef __cplusplus
63 #include <string>
64 #include <sstream>
65 
66 #define SIMPLELOGGER_TRACE_STDSTR( logger, message ) do{\
67  std::stringstream stream;\
68  stream << message;\
69  SIMPLELOGGER_LOG_CSTR( logger, stream.str().c_str(), SL_TRACE);\
70  } while(0)
71 #define SIMPLELOGGER_DEBUG_STDSTR( logger, message ) do{\
72  std::stringstream stream;\
73  stream << message;\
74  SIMPLELOGGER_LOG_CSTR( logger, stream.str().c_str(), SL_DEBUG);\
75  } while(0)
76 #define SIMPLELOGGER_INFO_STDSTR( logger, message ) do{\
77  std::stringstream stream;\
78  stream << message;\
79  SIMPLELOGGER_LOG_CSTR( logger, stream.str().c_str(), SL_INFO);\
80  } while(0)
81 #define SIMPLELOGGER_WARN_STDSTR( logger, message ) do{\
82  std::stringstream stream;\
83  stream << message;\
84  SIMPLELOGGER_LOG_CSTR( logger, stream.str().c_str(), SL_WARN);\
85  } while(0)
86 #define SIMPLELOGGER_ERROR_STDSTR( logger, message ) do{\
87  std::stringstream stream;\
88  stream << message;\
89  SIMPLELOGGER_LOG_CSTR( logger, stream.str().c_str(), SL_ERROR);\
90  } while(0)
91 #define SIMPLELOGGER_FATAL_STDSTR( logger, message ) do{\
92  std::stringstream stream;\
93  stream << message;\
94  SIMPLELOGGER_LOG_CSTR( logger, stream.str().c_str(), SL_FATAL);\
95  } while(0)
96 
97 #ifdef SIMPLELOGGER_ENABLE_AUTO_MACROS
98 #define SIMPLELOGGER_TRACE( logger, message )\
99  SIMPLELOGGER_TRACE_STDSTR(logger, message )
100 #define SIMPLELOGGER_DEBUG( logger, message )\
101  SIMPLELOGGER_DEBUG_STDSTR(logger, message )
102 #define SIMPLELOGGER_INFO( logger, message )\
103  SIMPLELOGGER_INFO_STDSTR(logger, message )
104 #define SIMPLELOGGER_WARN( logger, message )\
105  SIMPLELOGGER_WARN_STDSTR(logger, message )
106 #define SIMPLELOGGER_ERROR( logger, message )\
107  SIMPLELOGGER_ERROR_STDSTR(logger, message )
108 #define SIMPLELOGGER_FATAL( logger, message )\
109  SIMPLELOGGER_FATAL_STDSTR(logger, message )
110 #endif /* SIMPLELOGGER_ENABLE_AUTO_MACROS */
111 
112 #ifdef SIMPLELOGGER_ENABLE_SMALL_MACROS
113 #define LOG_TRACE( logger, message )\
114  SIMPLELOGGER_TRACE_STDSTR( logger, message )
115 #define LOG_DEBUG( logger, message )\
116  SIMPLELOGGER_DEBUG_STDSTR( logger, message )
117 #define LOG_INFO( logger, message )\
118  SIMPLELOGGER_INFO_STDSTR( logger, message )
119 #define LOG_WARN( logger, message )\
120  SIMPLELOGGER_WARN_STDSTR( logger, message )
121 #define LOG_ERROR( logger, message )\
122  SIMPLELOGGER_ERROR_STDSTR( logger, message )
123 #define LOG_FATAL( logger, message )\
124  SIMPLELOGGER_FATAL_STDSTR( logger, message )
125 #endif /* SIMPLELOGGER_ENABLE_SMALL_MACROS */
126 
127 #else
128 /* C macros */
129 
130 #ifdef SIMPLELOGGER_ENABLE_AUTO_MACROS
131 #define SIMPLELOGGER_TRACE( logger, message )\
132  SIMPLELOGGER_TRACE_CSTR(logger, message )
133 #define SIMPLELOGGER_DEBUG( logger, message )\
134  SIMPLELOGGER_DEBUG_CSTR(logger, message )
135 #define SIMPLELOGGER_INFO( logger, message )\
136  SIMPLELOGGER_INFO_CSTR(logger, message )
137 #define SIMPLELOGGER_WARN( logger, message )\
138  SIMPLELOGGER_WARN_CSTR(logger, message )
139 #define SIMPLELOGGER_ERROR( logger, message )\
140  SIMPLELOGGER_ERROR_CSTR(logger, message )
141 #define SIMPLELOGGER_FATAL( logger, message )\
142  SIMPLELOGGER_FATAL_CSTR(logger, message )
143 #endif /* SIMPLELOGGER_ENABLE_AUTO_MACROS */
144 
145 #ifdef SIMPLELOGGER_ENABLE_SMALL_MACROS
146 #define LOG_TRACE( logger, message )\
147  SIMPLELOGGER_TRACE_CSTR( logger, message )
148 #define LOG_DEBUG( logger, message )\
149  SIMPLELOGGER_DEBUG_CSTR( logger, message )
150 #define LOG_INFO( logger, message )\
151  SIMPLELOGGER_INFO_CSTR( logger, message )
152 #define LOG_WARN( logger, message )\
153  SIMPLELOGGER_WARN_CSTR( logger, message )
154 #define LOG_ERROR( logger, message )\
155  SIMPLELOGGER_ERROR_CSTR( logger, message )
156 #define LOG_FATAL( logger, message )\
157  SIMPLELOGGER_FATAL_CSTR( logger, message )
158 #endif /* SIMPLELOGGER_ENABLE_SMALL_MACROS */
159 
160 #endif /* __cplusplus */
161 
162 
163 #endif /* SIMPLELOGGER_H */
simplelogger_log_function SIMPLELOGGER_LOG_FUNCTION_NAME
Global log pointer.
void(* simplelogger_log_function)(const char *logger_name, const struct SL_LogLocation *location, const enum SL_LogLevel level, const char *log_string)
Pointer to a function that does the actual log operation.
Definition: simplelogger_defs.h:38