/* * Copyright (C) 2013 Red Hat, Inc. * Authors: * Thomas Woerner * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include "fwlog.h" #include #include #include #include #include int fwlog_level_info; int fwlog_level_debug; char *fwlog_priorities[] = { "FATAL", "ERROR", "WARNING", "INFO", "DEBUG", NULL }; void fwlog_init(int info_level, int debug_level) { g_assert(info_level >= FWLOG_NO_INFO); g_assert(info_level <= FWLOG_INFO_MAX); g_assert(debug_level >= FWLOG_NO_DEBUG); g_assert(debug_level <= FWLOG_DEBUG_MAX); fwlog_level_info = info_level; fwlog_level_debug = debug_level; } void fwlog_set_info_level(int level) { g_assert(level >= FWLOG_NO_INFO); g_assert(level <= FWLOG_INFO_MAX); fwlog_level_info = level; } void fwlog_set_debug_level(int level) { g_assert(level >= FWLOG_NO_DEBUG); g_assert(level <= FWLOG_DEBUG_MAX); fwlog_level_debug = level; } int fwlog_log(int priority, int level, const char *format, ...) { char buffer[LINE_MAX]; time_t t; struct tm *timep; size_t used=0; va_list ap; size_t res=0; /* date */ t = time(NULL); timep = localtime(&t); if (timep != NULL) { res = strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S ", timep); } used += res; /* proiority */ if (priority == FWLOG_INFO || priority == FWLOG_DEBUG) res = snprintf(buffer+used, sizeof(buffer)-used, "%s%d: ", fwlog_priorities[priority], level); else res = snprintf(buffer+used, sizeof(buffer)-used, "%s: ", fwlog_priorities[priority]); used += res; /* format+ap */ va_start(ap, format); res = vsnprintf(buffer+used, sizeof(buffer)-used, format, ap); va_end(ap); used += res; /* newline */ res = snprintf(buffer+used, sizeof(buffer)-used, "\n"); g_print(buffer); /* syslog(priority, buffer); */ return used+res; }