2 * Copyright (C) 2010-2013 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 512
52 static void vlogf(int lvl, const char *fmt, va_list ap)
55 char buffer[1 + LOG_BUFFER];
59 if (lvl > LOG_INFO && (!file || lvl > log_level))
62 vsnprintf(buffer, LOG_BUFFER, fmt, ap);
63 buffer[LOG_BUFFER] = '\0';
65 if (gettimeofday(&tv, NULL) != 0)
85 if (file && lvl <= log_level) {
87 "[%ld.%06ld] %s %s\n",
95 if (lvl <= LOG_INFO) {
96 if (lvl == LOG_WARN || lvl == LOG_ERR)
102 "[%ld.%06ld] %s %s\n",
110 void log_printf(int lvl, const char *fmt, ...)
119 void log_debug(const char *fmt, ...)
123 if (log_level < LOG_DEBUG)
127 vlogf(LOG_DEBUG, fmt, ap);
131 void log_err(const char *fmt, ...)
136 vlogf(LOG_ERR, fmt, ap);
140 void log_warn(const char *fmt, ...)
145 vlogf(LOG_WARN, fmt, ap);
149 void log_info(const char *fmt, ...)
154 vlogf(LOG_INFO, fmt, ap);