-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog_cpp.h
executable file
·79 lines (76 loc) · 1.53 KB
/
syslog_cpp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef __syslogwrapper
#define __syslogwrapper
#include <iostream>
#include <streambuf>
#include <string>
#include <utility>
namespace csyslog {
#include <syslog.h>
}
namespace syslog
{
struct level {
enum priority {
emerge = LOG_EMERG,
alert = LOG_ALERT,
critic = LOG_CRIT,
error = LOG_ERR,
warn = LOG_WARNING,
notice = LOG_NOTICE,
info = LOG_INFO,
debug = LOG_DEBUG
};
};
class streambuf : public std::streambuf
{
private:
std::string buffer;
int debug_level;
public:
streambuf() : debug_level(level::debug) { }
void level(int level) { debug_level = level; }
protected:
int sync()
{
if (buffer.size()) {
csyslog::syslog(debug_level, "%s", buffer.c_str());
buffer.erase();
}
return 0;
}
int_type overflow(int_type c)
{
if(c == traits_type::eof()) sync();
else buffer += static_cast<char>(c);
return c;
}
};
class syslog_ostream : public std::ostream
{
streambuf log_buffer;
public:
syslog_ostream() : std::ostream(&log_buffer) {}
syslog_ostream& operator<<(const level::priority lev) {
log_buffer.level(lev);
return *this;
}
void openlog(const char* procname)
{
csyslog::openlog( procname, LOG_CONS | LOG_PID | LOG_NDELAY | LOG_LOCAL1, LOG_USER );
}
void closelog()
{
csyslog::closelog();
}
void setlogmask(const level::priority level)
{
csyslog::setlogmask(LOG_UPTO(level));
}
syslog_ostream& operator << ( std::ostream&(*f)(std::ostream&)) {
*this << f;
return *this;
}
};
}
extern syslog::syslog_ostream logger;
#endif