plptools
Loading...
Searching...
No Matches
logbuf Class Reference

A streambuffer, logging via syslog. More...

#include <log.h>

Inheritance diagram for logbuf:
Collaboration diagram for logbuf:

Public Member Functions

 logbuf (int loglevel, int fd)
 Constructs a new instance. More...
 
void useSyslog ()
 Write logs using syslog. More...
 
void useFileDescriptor ()
 Write logs to the file descriptor passed in the constructor. More...
 
void setLevel (int newlevel)
 Modifies the loglevel of this instance. More...
 
int overflow (int c=EOF)
 Called by the associated ostream to write a character. More...
 

Private Attributes

char * ptr
 Pointer to next char in buffer. More...
 
unsigned int len
 Current length of buffer. More...
 
int _level
 The log level to use with syslog. More...
 
int _fd
 File descriptor to use when switched off. More...
 
bool _use_syslog
 Log flag. More...
 
char buf [1024]
 The internal buffer for holding messages. More...
 

Detailed Description

A streambuffer, logging via syslog.

logbuf can be used, if you want to use syslog for logging but don't want to change all your nice C++-style output statements in your code.

Here is an example showing the usage of logbuf:

    openlog("myDaemon", LOG_CONS|LOG_PID, LOG_DAEMON);
    logbuf ebuf(LOG_ERR, 2);
    ostream lerr(&ebuf);

    ... some code ...

    lerr << "Whoops, got an error" << endl;

Logging can be toggled between using syslog and writing to a file (see useSyslog and useFileDescriptor).

Definition at line 51 of file log.h.

Constructor & Destructor Documentation

◆ logbuf()

logbuf::logbuf ( int  loglevel,
int  fd 
)

Constructs a new instance.

Parameters
loglevelThe log level for this instance. see syslog(3) for symbolic names to use.
fdAn optional file descriptor to use if switched off.

Definition at line 29 of file log.cc.

Member Function Documentation

◆ overflow()

int logbuf::overflow ( int  c = EOF)

Called by the associated ostream to write a character.

Stores the character in a buffer and calls syslog(level, buffer) whenever a LF is seen.

Definition at line 37 of file log.cc.

◆ setLevel()

void logbuf::setLevel ( int  newlevel)
inline

Modifies the loglevel of this instance.

Parameters
newlevelThe new loglevel.

Definition at line 84 of file log.h.

◆ useFileDescriptor()

void logbuf::useFileDescriptor ( )
inline

Write logs to the file descriptor passed in the constructor.

When writing to the file descriptor, logs will not be written using syslog. If a file descriptor of -1 was specified, no logs will be written.

Definition at line 77 of file log.h.

◆ useSyslog()

void logbuf::useSyslog ( )
inline

Write logs using syslog.

When writing to syslog is enabled, logs will not be written to the file descriptor passed in the constructor.

Definition at line 69 of file log.h.

Member Data Documentation

◆ _fd

int logbuf::_fd
private

File descriptor to use when switched off.

If this is -1, don't output anything.

Definition at line 116 of file log.h.

◆ _level

int logbuf::_level
private

The log level to use with syslog.

Definition at line 110 of file log.h.

◆ _use_syslog

bool logbuf::_use_syslog
private

Log flag.

Definition at line 121 of file log.h.

◆ buf

char logbuf::buf[1024]
private

The internal buffer for holding messages.

Definition at line 127 of file log.h.

◆ len

unsigned int logbuf::len
private

Current length of buffer.

Definition at line 105 of file log.h.

◆ ptr

char* logbuf::ptr
private

Pointer to next char in buffer.

Definition at line 100 of file log.h.


The documentation for this class was generated from the following files: