Merge tag 'upstream/1.0.4'
[psensor-pkg-debian.git] / src / lib / plog.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 <plog.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 *fct, 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_current_ISO8601_time();
88         if (!t)
89                 return ;
90
91         if (file && lvl <= log_level) {
92                 if (fct)
93                         fprintf(file,
94                                 "[%s] %s %s(): %s\n", t, lvl_str, fct, buffer);
95                 else
96                         fprintf(file, "[%s] %s %s\n", t, lvl_str, buffer);
97                 fflush(file);
98         } else {
99                 t = NULL;
100         }
101
102         if (lvl <= LOG_INFO) {
103                 if (lvl == LOG_WARN || lvl == LOG_ERR)
104                         stdf = stderr;
105                 else
106                         stdf = stdout;
107
108                 if (fct)
109                         fprintf(file,
110                                 "[%s] %s %s(): %s\n", t, lvl_str, fct, buffer);
111                 else
112                         fprintf(stdf, "[%s] %s %s\n", t, lvl_str, buffer);
113         }
114
115         free(t);
116 }
117
118 void log_printf(int lvl, const char *fmt, ...)
119 {
120         va_list ap;
121
122         va_start(ap, fmt);
123         vlogf(lvl, NULL, fmt, ap);
124         va_end(ap);
125 }
126
127 void log_debug(const char *fmt, ...)
128 {
129         va_list ap;
130
131         if (log_level < LOG_DEBUG)
132                 return ;
133
134         va_start(ap, fmt);
135         vlogf(LOG_DEBUG, NULL, fmt, ap);
136         va_end(ap);
137 }
138
139 void log_err(const char *fmt, ...)
140 {
141         va_list ap;
142
143         va_start(ap, fmt);
144         vlogf(LOG_ERR, NULL, fmt, ap);
145         va_end(ap);
146 }
147
148 void log_warn(const char *fmt, ...)
149 {
150         va_list ap;
151
152         va_start(ap, fmt);
153         vlogf(LOG_WARN, NULL, fmt, ap);
154         va_end(ap);
155 }
156
157 void log_info(const char *fmt, ...)
158 {
159         va_list ap;
160
161         va_start(ap, fmt);
162         vlogf(LOG_INFO, NULL, fmt, ap);
163         va_end(ap);
164 }
165
166 void _log(const char *fct, const char *fmt, ...)
167 {
168         va_list ap;
169
170         va_start(ap, fmt);
171         vlogf(LOG_DEBUG, fct, fmt, ap);
172         va_end(ap);
173 }