Forgotten files needed for reporting.
This commit is contained in:
+100
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* REPORTING FUNCTIONS
|
||||
*
|
||||
*/
|
||||
|
||||
#include <malloc.h>
|
||||
#include "report.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <syslog.h>
|
||||
|
||||
int report_level = RPT_INFO;
|
||||
int report_dest = RPT_DEST_STORE;
|
||||
|
||||
#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 );
|
||||
static void flush_messages();
|
||||
|
||||
void report( const int level, const char *format, .../*args*/ )
|
||||
{
|
||||
// Check if we should report it
|
||||
if( level <= report_level || report_dest == RPT_DEST_STORE ) {
|
||||
|
||||
char buf[1024];
|
||||
|
||||
// Following functions appear to work on RedHat and Debian
|
||||
// Linux, FreeBSD and Solaris
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, format); /* measure the required size (the number of elements of format) */
|
||||
|
||||
switch( report_dest ) {
|
||||
case RPT_DEST_STDERR:
|
||||
vfprintf( stderr, format, ap );
|
||||
fprintf( stderr, "\n" );
|
||||
break;
|
||||
case RPT_DEST_SYSLOG:
|
||||
vsyslog( LOG_USER|(level+2), format, ap );
|
||||
break;
|
||||
case RPT_DEST_STORE:
|
||||
vsnprintf( buf, sizeof(buf), format, ap );
|
||||
buf[sizeof(buf)-1] = 0; // be sure to have a terminating 0
|
||||
store_report_message( level, buf );
|
||||
break;
|
||||
}
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int set_reporting( int new_level, int new_dest )
|
||||
{
|
||||
if( new_level < RPT_CRIT || new_level > RPT_DEBUG ) {
|
||||
report( RPT_ERR, "debug level invalid: %d", new_level );
|
||||
return -1;
|
||||
}
|
||||
|
||||
if( report_dest != RPT_DEST_SYSLOG && new_dest == RPT_DEST_SYSLOG ) {
|
||||
openlog( "LCDd", 0, LOG_USER );
|
||||
}
|
||||
else if( report_dest == RPT_DEST_SYSLOG && new_dest != RPT_DEST_SYSLOG ) {
|
||||
closelog();
|
||||
}
|
||||
|
||||
report_level = new_level;
|
||||
report_dest = new_dest;
|
||||
|
||||
if( report_dest != RPT_DEST_STORE )
|
||||
flush_messages();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void store_report_message( int level, const char *message )
|
||||
{
|
||||
stored_msgs[num_stored_msgs] = malloc(strlen( message )+1);
|
||||
strcpy( stored_msgs[num_stored_msgs], message );
|
||||
stored_levels[num_stored_msgs] = level;
|
||||
num_stored_msgs ++;
|
||||
}
|
||||
|
||||
|
||||
static void flush_messages()
|
||||
{
|
||||
int i;
|
||||
for( i=0; i<num_stored_msgs; i++ ) {
|
||||
report( stored_levels[i], "%s", stored_msgs[i] );
|
||||
free( stored_msgs[i] );
|
||||
}
|
||||
num_stored_msgs = 0;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#ifndef DEBUG_H
|
||||
#define DEBUG_H
|
||||
|
||||
/* DEBUGGING
|
||||
To enable the debug() function on all of the software, just type:
|
||||
./configure --enable-debug
|
||||
and recompile with 'make'
|
||||
|
||||
To enable the debug() function only in specific files:
|
||||
1) Configure without enabling debug (that is without --enable-debug)
|
||||
2) Edit the source file that you want to debug and put this:
|
||||
#define DEBUG
|
||||
#include "shared/debug.h"
|
||||
#undef DEBUG
|
||||
Then recompile with 'make'
|
||||
This way, the global DEBUG macro is off but is locally enabled in
|
||||
certains parts of the software.
|
||||
|
||||
The debug levels use the following
|
||||
|
||||
0 RPT_CRIT Critical conditions: the program stops right after this.
|
||||
Only use this if the program is exited from the current
|
||||
function.
|
||||
1 RPT_ERR Error conditions: serious problem, program continues.
|
||||
Use just before you return -1 from a function.
|
||||
2 RPT_WARNING Warning conditions: request user to fix this problem.
|
||||
Ex: What a queer port did you select.
|
||||
3 RPT_NOTICE Normal but significant condition:
|
||||
Ex: What options have been set.
|
||||
4 RPT_INFO Informational
|
||||
Ex: What functions have been called.
|
||||
5 RPT_DEBUG Debug-level messages: further debug messages
|
||||
Ex: what are we going to do in the next few lines of code.
|
||||
|
||||
Levels 4 and 5 should be reported using the debug function. The code
|
||||
that this function generates will not be in the executable when compiled
|
||||
without debugging. This way memory and CPU cycles are saved.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
// Reporting levels
|
||||
#define RPT_CRIT 0
|
||||
#define RPT_ERR 1
|
||||
#define RPT_WARNING 2
|
||||
#define RPT_NOTICE 3
|
||||
#define RPT_INFO 4
|
||||
#define RPT_DEBUG 5
|
||||
#define RPT_DEST_STDERR 0
|
||||
#define RPT_DEST_SYSLOG 1
|
||||
#define RPT_DEST_STORE 2
|
||||
// Don't just modify these numbers, they're related to syslog.
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <syslog.h>
|
||||
|
||||
extern int report_level;
|
||||
extern int report_dest;
|
||||
|
||||
int set_reporting( int new_level, int new_dest );
|
||||
// Sets reporting level and message destination.
|
||||
|
||||
void report( const int level, const char *format, .../*args*/ );
|
||||
// Report the message to the selected destination if important enough
|
||||
|
||||
// Consider the debug function to be exactly the same as the report function.
|
||||
// The only difference is that it is only compiled in if DEBUG is defined.
|
||||
|
||||
static inline void dont_report( const int level, const char *format, .../*args*/ )
|
||||
{} // The idea is that this gets optimized out
|
||||
|
||||
#ifdef DEBUG
|
||||
# define debug report
|
||||
#else
|
||||
# define debug dont_report
|
||||
#endif /*DEBUG*/
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user