diff options
Diffstat (limited to 'toxcore/logger.h')
-rw-r--r-- | toxcore/logger.h | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/toxcore/logger.h b/toxcore/logger.h new file mode 100644 index 00000000..6c65850e --- /dev/null +++ b/toxcore/logger.h | |||
@@ -0,0 +1,86 @@ | |||
1 | /* logger.h | ||
2 | * | ||
3 | * Wrapping logger functions in nice macros | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifndef __TOXLOGGER | ||
26 | #define __TOXLOGGER | ||
27 | |||
28 | // #define LOGGING | ||
29 | |||
30 | #ifdef LOGGING | ||
31 | #include <string.h> | ||
32 | |||
33 | typedef enum _LoggerLevel | ||
34 | { | ||
35 | INFO, | ||
36 | DEBUG, | ||
37 | WARNING, | ||
38 | ERROR | ||
39 | } LoggerLevel; | ||
40 | |||
41 | /* | ||
42 | * Set 'level' as the lowest printable level | ||
43 | */ | ||
44 | int logger_init(const char* file_name, LoggerLevel level); | ||
45 | const char* logger_stringify_level(LoggerLevel level); | ||
46 | unsigned logger_get_pid(); | ||
47 | void logger_write (LoggerLevel level, const char* format, ...); | ||
48 | char* logger_timestr (char* dest); | ||
49 | |||
50 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
51 | #define _SFILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) | ||
52 | #else | ||
53 | #define _SFILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) | ||
54 | #endif | ||
55 | |||
56 | #define WRITE_FORMAT(__LEVEL__, format) char* the_str = calloc(sizeof(char), strlen(format)+ 500); sprintf(the_str, "[%u] [%s] [%s] [%s:%d %s()] %s\n", \ | ||
57 | logger_get_pid(), logger_stringify_level(__LEVEL__), logger_timestr(__time__), _SFILE, __LINE__, __func__, format) | ||
58 | |||
59 | /* Use these macros */ | ||
60 | |||
61 | #define LOGGER_INIT(name, level) logger_init(name, level); | ||
62 | #define LOGGER_INFO(format, ...) do { char __time__[20]; WRITE_FORMAT(INFO, format); logger_write( INFO, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
63 | #define LOGGER_DEBUG(format, ...) do { char __time__[20]; WRITE_FORMAT(DEBUG, format); logger_write( DEBUG, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
64 | #define LOGGER_WARNING(format, ...) do { char __time__[20]; WRITE_FORMAT(WARNING, format); logger_write( WARNING, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
65 | #define LOGGER_ERROR(format, ...) do { char __time__[20]; WRITE_FORMAT(ERROR, format); logger_write( ERROR, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
66 | |||
67 | /* To do some checks or similar only when logging use this */ | ||
68 | #define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0) | ||
69 | |||
70 | #else | ||
71 | |||
72 | |||
73 | #define LOGGER_INIT(name, level) | ||
74 | #define LOGGER_INFO(format, ...) | ||
75 | #define LOGGER_DEBUG(format, ...) | ||
76 | #define LOGGER_WARNING(format, ...) | ||
77 | #define LOGGER_ERROR(format, ...) | ||
78 | |||
79 | #define LOGGER_SCOPE(__SCOPE_DO__) | ||
80 | |||
81 | #endif /* LOGGING */ | ||
82 | |||
83 | |||
84 | |||
85 | |||
86 | #endif /* __TOXLOGGER */ \ No newline at end of file | ||