9
9
#include < sstream>
10
10
#include < thread>
11
11
#include < cstring>
12
+ #include < vector>
13
+ #include < algorithm>
12
14
13
15
/* *
14
16
* Logger
15
17
* <p>
16
18
* Use Logger::init() to start the logger and Logger::exit() to close the logger
17
19
* <p>
18
20
* Simple usage :
19
- * Logger::debug (FILE_AND_CONSOLE, "Is it simple ? YES");
21
+ * DEBUG_LOG (FILE_AND_CONSOLE, "Is it simple ? YES");
20
22
* Write 'Is it simple ? YES' (without the quote) in the console and the file
21
23
* <p>
22
24
* Complex usage :
23
- * Logger::info (CONSOLE_ONLY, "Not too complex ? ", "Maybe");
25
+ * INFO_LOG (CONSOLE_ONLY, "Not too complex ? ", "Maybe");
24
26
* Write 'Not toot complex ? Maybe' (without the quote) only in the console
25
27
*
26
28
* <!> WARNING <!>
@@ -112,7 +114,9 @@ class Logger {
112
114
/* *
113
115
* Initialisation
114
116
*/
115
- static void init ();
117
+ static void init (LoggerOption verboseP = FILE_AND_CONSOLE,
118
+ bool showTraceP = true ,
119
+ const std::vector<LoggerType> &showTypesP = {INFO, SUCCESS, ERROR, WARNING, DEBUG});
116
120
117
121
/* *
118
122
* Quit the log and close the writer
@@ -122,67 +126,73 @@ class Logger {
122
126
public:
123
127
/* *
124
128
* Info
129
+ * @param function std::string
125
130
* @param option LoggerOption
126
131
* @param arg const char*
127
132
* @param ...
128
133
*/
129
134
template <typename ... Ts>
130
- static void info (LoggerOption option, Ts const &... args) {
131
- genericLog (stringify (args...), INFO, option);
135
+ static void info (const std::string &function, LoggerOption option, Ts const &... args) {
136
+ genericLog (function, stringify (args...), INFO, option);
132
137
}
133
138
134
139
/* *
135
140
* Success
141
+ * @param function std::string
136
142
* @param option LoggerOption
137
143
* @param arg const char*
138
144
* @param ...
139
145
*/
140
146
template <typename ... Ts>
141
- static void success (LoggerOption option, Ts const &... args) {
142
- genericLog (stringify (args...), SUCCESS, option);
147
+ static void success (const std::string &function, LoggerOption option, Ts const &... args) {
148
+ genericLog (function, stringify (args...), SUCCESS, option);
143
149
}
144
150
145
151
/* *
146
152
* Error
153
+ * @param function std::string
147
154
* @param option LoggerOption
148
155
* @param arg const char*
149
156
* @param ...
150
157
*/
151
158
template <typename ... Ts>
152
- static void error (LoggerOption option, Ts const &... args) {
153
- genericLog (stringify (args...), ERROR, option);
159
+ static void error (const std::string &function, LoggerOption option, Ts const &... args) {
160
+ genericLog (function, stringify (args...), ERROR, option);
154
161
}
155
162
156
163
/* *
157
164
* Warning
165
+ * @param function std::string
158
166
* @param option LoggerOption
159
167
* @param arg const char*
160
168
* @param ...
161
169
*/
162
170
template <typename ... Ts>
163
- static void warning (LoggerOption option, Ts const &... args) {
164
- genericLog (stringify (args...), WARNING, option);
171
+ static void warning (const std::string &function, LoggerOption option, Ts const &... args) {
172
+ genericLog (function, stringify (args...), WARNING, option);
165
173
}
166
174
167
175
/* *
168
176
* Debug
177
+ * @param function std::string
169
178
* @param option LoggerOption
170
179
* @param arg const char*
171
180
* @param ...
172
181
*/
173
182
template <typename ... Ts>
174
- static void debug (LoggerOption option, Ts const &... args) {
175
- genericLog (stringify (args...), DEBUG, option);
183
+ static void debug (const std::string &function, LoggerOption option, Ts const &... args) {
184
+ genericLog (function, stringify (args...), DEBUG, option);
176
185
}
177
186
178
187
private:
179
188
/* *
180
189
* Generic log use for all logs
190
+ * @param function std::string
181
191
* @param message std::string
182
192
* @param type LoggerType
183
193
* @param option LoggerOption
184
194
*/
185
- static void genericLog (const std::string &message, LoggerType type, LoggerOption option);
195
+ static void genericLog (const std::string &function, const std::string & message, LoggerType type, LoggerOption option);
186
196
187
197
/* *
188
198
* Write the log into the file
@@ -218,6 +228,18 @@ class Logger {
218
228
* A mutex for writeToFile(), to be thread-safe
219
229
*/
220
230
static pthread_mutex_t mutex;
231
+ /* *
232
+ * The type of verbose
233
+ */
234
+ static LoggerOption verbose;
235
+ /* *
236
+ * Show trace or not
237
+ */
238
+ static bool showTrace;
239
+ /* *
240
+ * The types of logs that be shown
241
+ */
242
+ static std::vector<LoggerType> showTypes;
221
243
222
244
private: // Disallow to instance this class
223
245
Logger () = default ;
@@ -249,4 +271,10 @@ class Logger {
249
271
}
250
272
};
251
273
274
+ #define INFO_LOG (option, msg... ) Logger::info(__FUNCTION__, option, msg)
275
+ #define SUCCESS_LOG (option, msg... ) Logger::success(__FUNCTION__, option, msg)
276
+ #define ERROR_LOG (option, msg... ) Logger::error(__FUNCTION__, option, msg)
277
+ #define WARNING_LOG (option, msg... ) Logger::warning(__FUNCTION__, option, msg)
278
+ #define DEBUG_LOG (option, msg... ) Logger::debug(__FUNCTION__, option, msg)
279
+
252
280
#endif // LOGGER_LOGGER_HPP
0 commit comments