2 * Copyright (C) 2010-2014 jeanfi@gmail.com
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.
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.
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
19 #define _LARGEFILE_SOURCE 1
24 #define _(str) gettext(str)
36 int log_level = LOG_WARN;
38 void log_open(const char *path)
40 file = fopen(path, "a");
43 log_printf(LOG_ERR, _("Cannot open log file: %s"), path);
57 #define LOG_BUFFER 4096
58 static void vlogf(int lvl, const char *fmt, va_list ap)
60 char buffer[1 + LOG_BUFFER];
64 if (lvl > LOG_INFO && (!file || lvl > log_level))
67 vsnprintf(buffer, LOG_BUFFER, fmt, ap);
68 buffer[LOG_BUFFER] = '\0';
87 t = get_current_ISO8601_time();
91 if (file && lvl <= log_level) {
92 fprintf(file, "[%s] %s %s\n", t, lvl_str, buffer);
98 if (lvl <= LOG_INFO) {
99 if (lvl == LOG_WARN || lvl == LOG_ERR)
105 fprintf(stdf, "[%s] %s %s\n", t, lvl_str, buffer);
111 void log_printf(int lvl, const char *fmt, ...)
120 void log_debug(const char *fmt, ...)
124 if (log_level < LOG_DEBUG)
128 vlogf(LOG_DEBUG, fmt, ap);
132 void log_err(const char *fmt, ...)
137 vlogf(LOG_ERR, fmt, ap);
141 void log_warn(const char *fmt, ...)
146 vlogf(LOG_WARN, fmt, ap);
150 void log_info(const char *fmt, ...)
155 vlogf(LOG_INFO, fmt, ap);