syslog.h
Go to the documentation of this file.
/*
* syslog.h
*
* System Logging class.
*
* Portable Tools Library
*
* Copyright (c) 2009 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Contributor(s): ______________________________________.
*
* $Revision: 24994 $
* $Author: rjongbloed $
* $Date: 2011-01-04 17:13:51 -0600 (Tue, 04 Jan 2011) $
*/
#ifndef _PSYSTEMLOG
#define _PSYSTEMLOG
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include "ptlib/udpsock.h"
class PSystemLogTarget;
class PSystemLog : public PObject, public iostream
{
PCLASSINFO(PSystemLog, PObject);
public:
enum Level {
StdError = -1,
Fatal,
Error,
Warning,
Info,
Debug,
Debug2,
Debug3,
Debug4,
Debug5,
Debug6,
NumLogLevels
};
PSystemLog(
Level level
);
~PSystemLog() { flush(); }
static PSystemLogTarget & GetTarget();
static void SetTarget(
PSystemLogTarget * target,
bool autoDelete = true
);
private:
PSystemLog(const PSystemLog & other);
PSystemLog & operator=(const PSystemLog &);
class Buffer : public streambuf {
public:
Buffer();
virtual int_type overflow(int_type=EOF);
virtual int_type underflow();
virtual int sync();
PSystemLog * m_log;
PString m_string;
} m_buffer;
friend class Buffer;
Level m_logLevel;
friend class PSystemLogTarget;
};
class PSystemLogTarget : public PObject
{
PCLASSINFO(PSystemLogTarget, PObject);
public:
PSystemLogTarget();
void SetThresholdLevel(
PSystemLog::Level level
) { m_thresholdLevel = level; }
PSystemLog::Level GetThresholdLevel() const { return m_thresholdLevel; }
protected:
virtual void Output(
PSystemLog::Level level,
const char * msg
) = 0;
void OutputToStream(
ostream & strm,
PSystemLog::Level level,
const char * msg
);
protected:
PSystemLog::Level m_thresholdLevel;
private:
PSystemLogTarget(const PSystemLogTarget & other);
PSystemLogTarget & operator=(const PSystemLogTarget &);
friend class PSystemLog::Buffer;
};
class PSystemLogToNowhere : public PSystemLogTarget
{
PCLASSINFO(PSystemLogToNowhere, PSystemLogTarget);
public:
virtual void Output(PSystemLog::Level, const char *)
{
}
};
class PSystemLogToStderr : public PSystemLogTarget
{
PCLASSINFO(PSystemLogToStderr, PSystemLogTarget);
public:
virtual void Output(
PSystemLog::Level level,
const char * msg
);
};
class PSystemLogToFile : public PSystemLogTarget
{
PCLASSINFO(PSystemLogToFile, PSystemLogTarget);
public:
PSystemLogToFile(
const PString & filename
);
virtual void Output(
PSystemLog::Level level,
const char * msg
);
const PFilePath & GetFilePath() const { return m_file.GetFilePath(); }
protected:
PTextFile m_file;
};
class PSystemLogToNetwork : public PSystemLogTarget
{
PCLASSINFO(PSystemLogToNetwork, PSystemLogTarget);
public:
enum { RFC3164_Port = 514 };
PSystemLogToNetwork(
const PIPSocket::Address & address,
WORD port = RFC3164_Port,
unsigned facility = 16
);
PSystemLogToNetwork(
const PString & hostname,
WORD port = RFC3164_Port,
unsigned facility = 16
);
virtual void Output(
PSystemLog::Level level,
const char * msg
);
protected:
PIPSocket::Address m_host;
WORD m_port;
unsigned m_facility;
PUDPSocket m_socket;
};
#ifdef WIN32
class PSystemLogToDebug : public PSystemLogTarget
{
PCLASSINFO(PSystemLogToDebug, PSystemLogTarget);
public:
virtual void Output(
PSystemLog::Level level,
const char * msg
);
};
#elif !defined(P_VXWORKS)
class PSystemLogToSyslog : public PSystemLogTarget
{
PCLASSINFO(PSystemLogToSyslog, PSystemLogTarget);
public:
PSystemLogToSyslog();
~PSystemLogToSyslog();
virtual void Output(
PSystemLog::Level level,
const char * msg
);
};
#endif
#define PSYSTEMLOG(level, variables) \
if (PSystemLog::GetTarget().GetThresholdLevel() >= PSystemLog::level) { \
PSystemLog P_systemlog(PSystemLog::level); \
P_systemlog << variables; \
} else (void)0
#endif
// End Of File ///////////////////////////////////////////////////////////////
PSystemLog::Error
Log a non-fatal error.
Definition: syslog.h:60
PSystemLog::PSystemLogTarget
friend class PSystemLogTarget
Definition: syslog.h:121
PSystemLog::Warning
Log a warning.
Definition: syslog.h:62
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PSystemLog
This class abstracts the operating system dependent error logging facility.
Definition: syslog.h:47
PSystemLogToFile::GetFilePath
const PFilePath & GetFilePath() const
Get the path to the file being logged to.
Definition: syslog.h:238
PUDPSocket
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:47
PFilePath
This class describes a full description for a file on the particular platform.
Definition: filepath.h:65
PSystemLogTarget::m_thresholdLevel
PSystemLog::Level m_thresholdLevel
Definition: syslog.h:171
PSystemLog::Level
Level
define the different error log levels
Definition: syslog.h:54
PSystemLogToNetwork::m_facility
unsigned m_facility
Definition: syslog.h:281
PSystemLog::Buffer
friend class Buffer
Definition: syslog.h:117
udpsock.h
PSystemLog::StdError
Log from standard error stream.
Definition: syslog.h:56
PSystemLogTarget::GetThresholdLevel
PSystemLog::Level GetThresholdLevel() const
Get the current level for logging.
Definition: syslog.h:148
PSystemLogToFile::m_file
PTextFile m_file
Definition: syslog.h:242
PSystemLog::Debug3
Log even more debugging information.
Definition: syslog.h:70
PSystemLog::Debug6
Log a bucket load of debugging information.
Definition: syslog.h:76
PSystemLogToNowhere
Log system output to nowhere.
Definition: syslog.h:183
PSystemLog::Debug2
Log more debugging information.
Definition: syslog.h:68
PSystemLogToStderr
Log system output to stderr.
Definition: syslog.h:195
PSystemLogToNetwork
Log system output to the network using RFC 3164 BSD syslog protocol.
Definition: syslog.h:248
PSystemLog::Debug5
Log a real lot of debugging information.
Definition: syslog.h:74
PString
The character string class.
Definition: pstring.h:108
PSystemLog::Debug
Log debugging information.
Definition: syslog.h:66
PSystemLog::GetTarget
static PSystemLogTarget & GetTarget()
Get the current target/destination for system logging.
PSystemLog::PSystemLog
PSystemLog(Level level)
Create a system log stream.
PSystemLog::~PSystemLog
~PSystemLog()
Destroy the string stream, deleting the stream buffer.
Definition: syslog.h:87
PSystemLog::Info
Log general information.
Definition: syslog.h:64
PSystemLog::NumLogLevels
Definition: syslog.h:78
PIPSocket::Address
A class describing an IP address.
Definition: ipsock.h:75
PSystemLogToNetwork::m_socket
PUDPSocket m_socket
Definition: syslog.h:282
PSystemLog::Debug4
Log a lot of debugging information.
Definition: syslog.h:72
PSystemLogToSyslog
Log system output to the Posix syslog() function.
Definition: syslog.h:306
PSystemLogToFile
Log system output to a file.
Definition: syslog.h:213
PSystemLogTarget::SetThresholdLevel
void SetThresholdLevel(PSystemLog::Level level)
Set the level at which errors are logged.
Definition: syslog.h:139
PSystemLogTarget
Definition: syslog.h:125
PTextFile
A class representing a a structured file that is portable accross CPU architectures.
Definition: textfile.h:49
PSystemLog::SetTarget
static void SetTarget(PSystemLogTarget *target, bool autoDelete=true)
Set the current target/destination for system logging.
PSystemLogToNetwork::m_port
WORD m_port
Definition: syslog.h:280
PSystemLog::Fatal
Log a fatal error.
Definition: syslog.h:58
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PSystemLogToNowhere::Output
virtual void Output(PSystemLog::Level, const char *)
Log an error into the system log.
Definition: syslog.h:187
PSystemLogToNetwork::m_host
PIPSocket::Address m_host
Definition: syslog.h:279