2 * Copyright (C) 2010-2012 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
21 #define _(str) gettext(str)
30 int log_level = LOG_WARN;
32 void log_open(const char *path)
34 file = fopen(path, "a");
37 log_printf(LOG_ERR, _("Cannot open log file: %s"), path);
51 #define LOG_BUFFER 4096
52 static void vlogf(int lvl, const char *fmt, va_list ap)
55 char buffer[1 + LOG_BUFFER];
62 vsnprintf(buffer, LOG_BUFFER, fmt, ap);
63 buffer[LOG_BUFFER] = '\0';
65 if (gettimeofday(&tv, NULL) != 0)
86 fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
90 if (lvl == LOG_WARN || lvl == LOG_ERR)
95 fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
98 void log_printf(int lvl, const char *fmt, ...)
107 void log_debug(const char *fmt, ...)
111 if (log_level < LOG_DEBUG)
115 vlogf(LOG_DEBUG, fmt, ap);
119 void log_err(const char *fmt, ...)
124 vlogf(LOG_ERR, fmt, ap);
128 void log_warn(const char *fmt, ...)
133 vlogf(LOG_WARN, fmt, ap);