Files
lcdproc/shared/report.c
T

130 lines
2.8 KiB
C
Raw Normal View History

2010-02-02 23:20:29 +00:00
/** \file shared/report.c
* Contains reporting functions.
*/
/*-
* This file is part of LCDproc.
2001-12-30 00:15:25 +00:00
*
* This file is released under the GNU General Public License. Refer to the
* COPYING file distributed with this package.
*
2011-01-05 23:34:37 +00:00
* Copyright (c) 1999, William Ferrell, Selene Scriven
2001-12-30 00:15:25 +00:00
* 2001, Joris Robijn
* 2005, Peter Marschall
2001-11-29 15:15:14 +00:00
*/
#include <stdlib.h>
2001-11-29 15:15:14 +00:00
#include <stdarg.h>
#include <stdio.h>
2001-11-30 15:31:04 +00:00
#include <string.h>
#include <syslog.h>
2001-11-29 15:15:14 +00:00
2010-02-02 23:20:29 +00:00
#include "report.h"
static int report_level = RPT_INFO;
static int report_dest = RPT_DEST_STORE;
2001-11-29 15:15:14 +00:00
#define MAX_STORED_MSGS 200
static char *stored_msgs[MAX_STORED_MSGS];
static int stored_levels[MAX_STORED_MSGS];
static int num_stored_msgs = 0;
/* local functions */
static void store_report_message(int level, const char *message);
2001-11-29 15:15:14 +00:00
static void flush_messages();
void
report(const int level, const char *format,... /* args */ )
2001-11-29 15:15:14 +00:00
{
/* Check if we should report it */
if (level <= report_level || report_dest == RPT_DEST_STORE) {
char buf[1024];
2001-11-29 15:15:14 +00:00
/*
* Following functions appear to work on RedHat and Debian
* Linux, FreeBSD and Solaris
*/
2001-11-29 15:15:14 +00:00
va_list ap;
va_start(ap, format);
2001-11-29 15:15:14 +00:00
switch (report_dest) {
case RPT_DEST_STDERR:
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
2001-11-29 15:15:14 +00:00
break;
case RPT_DEST_SYSLOG:
vsyslog(LOG_USER | (level + 2), format, ap);
2001-11-29 15:15:14 +00:00
break;
case RPT_DEST_STORE:
vsnprintf(buf, sizeof(buf), format, ap);
buf[sizeof(buf) - 1] = 0;
store_report_message(level, buf);
2001-11-29 15:15:14 +00:00
break;
}
va_end(ap);
}
}
int
set_reporting(char *application_name, int new_level, int new_dest)
2001-11-29 15:15:14 +00:00
{
if (new_level < RPT_CRIT || new_level > RPT_DEBUG) {
report(RPT_ERR, "report level invalid: %d", new_level);
2001-11-29 15:15:14 +00:00
return -1;
}
if (report_dest != RPT_DEST_SYSLOG && new_dest == RPT_DEST_SYSLOG) {
openlog(application_name, 0, LOG_USER);
2001-11-29 15:15:14 +00:00
}
else if (report_dest == RPT_DEST_SYSLOG && new_dest != RPT_DEST_SYSLOG) {
2001-11-29 15:15:14 +00:00
closelog();
}
report_level = new_level;
report_dest = new_dest;
/*
* Flush all messages currently in the message store if the new
* destination is not the store itself.
*/
if (report_dest != RPT_DEST_STORE)
2001-11-29 15:15:14 +00:00
flush_messages();
return 0;
}
/**
* Puts a message into the message store. If the store is full new messages
* are silently discarded.
*/
static void
store_report_message(int level, const char *message)
2001-11-29 15:15:14 +00:00
{
if (num_stored_msgs < MAX_STORED_MSGS) {
stored_msgs[num_stored_msgs] = malloc(strlen(message) + 1);
strcpy(stored_msgs[num_stored_msgs], message);
2002-02-21 22:50:12 +00:00
stored_levels[num_stored_msgs] = level;
num_stored_msgs++;
2002-02-21 22:50:12 +00:00
}
2001-11-29 15:15:14 +00:00
}
/**
* Report all messages contained in the message store to the current report
* destination and release their memory.
*/
static void
flush_messages()
2001-11-29 15:15:14 +00:00
{
int i;
for (i = 0; i < num_stored_msgs; i++) {
report(stored_levels[i], "%s", stored_msgs[i]);
free(stored_msgs[i]);
2001-11-29 15:15:14 +00:00
}
num_stored_msgs = 0;
}