summaryrefslogtreecommitdiff
path: root/toxcore/logger.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/logger.c')
-rw-r--r--toxcore/logger.c106
1 files changed, 54 insertions, 52 deletions
diff --git a/toxcore/logger.c b/toxcore/logger.c
index 21ff81c6..fa9d1760 100644
--- a/toxcore/logger.c
+++ b/toxcore/logger.c
@@ -46,20 +46,20 @@ typedef struct logger {
46 FILE *log_file; 46 FILE *log_file;
47 LOG_LEVEL level; 47 LOG_LEVEL level;
48 uint64_t start_time; /* Time when lib loaded */ 48 uint64_t start_time; /* Time when lib loaded */
49 char* id; 49 char *id;
50 50
51 /* Allocate these once */ 51 /* Allocate these once */
52 char* tstr; 52 char *tstr;
53 char* posstr; 53 char *posstr;
54 char* msg; 54 char *msg;
55 55
56 /* For thread synchronisation */ 56 /* For thread synchronisation */
57 pthread_mutex_t mutex[1]; 57 pthread_mutex_t mutex[1];
58} logger; 58} logger;
59 59
60logger* global = NULL; 60logger *global = NULL;
61 61
62const char* LOG_LEVEL_STR [] = { 62const char *LOG_LEVEL_STR [] = {
63 [LOG_TRACE] = "TRACE", 63 [LOG_TRACE] = "TRACE",
64 [LOG_DEBUG] = "DEBUG", 64 [LOG_DEBUG] = "DEBUG",
65 [LOG_INFO] = "INFO" , 65 [LOG_INFO] = "INFO" ,
@@ -67,14 +67,14 @@ const char* LOG_LEVEL_STR [] = {
67 [LOG_ERROR] = "ERROR", 67 [LOG_ERROR] = "ERROR",
68}; 68};
69 69
70char* strtime(char* dest, size_t max_len) 70char *strtime(char *dest, size_t max_len)
71{ 71{
72 time_t timer; 72 time_t timer;
73 struct tm *tm_info; 73 struct tm *tm_info;
74 74
75 time(&timer); 75 time(&timer);
76 tm_info = localtime(&timer); 76 tm_info = localtime(&timer);
77 77
78 strftime(dest, max_len, "%m:%d %H:%M:%S", tm_info); 78 strftime(dest, max_len, "%m:%d %H:%M:%S", tm_info);
79 return dest; 79 return dest;
80} 80}
@@ -83,22 +83,22 @@ char* strtime(char* dest, size_t max_len)
83/** 83/**
84 * Public Functions 84 * Public Functions
85 */ 85 */
86logger* logger_new (const char *file_name, LOG_LEVEL level, const char* id) 86logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id)
87{ 87{
88#ifndef LOGGING /* Disabled */ 88#ifndef LOGGING /* Disabled */
89 return NULL; 89 return NULL;
90#endif 90#endif
91 91
92 logger* retu = calloc(1, sizeof(logger)); 92 logger *retu = calloc(1, sizeof(logger));
93 93
94 if (!retu) 94 if (!retu)
95 return NULL; 95 return NULL;
96 96
97 if ( pthread_mutex_init(retu->mutex, NULL) != 0 ) { 97 if ( pthread_mutex_init(retu->mutex, NULL) != 0 ) {
98 free(retu); 98 free(retu);
99 return NULL; 99 return NULL;
100 } 100 }
101 101
102 if (!(retu->log_file = fopen(file_name, "ab"))) { 102 if (!(retu->log_file = fopen(file_name, "ab"))) {
103 fprintf(stderr, "Error opening logger file: %s; info: %s\n", file_name, strerror(errno)); 103 fprintf(stderr, "Error opening logger file: %s; info: %s\n", file_name, strerror(errno));
104 free(retu); 104 free(retu);
@@ -107,30 +107,30 @@ logger* logger_new (const char *file_name, LOG_LEVEL level, const char* id)
107 } 107 }
108 108
109 if (!(retu->tstr = calloc(16, sizeof (char))) || 109 if (!(retu->tstr = calloc(16, sizeof (char))) ||
110 !(retu->posstr = calloc(300, sizeof (char))) || 110 !(retu->posstr = calloc(300, sizeof (char))) ||
111 !(retu->msg = calloc(4096, sizeof (char))) ) 111 !(retu->msg = calloc(4096, sizeof (char))) )
112 goto ERROR; 112 goto ERROR;
113 113
114 if (id) { 114 if (id) {
115 if (!(retu->id = calloc(strlen(id) + 1, 1))) 115 if (!(retu->id = calloc(strlen(id) + 1, 1)))
116 goto ERROR; 116 goto ERROR;
117 117
118 strcpy(retu->id, id); 118 strcpy(retu->id, id);
119 } else { 119 } else {
120 if (!(retu->id = malloc(8))) 120 if (!(retu->id = malloc(8)))
121 goto ERROR; 121 goto ERROR;
122 122
123 snprintf(retu->id, 8, "%u", random_int()); 123 snprintf(retu->id, 8, "%u", random_int());
124 } 124 }
125 125
126 retu->level = level; 126 retu->level = level;
127 retu->start_time = current_time_monotonic(); 127 retu->start_time = current_time_monotonic();
128 128
129 fprintf(retu->log_file, "Successfully created and running logger id: %s; time: %s\n", 129 fprintf(retu->log_file, "Successfully created and running logger id: %s; time: %s\n",
130 retu->id, strtime(retu->tstr, 16)); 130 retu->id, strtime(retu->tstr, 16));
131 131
132 return retu; 132 return retu;
133 133
134ERROR: 134ERROR:
135 fprintf(stderr, "Failed to create logger!\n"); 135 fprintf(stderr, "Failed to create logger!\n");
136 pthread_mutex_destroy(retu->mutex); 136 pthread_mutex_destroy(retu->mutex);
@@ -143,25 +143,27 @@ ERROR:
143 return NULL; 143 return NULL;
144} 144}
145 145
146void logger_kill(logger* log) 146void logger_kill(logger *log)
147{ 147{
148#ifndef LOGGING /* Disabled */ 148#ifndef LOGGING /* Disabled */
149 return; 149 return;
150#endif 150#endif
151 151
152 if (!log) 152 if (!log)
153 return; 153 return;
154 154
155 pthread_mutex_lock(log->mutex); 155 pthread_mutex_lock(log->mutex);
156 free(log->id); 156 free(log->id);
157 free(log->tstr); 157 free(log->tstr);
158 free(log->posstr); 158 free(log->posstr);
159 free(log->msg); 159 free(log->msg);
160
160 if (fclose(log->log_file) != 0 ) 161 if (fclose(log->log_file) != 0 )
161 perror("Could not close log file"); 162 perror("Could not close log file");
163
162 pthread_mutex_unlock(log->mutex); 164 pthread_mutex_unlock(log->mutex);
163 pthread_mutex_destroy(log->mutex); 165 pthread_mutex_destroy(log->mutex);
164 166
165 free(log); 167 free(log);
166} 168}
167 169
@@ -170,31 +172,31 @@ void logger_kill_global(void)
170 logger_kill(global); 172 logger_kill(global);
171} 173}
172 174
173void logger_set_global(logger* log) 175void logger_set_global(logger *log)
174{ 176{
175 #ifndef LOGGING /* Disabled */ 177#ifndef LOGGING /* Disabled */
176 return; 178 return;
177 #endif 179#endif
178 180
179 global = log; 181 global = log;
180} 182}
181 183
182logger* logger_get_global(void) 184logger *logger_get_global(void)
183{ 185{
184 #ifndef LOGGING /* Disabled */ 186#ifndef LOGGING /* Disabled */
185 return NULL; 187 return NULL;
186 #endif 188#endif
187 189
188 return global; 190 return global;
189} 191}
190 192
191void logger_write (logger* log, LOG_LEVEL level, const char* file, int line, const char *format, ...) 193void logger_write (logger *log, LOG_LEVEL level, const char *file, int line, const char *format, ...)
192{ 194{
193#ifndef LOGGING /* Disabled */ 195#ifndef LOGGING /* Disabled */
194 return; 196 return;
195#endif 197#endif
196 198
197 static const char* logger_format = 199 static const char *logger_format =
198 "%s " /* Logger id string */ 200 "%s " /* Logger id string */
199 "%-16s" /* Time string of format: %m:%d %H:%M:%S */ 201 "%-16s" /* Time string of format: %m:%d %H:%M:%S */
200 "%u " /* Thread id */ 202 "%u " /* Thread id */
@@ -202,23 +204,23 @@ void logger_write (logger* log, LOG_LEVEL level, const char* file, int line, con
202 "%-20s " /* File:line string */ 204 "%-20s " /* File:line string */
203 "- %s" /* Output message */ 205 "- %s" /* Output message */
204 "\n"; /* Every new print new line */ 206 "\n"; /* Every new print new line */
205 207
206 208
207 logger* this_log = log ? log: global; 209 logger *this_log = log ? log : global;
208 210
209 if (!this_log) 211 if (!this_log)
210 return; 212 return;
211 213
212 /* Don't print levels lesser than set one */ 214 /* Don't print levels lesser than set one */
213 if (this_log->level > level) 215 if (this_log->level > level)
214 return; 216 return;
215 217
216 pthread_mutex_lock(this_log->mutex); 218 pthread_mutex_lock(this_log->mutex);
217 219
218 /* Set position str */ 220 /* Set position str */
219 snprintf(this_log->posstr, 300, "%s:%d", SFILE(file), line); 221 snprintf(this_log->posstr, 300, "%s:%d", SFILE(file), line);
220 222
221 /* Set message */ 223 /* Set message */
222 va_list args; 224 va_list args;
223 va_start (args, format); 225 va_start (args, format);
224 vsnprintf(this_log->msg, 4096, format, args); 226 vsnprintf(this_log->msg, 4096, format, args);
@@ -227,6 +229,6 @@ void logger_write (logger* log, LOG_LEVEL level, const char* file, int line, con
227 fprintf(this_log->log_file, logger_format, this_log->id, strtime(this_log->tstr, 16), pthread_self(), 229 fprintf(this_log->log_file, logger_format, this_log->id, strtime(this_log->tstr, 16), pthread_self(),
228 LOG_LEVEL_STR[level], this_log->posstr, this_log->msg); 230 LOG_LEVEL_STR[level], this_log->posstr, this_log->msg);
229 fflush(this_log->log_file); 231 fflush(this_log->log_file);
230 232
231 pthread_mutex_unlock(this_log->mutex); 233 pthread_mutex_unlock(this_log->mutex);
232} \ No newline at end of file 234}