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
19 #define _LARGEFILE_SOURCE 1
24 #define _(str) gettext(str)
33 int log_level = LOG_WARN;
35 void log_open(const char *path)
37 file = fopen(path, "a");
40 log_printf(LOG_ERR, _("Cannot open log file: %s"), path);
54 #define LOG_BUFFER 4096
55 static void vlogf(int lvl, const char *fmt, va_list ap)
58 char buffer[1 + LOG_BUFFER];
62 if (lvl > LOG_INFO && (!file || lvl > log_level))
65 vsnprintf(buffer, LOG_BUFFER, fmt, ap);
66 buffer[LOG_BUFFER] = '\0';
68 if (gettimeofday(&tv, NULL) != 0)
88 if (file && lvl <= log_level) {
89 fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
93 if (lvl <= LOG_INFO) {
94 if (lvl == LOG_WARN || lvl == LOG_ERR)
99 fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer);
103 void log_printf(int lvl, const char *fmt, ...)
112 void log_debug(const char *fmt, ...)
116 if (log_level < LOG_DEBUG)
120 vlogf(LOG_DEBUG, fmt, ap);
124 void log_err(const char *fmt, ...)
129 vlogf(LOG_ERR, fmt, ap);
133 void log_warn(const char *fmt, ...)
138 vlogf(LOG_WARN, fmt, ap);
142 void log_info(const char *fmt, ...)
147 vlogf(LOG_INFO, fmt, ap);