Imported Upstream version 0.6.2.17
[psensor-pkg-debian.git] / src / lib / log.c
1 /*
2  * Copyright (C) 2010-2012 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_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, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
87                 fflush(file);
88         }
89
90         if (lvl <= LOG_INFO) {
91                 if (lvl == LOG_WARN || lvl == LOG_ERR)
92                         stdf = stderr;
93                 else
94                         stdf = stdout;
95
96                 fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
97         }
98 }
99
100 void log_printf(int lvl, const char *fmt, ...)
101 {
102         va_list ap;
103
104         va_start(ap, fmt);
105         vlogf(lvl, fmt, ap);
106         va_end(ap);
107 }
108
109 void log_debug(const char *fmt, ...)
110 {
111         va_list ap;
112
113         if (log_level < LOG_DEBUG)
114                 return ;
115
116         va_start(ap, fmt);
117         vlogf(LOG_DEBUG, fmt, ap);
118         va_end(ap);
119 }