put date col on left as I did not find a way to correctly handle the headline col...
[prss.git] / src / log.c
1 /*
2  * Copyright (C) 2010-2013 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 512
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_INFO && (!file || 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 && lvl <= log_level) {
86                 fprintf(file,
87                         "[%ld.%06ld] %s %s\n",
88                         tv.tv_sec,
89                         tv.tv_usec,
90                         lvl_str,
91                         buffer);
92                 fflush(file);
93         }
94
95         if (lvl <= LOG_INFO) {
96                 if (lvl == LOG_WARN || lvl == LOG_ERR)
97                         stdf = stderr;
98                 else
99                         stdf = stdout;
100
101                 fprintf(stdf,
102                         "[%ld.%06ld] %s %s\n",
103                         tv.tv_sec,
104                         tv.tv_usec,
105                         lvl_str,
106                         buffer);
107         }
108 }
109
110 void log_printf(int lvl, const char *fmt, ...)
111 {
112         va_list ap;
113
114         va_start(ap, fmt);
115         vlogf(lvl, fmt, ap);
116         va_end(ap);
117 }
118
119 void log_debug(const char *fmt, ...)
120 {
121         va_list ap;
122
123         if (log_level < LOG_DEBUG)
124                 return ;
125
126         va_start(ap, fmt);
127         vlogf(LOG_DEBUG, fmt, ap);
128         va_end(ap);
129 }
130
131 void log_err(const char *fmt, ...)
132 {
133         va_list ap;
134
135         va_start(ap, fmt);
136         vlogf(LOG_ERR, fmt, ap);
137         va_end(ap);
138 }
139
140 void log_warn(const char *fmt, ...)
141 {
142         va_list ap;
143
144         va_start(ap, fmt);
145         vlogf(LOG_WARN, fmt, ap);
146         va_end(ap);
147 }
148
149 void log_info(const char *fmt, ...)
150 {
151         va_list ap;
152
153         va_start(ap, fmt);
154         vlogf(LOG_INFO, fmt, ap);
155         va_end(ap);
156 }