134 lines
4.1 KiB
C
134 lines
4.1 KiB
C
|
|
/*
|
||
|
|
Modifications copyright (c) 2013 IOnU Security Inc. All rights reserved
|
||
|
|
Added to IonU source repository in August 2013 by Kendrick Webster,
|
||
|
|
based on public-domain code by Kendrick Webster.
|
||
|
|
-----------------------------------------------------------------------------
|
||
|
|
Log trace message writer 2011 - 2013
|
||
|
|
|
||
|
|
Written by Ken Webster <ken@kenwebster.org>
|
||
|
|
[ other contributers can add their names here ]
|
||
|
|
|
||
|
|
To the extent possible under law, the author(s) have dedicated all copyright
|
||
|
|
and related and neighboring rights to this software to the public domain
|
||
|
|
worldwide. This software is distributed without any warranty.
|
||
|
|
|
||
|
|
See the CC0 Public Domain Dedication for details:
|
||
|
|
<http://creativecommons.org/publicdomain/zero/1.0/>
|
||
|
|
-----------------------------------------------------------------------------
|
||
|
|
Description
|
||
|
|
|
||
|
|
This module defines and implements a simple platform-agnostic API suitable
|
||
|
|
for producing log and trace messages in code that may be multi-threaded and
|
||
|
|
timing-sensitive. Messages are sent to UDP port 514 on the local host by
|
||
|
|
default, but they can easily be sent elsewhere by editing this module's
|
||
|
|
implementation. Meta data for code execution tracing is automatically added
|
||
|
|
(source file name, line number, elapsed time).
|
||
|
|
-----------------------------------------------------------------------------
|
||
|
|
Usage
|
||
|
|
|
||
|
|
A global function-like macro named 'logprintf' is used like the standard
|
||
|
|
library printf() function except that it is not line-buffered. Each call
|
||
|
|
corresponds to one message, like the Unix/Linux syslog function. Its
|
||
|
|
first argument specifies the severity level:
|
||
|
|
|
||
|
|
Log::
|
||
|
|
emergency
|
||
|
|
alert
|
||
|
|
critical
|
||
|
|
error
|
||
|
|
warning
|
||
|
|
notice
|
||
|
|
informational
|
||
|
|
debug (default)
|
||
|
|
debug2
|
||
|
|
debug3
|
||
|
|
|
||
|
|
The first 8 levels correspond to syslog severity levels. This may be useful
|
||
|
|
for interoperability with syslog or for re-using code that uses syslog.
|
||
|
|
---------------------------------------------------------------------------*/
|
||
|
|
#pragma once
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
//-------------------------------------------------------------------------
|
||
|
|
// Severity levels
|
||
|
|
#define LOG_TRACE_SEVERITY_LEVELS \
|
||
|
|
X(emergency, EMG) \
|
||
|
|
X(alert, ALR) \
|
||
|
|
X(critical, CRI) \
|
||
|
|
X(error, ERR) \
|
||
|
|
X(warning, WRN) \
|
||
|
|
X(notice, NTC) \
|
||
|
|
X(informational, INF) \
|
||
|
|
X(debug, DBG) \
|
||
|
|
X(debug2, DB2) \
|
||
|
|
X(debug3, DB3) \
|
||
|
|
|
||
|
|
namespace Log
|
||
|
|
{
|
||
|
|
enum SeverityLevels
|
||
|
|
{
|
||
|
|
#define X(level, abbrev) \
|
||
|
|
level,
|
||
|
|
LOG_TRACE_SEVERITY_LEVELS
|
||
|
|
#undef X
|
||
|
|
};
|
||
|
|
typedef enum SeverityLevels severity_t;
|
||
|
|
|
||
|
|
extern severity_t FilterLevel;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//-------------------------------------------------------------------------
|
||
|
|
// logprintf() is called like printf(), but adding <severity> as 1st arg
|
||
|
|
//
|
||
|
|
// CAUTION: function argument expressions must not have side-effects
|
||
|
|
// -- evaluation is skipped if severity is less than the filter level
|
||
|
|
//
|
||
|
|
#define logprintf(severity, ...) \
|
||
|
|
do {if (severity <= Log::FilterLevel) logprintf_1(__FILE__, __LINE__, severity, __VA_ARGS__);} while(0)
|
||
|
|
|
||
|
|
void logprintf_1(
|
||
|
|
const char * source_file,
|
||
|
|
unsigned int source_line,
|
||
|
|
Log::severity_t severity,
|
||
|
|
const char * format,
|
||
|
|
...);
|
||
|
|
|
||
|
|
//-------------------------------------------------------------------------
|
||
|
|
// Hex dump
|
||
|
|
//
|
||
|
|
// CAUTION: function argument expressions must not have side-effects
|
||
|
|
// -- evaluation is skipped if severity is less than the filter level
|
||
|
|
//
|
||
|
|
#define loghexdump(severity, ...) \
|
||
|
|
do {if (severity <= Log::FilterLevel) loghexdump_1(__FILE__, __LINE__, severity, __VA_ARGS__);} while(0)
|
||
|
|
|
||
|
|
void loghexdump_1(
|
||
|
|
const char * source_file,
|
||
|
|
unsigned int source_line,
|
||
|
|
Log::severity_t severity,
|
||
|
|
const void * d,
|
||
|
|
size_t n);
|
||
|
|
|
||
|
|
|
||
|
|
// ---------- Log initialization and cleanup ----------
|
||
|
|
namespace Log
|
||
|
|
{
|
||
|
|
typedef struct
|
||
|
|
{
|
||
|
|
bool echoToStdout;
|
||
|
|
bool echoToSyslog;
|
||
|
|
bool echoToUDP514;
|
||
|
|
severity_t filterLevel; // messages with (severity > level) are dropped
|
||
|
|
bool logToFile;
|
||
|
|
std::string file;
|
||
|
|
unsigned int nFilesMax;
|
||
|
|
size_t nBytesPerFileMax;
|
||
|
|
std::string shortIdentifier;
|
||
|
|
}
|
||
|
|
config_t;
|
||
|
|
void Initialize(config_t* config);
|
||
|
|
void Finalize(void);
|
||
|
|
}
|