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