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