/* ** LOG-STDERR: Simple minded stderr based message logging module ** Copyright (C) 2002 Michael W. Shaffer ** ** 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 (see the file COPYING). If not, write to: ** ** The Free Software Foundation, Inc. ** 59 Temple Place, Suite 330, ** Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include "conf.h" #include "log.h" struct globals { int log_fd; int log_level; }; static struct globals g; void log_init_stderr (void) { g.log_fd = 2; log_set_level_stderr ((int) get_config_long ("log-level")); return; } void log_free_stderr (void) { g.log_fd = -1; g.log_level = all; return; } void log_set_level_stderr (int level) { g.log_level = level; if (g.log_level < all) g.log_level = all; if (g.log_level > fatal) g.log_level = fatal; return; } void log_msg_stderr (int pri, char *msg) { time_t tt; struct tm *tm = NULL; char timestamp[21]; char mbuf[256]; char wbuf[256]; int pos = 0; if ((g.log_fd < 0) || (!msg) || (pri < g.log_level)) goto EXIT; if (pri < all) pri = all; if (pri > fatal) pri = fatal; memset (mbuf, 0, sizeof (mbuf)); strncpy (mbuf, msg, (sizeof (mbuf) - 1)); pos = strlen (mbuf) - 1; while (pos >= 0) { if ((mbuf[pos] == '\n') || (mbuf[pos] == '\r') || (mbuf[pos] == '\t')) { mbuf[pos] = ' '; } pos--; } tt = time (NULL); tm = localtime (&tt); memset (timestamp, 0, sizeof (timestamp)); strftime (timestamp, (sizeof (timestamp) - 1), "%Y/%m/%d %H:%M:%S", tm); memset (wbuf, 0, sizeof (wbuf)); snprintf (wbuf, (sizeof (wbuf) - 1), "[%s] %s\n", timestamp, mbuf); pos = strlen (wbuf) - 1; if (wbuf[pos] != '\n') wbuf[pos] = '\n'; write (g.log_fd, wbuf, strlen (wbuf)); EXIT: return; }