forked from PowerDNS/powermail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.hh
90 lines (75 loc) · 2.91 KB
/
logger.hh
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
80
81
82
83
84
85
86
87
88
89
90
/*
PowerMail versatile mail receiver
Copyright (C) 2002 PowerDNS.COM BV
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef LOGGER_HH
#define LOGGER_HH
#include <string>
#include <map>
#include <ctime>
#include <iostream>
#include <sstream>
#include <pthread.h>
#include <syslog.h>
using namespace std;
//! The Logger class can be used to log messages in various ways.
class Logger
{
public:
Logger(const string &, int facility=LOG_DAEMON); //!< pass the identification you wish to appear in the log
//! The urgency of a log message
enum Urgency {All=99999,Alert=LOG_ALERT, Critical=LOG_CRIT, Error=LOG_ERR, Warning=LOG_WARNING,
Notice=LOG_NOTICE,Info=LOG_INFO, Debug=LOG_DEBUG, None=-1};
/** Log a message.
\param msg Message you wish to log
\param Urgency Urgency of the message you wish to log
*/
void log(const string &msg, Urgency u=Notice);
void setFacility(int f){d_facility=f;open();} //!< Choose logging facility
void setName(const string &n); //!< Change the name we log under
void setFlag(int f){flags|=f;open();} //!< set a syslog flag
//! determine of messages go only to syslog, or appear on the screen as well
void toConsole(bool b);
//! set lower limit of urgency needed for console display. Messages of this urgency, and higher, will be displayed
void toConsole(Urgency);
void resetFlags(){flags=0;open();} //!< zero the flags
/** Use this to stream to your log, like this:
\code
L<<"This is an informational message"<<endl; // logged at default loglevel (Info)
L<<Logger::Warning<<"Out of diskspace"<<endl; // Logged as a warning
L<<"This is an informational message"<<endl; // logged AGAIN at default loglevel (Info)
\endcode
*/
Logger& operator<<(const string &s); //!< log a string
Logger& operator<<(int); //!< log an int
Logger& operator<<(Urgency); //!< set the urgency, << style
Logger& operator<<(ostream & (&)(ostream &)); //!< this is to recognise the endl, and to commit the log
private:
Logger& operator=(const Logger &);
Logger(const Logger &);
map<pthread_t,string>d_strings;
map<pthread_t,Urgency> d_outputurgencies;
void open();
string name;
int flags;
int d_facility;
bool opened;
Urgency consoleUrgency;
pthread_mutex_t lock;
};
#ifdef VERBOSELOG
#define DLOG(x) x
#else
#define DLOG(x)
#endif
#endif