diff options
author | mannol <eniz_vukovic@hotmail.com> | 2014-04-27 19:21:26 +0200 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2014-04-27 19:21:26 +0200 |
commit | 42b25a4d3e2fe66f03cbd8c866d8af7bd4f6e5a7 (patch) | |
tree | 161a21847a79f7fe052a9e9ad1b9b802d04defc6 /toxcore/logger.c | |
parent | 736f5f80347a39f6b82cda8a4ddc1f7d88fcc2f5 (diff) |
Yeah many calls
Diffstat (limited to 'toxcore/logger.c')
-rw-r--r-- | toxcore/logger.c | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/toxcore/logger.c b/toxcore/logger.c new file mode 100644 index 00000000..e700fe71 --- /dev/null +++ b/toxcore/logger.c | |||
@@ -0,0 +1,159 @@ | |||
1 | /* logger.c | ||
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 | #ifdef HAVE_CONFIG_H | ||
25 | #include "config.h" | ||
26 | #endif /* HAVE_CONFIG_H */ | ||
27 | |||
28 | |||
29 | #include "logger.h" | ||
30 | |||
31 | #ifdef LOGGING | ||
32 | |||
33 | #include "network.h" /* for time */ | ||
34 | |||
35 | #include <stdio.h> | ||
36 | #include <errno.h> | ||
37 | #include <stdlib.h> | ||
38 | #include <stdarg.h> | ||
39 | #include <inttypes.h> | ||
40 | #include <time.h> | ||
41 | |||
42 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
43 | #define strerror_r(errno,buf,len) strerror_s(buf,len,errno) | ||
44 | #endif | ||
45 | |||
46 | static struct logger_config { | ||
47 | FILE* log_file; | ||
48 | LoggerLevel level; | ||
49 | uint64_t start_time; /* Time when lib loaded */ | ||
50 | } | ||
51 | logger = { | ||
52 | NULL, | ||
53 | DEBUG, | ||
54 | 0 | ||
55 | }; | ||
56 | |||
57 | void __attribute__((destructor)) terminate_logger() | ||
58 | { | ||
59 | if ( !logger.log_file ) return; | ||
60 | |||
61 | time_t tim = time(NULL); | ||
62 | |||
63 | logger_write(ERROR, "============== Closing logger ==============\n" | ||
64 | "Time: %s", asctime(localtime(&tim))); | ||
65 | |||
66 | fclose(logger.log_file); | ||
67 | } | ||
68 | |||
69 | unsigned logger_get_pid() | ||
70 | { | ||
71 | return | ||
72 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
73 | GetCurrentProcessId(); | ||
74 | #else | ||
75 | getpid(); | ||
76 | #endif | ||
77 | } | ||
78 | |||
79 | const char* logger_stringify_level(LoggerLevel level) | ||
80 | { | ||
81 | static const char* strings [] = | ||
82 | { | ||
83 | "INFO", | ||
84 | "DEBUG", | ||
85 | "WARNING", | ||
86 | "ERROR" | ||
87 | }; | ||
88 | |||
89 | return strings[level]; | ||
90 | } | ||
91 | |||
92 | |||
93 | int logger_init(const char* file_name, LoggerLevel level) | ||
94 | { | ||
95 | char* final_l = calloc(sizeof(char), strlen(file_name) + 32); | ||
96 | sprintf(final_l, "%s"/*.%u"*/, file_name, logger_get_pid()); | ||
97 | |||
98 | if ( logger.log_file ) { | ||
99 | fprintf(stderr, "Error opening logger name: %s with level %d: already opened!\n", final_l, level); | ||
100 | free (final_l); | ||
101 | return -1; | ||
102 | } | ||
103 | |||
104 | logger.log_file = fopen(final_l, "wb"); | ||
105 | |||
106 | if ( logger.log_file == NULL ) { | ||
107 | char error[1000]; | ||
108 | if ( strerror_r(errno, error, 1000) == 0 ) | ||
109 | fprintf(stderr, "Error opening logger file: %s; info: %s\n", final_l, error); | ||
110 | else | ||
111 | fprintf(stderr, "Error opening logger file: %s\n", final_l); | ||
112 | |||
113 | free (final_l); | ||
114 | return -1; | ||
115 | } | ||
116 | |||
117 | |||
118 | logger.level = level; | ||
119 | logger.start_time = current_time(); | ||
120 | |||
121 | |||
122 | time_t tim = time(NULL); | ||
123 | logger_write(ERROR, "============== Starting logger ==============\n" | ||
124 | "Time: %s", asctime(localtime(&tim))); | ||
125 | |||
126 | |||
127 | |||
128 | free (final_l); | ||
129 | return 0; | ||
130 | } | ||
131 | |||
132 | |||
133 | void logger_write (LoggerLevel level, const char* format, ...) | ||
134 | { | ||
135 | if (logger.log_file == NULL) { | ||
136 | /*fprintf(stderr, "Logger file is NULL!\n");*/ | ||
137 | return; | ||
138 | } | ||
139 | |||
140 | if (logger.level > level) return; /* Don't print some levels xuh */ | ||
141 | |||
142 | va_list _arg; | ||
143 | va_start (_arg, format); | ||
144 | vfprintf (logger.log_file, format, _arg); | ||
145 | va_end (_arg); | ||
146 | |||
147 | fflush(logger.log_file); | ||
148 | } | ||
149 | |||
150 | char* logger_timestr(char* dest) | ||
151 | { | ||
152 | uint64_t diff = (current_time() - logger.start_time) / 1000; /* ms */ | ||
153 | sprintf(dest, "%"PRIu64"", diff); | ||
154 | |||
155 | return dest; | ||
156 | } | ||
157 | |||
158 | |||
159 | #endif /* LOGGING */ \ No newline at end of file | ||