Imported Upstream version 0.3
[anytun.git] / src / logTargets.h
1 /*
2  *  anytun
3  *
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.
12  *
13  *
14  *  Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, 
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
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.
22  *
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.
27  *
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/>.
30  */
31
32 #ifndef _LOG_TARGETS_H_
33 #define _LOG_TARGETS_H_
34
35 #include <string>
36 #include <map>
37
38 #ifdef LOG_SYSLOG
39 #include <syslog.h>
40 #endif
41
42 #ifdef LOG_FILE
43 #include <fstream>
44 #endif
45
46 #include "datatypes.h"
47
48 class LogTarget
49 {
50 public:
51   LogTarget();
52   LogTarget(int prio);
53   virtual ~LogTarget() {};
54
55   virtual void open() = 0;
56   virtual void close() = 0;
57   bool isOpen() { return opened; };
58
59   void enable() { enabled = true; };
60   void disable() { enabled = false; };
61   bool isEnabled() { return enabled; };
62
63   int getMaxPrio() { return max_prio; };
64   void setMaxPrio(int p) { max_prio = p; };
65
66   virtual void log(std::string msg, int prio) = 0;  
67
68 protected:
69   bool opened;
70   bool enabled;
71   int max_prio;
72 };
73
74 class LogTargetList 
75 {
76 public:
77   typedef enum { TARGET_UNKNOWN, TARGET_SYSLOG, TARGET_FILE, 
78                  TARGET_STDOUT, TARGET_STDERR, TARGET_WINEVENTLOG } target_type_t;
79
80   static target_type_t targetTypeFromString(std::string type);
81   static std::string targetTypeToString(target_type_t type);
82
83   ~LogTargetList();
84   LogTarget* add(std::string conf);
85   LogTarget* add(target_type_t type, int prio, std::string conf);
86   void clear();
87   
88   void log(std::string msg, int prio);
89
90 private:
91   typedef std::multimap<target_type_t, LogTarget*> TargetsMap;
92   TargetsMap targets;
93 };
94
95
96 #ifdef LOG_SYSLOG
97 class LogTargetSyslog : public LogTarget
98 {
99 public:
100   static const int FAC_USER = LOG_USER;
101   static const int FAC_MAIL = LOG_MAIL;
102   static const int FAC_DAEMON = LOG_DAEMON;
103   static const int FAC_AUTH = LOG_AUTH;
104   static const int FAC_SYSLOG = LOG_SYSLOG;
105   static const int FAC_LPR = LOG_LPR;
106   static const int FAC_NEWS = LOG_NEWS;
107   static const int FAC_UUCP = LOG_UUCP;
108   static const int FAC_CRON = LOG_CRON;
109   static const int FAC_AUTHPRIV = LOG_AUTHPRIV;
110   static const int FAC_FTP = LOG_FTP;
111   static const int FAC_LOCAL0 = LOG_LOCAL0;
112   static const int FAC_LOCAL1 = LOG_LOCAL1;
113   static const int FAC_LOCAL2 = LOG_LOCAL2;
114   static const int FAC_LOCAL3 = LOG_LOCAL3;
115   static const int FAC_LOCAL4 = LOG_LOCAL4;
116   static const int FAC_LOCAL5 = LOG_LOCAL5;
117   static const int FAC_LOCAL6 = LOG_LOCAL6;
118   static const int FAC_LOCAL7 = LOG_LOCAL7;
119
120   static int facilityFromString(std::string fac);
121   static std::string facilityToString(int fac);
122
123   LogTargetSyslog(int prio, std::string conf);
124   ~LogTargetSyslog();
125
126   void open();
127   void close();
128   void log(std::string msg, int prio);  
129   static bool duplicateAllowed() { return false; };
130
131   LogTargetSyslog& setLogName(std::string l); 
132   std::string getLogName() const { return logname; }
133   LogTargetSyslog& setFacility(int f);
134   int getFacility() const { return facility; }
135   
136 private:
137   std::string logname;
138   int facility;
139 };
140 #endif
141
142 #ifdef LOG_FILE
143 class LogTargetFile : public LogTarget
144 {
145 public:
146   LogTargetFile(int prio, std::string conf);
147   ~LogTargetFile();
148
149   void open();
150   void close();
151   void log(std::string msg, int prio);  
152   static bool duplicateAllowed() { return true; };
153
154   LogTargetFile& setLogFilename(std::string l); 
155   std::string getLogFilename() const { return logfilename; }
156   
157 private:
158   std::string logfilename;
159   std::ofstream logfile;
160 };
161 #endif
162
163 #ifdef LOG_STDOUT
164 class LogTargetStdout : public LogTarget
165 {
166 public:
167   LogTargetStdout(int prio, std::ostream& s);
168   ~LogTargetStdout();
169
170   void open();
171   void close();
172   void log(std::string msg, int prio);  
173   static bool duplicateAllowed() { return false; };
174
175 private:
176   std::ostream& stream;
177 };
178 #endif
179
180 #ifdef LOG_WINEVENTLOG
181 class LogTargetWinEventlog : public LogTarget
182 {
183 public:
184   static WORD prioToEventLogType(int prio);
185
186   LogTargetWinEventlog(int prio, std::string conf);
187   ~LogTargetWinEventlog();
188
189   void open();
190   void close();
191   void log(std::string msg, int prio);  
192   static bool duplicateAllowed() { return false; };
193
194   LogTargetWinEventlog& setLogName(std::string l); 
195   std::string getLogName() const { return logname; };
196
197 private:
198   std::string logname;
199   HANDLE h_event_source;
200 };
201 #endif
202
203 #endif