added a workaround for strerror_r problem
[anytun.git] / src / log.h
blobe1f916351a4e3d2e1288e6c9316bddcd5446a0ba
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #ifndef _LOG_H_
33 #define _LOG_H_
35 #include <string>
36 #include <sstream>
37 #ifndef NOSYSLOG
38 #include <syslog.h>
39 #endif
41 #include "threadUtils.hpp"
44 #define STERROR_TEXT_MAX 100
46 #ifndef NOCRYPT
47 #ifndef USE_SSL_CRYPTO
48 #include <gcrypt.h>
50 class LogGpgError
52 public:
53 LogGpgError(gcry_error_t e) : err_(e) {};
54 gcry_error_t err_;
56 std::ostream& operator<<(std::ostream& stream, LogGpgError const& value);
57 #endif
58 #endif
60 class LogErrno
62 public:
63 LogErrno(int e) : err_(e) {};
64 int err_;
66 std::ostream& operator<<(std::ostream& stream, LogErrno const& value);
68 class Log;
70 class LogStringBuilder
72 public:
73 LogStringBuilder(LogStringBuilder const& src);
74 LogStringBuilder(Log& l, int p);
75 ~LogStringBuilder();
77 template<class T>
78 std::ostream& operator<<(T const& value) { return stream << value; }
80 private:
81 Log& log;
82 int prio;
83 std::stringstream stream;
86 class Log : public std::ostringstream
88 public:
89 #ifndef NOSYSLOG
90 static const int FAC_USER = LOG_USER;
91 static const int FAC_MAIL = LOG_MAIL;
92 static const int FAC_DAEMON = LOG_DAEMON;
93 static const int FAC_AUTH = LOG_AUTH;
94 static const int FAC_SYSLOG = LOG_SYSLOG;
95 static const int FAC_LPR = LOG_LPR;
96 static const int FAC_NEWS = LOG_NEWS;
97 static const int FAC_UUCP = LOG_UUCP;
98 static const int FAC_CRON = LOG_CRON;
99 static const int FAC_AUTHPRIV = LOG_AUTHPRIV;
100 static const int FAC_FTP = LOG_FTP;
101 static const int FAC_LOCAL0 = LOG_LOCAL0;
102 static const int FAC_LOCAL1 = LOG_LOCAL1;
103 static const int FAC_LOCAL2 = LOG_LOCAL2;
104 static const int FAC_LOCAL3 = LOG_LOCAL3;
105 static const int FAC_LOCAL4 = LOG_LOCAL4;
106 static const int FAC_LOCAL5 = LOG_LOCAL5;
107 static const int FAC_LOCAL6 = LOG_LOCAL6;
108 static const int FAC_LOCAL7 = LOG_LOCAL7;
110 static const int PRIO_EMERG = LOG_EMERG;
111 static const int PRIO_ALERT = LOG_ALERT;
112 static const int PRIO_CRIT = LOG_CRIT;
113 static const int PRIO_ERR = LOG_ERR;
114 static const int PRIO_WARNING = LOG_WARNING;
115 static const int PRIO_NOTICE = LOG_NOTICE;
116 static const int PRIO_INFO = LOG_INFO;
117 static const int PRIO_DEBUG = LOG_DEBUG;
118 #else
119 static const int FAC_USER = 0;
120 static const int FAC_MAIL = 0;
121 static const int FAC_DAEMON = 0;
122 static const int FAC_AUTH = 0;
123 static const int FAC_SYSLOG = 0;
124 static const int FAC_LPR = 0;
125 static const int FAC_NEWS = 0;
126 static const int FAC_UUCP = 0;
127 static const int FAC_CRON = 0;
128 static const int FAC_AUTHPRIV = 0;
129 static const int FAC_FTP = 0;
130 static const int FAC_LOCAL0 = 0;
131 static const int FAC_LOCAL1 = 0;
132 static const int FAC_LOCAL2 = 0;
133 static const int FAC_LOCAL3 = 0;
134 static const int FAC_LOCAL4 = 0;
135 static const int FAC_LOCAL5 = 0;
136 static const int FAC_LOCAL6 = 0;
137 static const int FAC_LOCAL7 = 0;
139 static const int PRIO_EMERG = 0;
140 static const int PRIO_ALERT = 0;
141 static const int PRIO_CRIT = 0;
142 static const int PRIO_ERR = 0;
143 static const int PRIO_WARNING = 0;
144 static const int PRIO_NOTICE = 0;
145 static const int PRIO_INFO = 0;
146 static const int PRIO_DEBUG = 0;
147 #endif
149 static Log& instance();
151 Log& setLogName(std::string newLogName);
152 std::string getLogName() const { return logName; }
153 Log& setFacility(int newFacility);
154 int getFacility() const { return facility; }
156 LogStringBuilder msg(int prio=PRIO_INFO) { return LogStringBuilder(*this, prio); }
158 private:
159 Log();
160 ~Log();
161 Log(const Log &l);
162 void operator=(const Log &l);
164 static Log* inst;
165 static Mutex instMutex;
166 class instanceCleaner {
167 public: ~instanceCleaner() {
168 if(Log::inst != 0)
169 delete Log::inst;
172 friend class instanceCleaner;
174 void open();
176 Mutex mutex;
177 friend class LogStringBuilder;
179 std::string logName;
180 int facility;
183 extern Log& cLog;
185 #endif