reintegrate source code of 0.8.0.6 v2
[psensor-pkg-debian.git] / src / lib / 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 #define _LARGEFILE_SOURCE 1
20 #include "config.h"
21
22 #include <locale.h>
23 #include <libintl.h>
24 #define _(str) gettext(str)
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30 #include <time.h>
31
32 #include "log.h"
33 #include "ptime.h"
34
35 static FILE *file;
36 int log_level =  LOG_WARN;
37
38 void log_open(const char *path)
39 {
40         file = fopen(path, "a");
41
42         if (!file)
43                 log_printf(LOG_ERR, _("Cannot open log file: %s"), path);
44 }
45
46 void log_close()
47 {
48         if (!file)
49                 return ;
50
51         fclose(file);
52
53         file = NULL;
54 }
55
56
57 #define LOG_BUFFER 4096
58 static void vlogf(int lvl, const char *fmt, va_list ap)
59 {
60         char buffer[1 + LOG_BUFFER];
61         char *lvl_str, *t;
62         FILE *stdf;
63
64         if (lvl > LOG_INFO && (!file || lvl > log_level))
65                 return ;
66
67         vsnprintf(buffer, LOG_BUFFER, fmt, ap);
68         buffer[LOG_BUFFER] = '\0';
69
70         switch (lvl) {
71         case LOG_WARN:
72                 lvl_str = "[WARN]";
73                 break;
74         case LOG_ERR:
75                 lvl_str = "[ERR]";
76                 break;
77         case LOG_DEBUG:
78                 lvl_str = "[DEBUG]";
79                 break;
80         case LOG_INFO:
81                 lvl_str = "[INFO]";
82                 break;
83         default:
84                 lvl_str = "[??]";
85         }
86
87         t = get_time_str();
88         if (!t)
89                 return ;
90
91         if (file && lvl <= log_level) {
92                 fprintf(file, "[%s] %s %s\n", t, lvl_str, buffer);
93                 fflush(file);
94         } else {
95                 t = NULL;
96         }
97
98         if (lvl <= LOG_INFO) {
99                 if (lvl == LOG_WARN || lvl == LOG_ERR)
100                         stdf = stderr;
101                 else
102                         stdf = stdout;
103
104
105                 fprintf(stdf, "[%s] %s %s\n", t, lvl_str, buffer);
106         }
107
108         free(t);
109 }
110
111 void log_printf(int lvl, const char *fmt, ...)
112 {
113         va_list ap;
114
115         va_start(ap, fmt);
116         vlogf(lvl, fmt, ap);
117         va_end(ap);
118 }
119
120 void log_debug(const char *fmt, ...)
121 {
122         va_list ap;
123
124         if (log_level < LOG_DEBUG)
125                 return ;
126
127         va_start(ap, fmt);
128         vlogf(LOG_DEBUG, fmt, ap);
129         va_end(ap);
130 }
131
132 void log_err(const char *fmt, ...)
133 {
134         va_list ap;
135
136         va_start(ap, fmt);
137         vlogf(LOG_ERR, fmt, ap);
138         va_end(ap);
139 }
140
141 void log_warn(const char *fmt, ...)
142 {
143         va_list ap;
144
145         va_start(ap, fmt);
146         vlogf(LOG_WARN, fmt, ap);
147         va_end(ap);
148 }
149
150 void log_info(const char *fmt, ...)
151 {
152         va_list ap;
153
154         va_start(ap, fmt);
155         vlogf(LOG_INFO, fmt, ap);
156         va_end(ap);
157 }