Sleds/gluster_status/LogTrace.h

139 lines
4.2 KiB
C++

/*
Modifications copyright (c) 2013, 2014 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
Simple, platform-agnostic API for producing log and trace messages in code
that may be multi-threaded and timing-sensitive. Meta data for code
execution tracing is automatically added (source file name, line number,
elapsed time, etc.)
-----------------------------------------------------------------------------
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 (highest filter level)
ignore (above higest filter level, always ignored)
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 "../libionu/libionu.h"
//-------------------------------------------------------------------------
// 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) \
X(ignore, IGN)
namespace Log
{
typedef enum
{
#define X(level, abbrev) \
level,
LOG_TRACE_SEVERITY_LEVELS
#undef X
}
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,
...);
// variant that saves a copy of the message string to <save_to>
#define logprintf_save_string(save_to, severity, ...) \
do {if (severity <= Log::FilterLevel) logprintf_ss(save_to, __FILE__, __LINE__, severity, __VA_ARGS__);} while(0)
void logprintf_ss(
std::string& save_to,
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);
// ---------- Hook up to LibIonu logging ----------
namespace Log
{
typedef struct
{
bool echoToStdout;
bool echoToUDP514;
severity_t filterLevel; // messages with (severity > level) are dropped
}
k2logconfig_t;
void InitializeIonu(IONU_LOG_CONFIG* config, k2logconfig_t* k2config);
void FinalizeIonu();
}