fixed style
[ppastats.git] / src / log.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <sys/time.h>
26
27 #include "log.h"
28
29 static FILE *file;
30 int log_level =  LOG_WARN;
31
32 void log_open(const char *path)
33 {
34         file = fopen(path, "a");
35
36         if (!file)
37                 log_printf(LOG_ERR, _("Cannot open log file: %s"), path);
38 }
39
40 void log_close()
41 {
42         if (!file)
43                 return ;
44
45         fclose(file);
46
47         file = NULL;
48 }
49
50
51 #define LOG_BUFFER 4096
52 static void vlogf(int lvl, const char *fmt, va_list ap)
53 {
54         struct timeval tv;
55         char buffer[1 + LOG_BUFFER];
56         char *lvl_str;
57         FILE *stdf;
58
59         if (lvl > log_level)
60                 return ;
61
62         vsnprintf(buffer, LOG_BUFFER, fmt, ap);
63         buffer[LOG_BUFFER] = '\0';
64
65         if (gettimeofday(&tv, NULL) != 0)
66                 timerclear(&tv);
67
68         switch (lvl) {
69         case LOG_WARN:
70                 lvl_str = "[WARN]";
71                 break;
72         case LOG_ERR:
73                 lvl_str = "[ERR]";
74                 break;
75         case LOG_DEBUG:
76                 lvl_str = "[DEBUG]";
77                 break;
78         case LOG_INFO:
79                 lvl_str = "[INFO]";
80                 break;
81         default:
82                 lvl_str = "[??]";
83         }
84
85         if (file) {
86                 fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
87                 fflush(file);
88         }
89
90         if (lvl == LOG_WARN || lvl == LOG_ERR)
91                 stdf = stderr;
92         else
93                 stdf = stdout;
94
95         fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
96         fflush(stdf);
97 }
98
99 void log_printf(int lvl, const char *fmt, ...)
100 {
101         va_list ap;
102
103         va_start(ap, fmt);
104         vlogf(lvl, fmt, ap);
105         va_end(ap);
106 }
107
108 void log_debug(const char *fmt, ...)
109 {
110         va_list ap;
111
112         if (log_level < LOG_DEBUG)
113                 return ;
114
115         va_start(ap, fmt);
116         vlogf(LOG_DEBUG, fmt, ap);
117         va_end(ap);
118 }
119
120 void log_err(const char *fmt, ...)
121 {
122         va_list ap;
123
124         va_start(ap, fmt);
125         vlogf(LOG_ERR, fmt, ap);
126         va_end(ap);
127 }
128
129 void log_warn(const char *fmt, ...)
130 {
131         va_list ap;
132
133         va_start(ap, fmt);
134         vlogf(LOG_WARN, fmt, ap);
135         va_end(ap);
136 }