OSDN Git Service

Fix no pic
[uclinux-h8/uClinux-dist.git] / user / gnugk / syslogacct.cxx
1 /*
2  * syslogacct.cxx
3  *
4  * accounting module for GNU Gatekeeper for the syslog.
5  *
6  * Copyright (c) 2006, Jan Willamowius
7  *
8  * This work is published under the GNU Public License (GPL)
9  * see file COPYING for details.
10  * We also explicitely grant the right to link this code
11  * with the OpenH323 library.
12  *
13  * $Log: syslogacct.cxx,v $
14  * Revision 1.2  2006/07/31 08:34:22  willamowius
15  * make logging facility and priority configurable (patch by Brian Thompson)
16  *
17  * Revision 1.1  2006/06/30 08:07:05  willamowius
18  * new accounting module SyslogAcct
19  *
20  *
21  */
22  
23 #ifndef _WIN32
24
25 #if (_MSC_VER >= 1200)
26 #pragma warning( disable : 4786 ) // warning about too long debug symbols off
27 #endif
28
29 #include <syslog.h>
30 #include <ptlib.h>
31 #include <h323pdu.h>
32 #include "GkStatus.h"
33 #include "syslogacct.h"
34 #include "Toolkit.h"
35
36 const char* const SyslogSec = "SyslogAcct";
37 static int syslog_level = LOG_INFO;
38 static int syslog_facility = LOG_USER;
39
40 SyslogAcct::SyslogAcct( 
41         const char* moduleName,
42         const char* cfgSecName
43         )
44         :
45         GkAcctLogger(moduleName, cfgSecName)
46 {
47         // it is very important to set what type of accounting events
48         // are supported for each accounting module, otherwise the Log method
49         // will no get called
50         SetSupportedEvents(SyslogAcctEvents);
51
52         PConfig* cfg = GetConfig();
53         const PString& cfgSec = GetConfigSectionName();
54         m_timestampFormat = cfg->GetString(cfgSec, "TimestampFormat", "");
55         m_startEvent = cfg->GetString(cfgSec, "StartEvent", "CALL|Start|%{caller-ip}:%{caller-port}|%{callee-ip}:%{callee-port}|%{CallId}");
56         m_stopEvent = cfg->GetString(cfgSec, "StopEvent", "CALL|Stop|%{caller-ip}:%{caller-port}|%{callee-ip}:%{callee-port}|%{CallId}");
57         m_updateEvent = cfg->GetString(cfgSec, "UpdateEvent", "CALL|Update|%{caller-ip}:%{caller-port}|%{callee-ip}:%{callee-port}|%{CallId}");
58         m_connectEvent = cfg->GetString(cfgSec, "ConnectEvent", "CALL|Connect|%{caller-ip}:%{caller-port}|%{callee-ip}:%{callee-port}|%{CallId}");
59 }
60
61 SyslogAcct::~SyslogAcct()
62 {
63 }
64
65 GkAcctLogger::Status SyslogAcct::Log(
66         GkAcctLogger::AcctEvent evt, 
67         const callptr& call
68         )
69 {
70         // a workaround to prevent processing end on "sufficient" module
71         // if it is not interested in this event type
72         if ((evt & GetEnabledEvents() & GetSupportedEvents()) == 0)
73                 return Next;
74                 
75         if (!call) {
76                 PTRACE(1,"SYSLOGACCT\t"<<GetName()<<" - missing call info for event "<<evt);
77                 return Fail;
78         }
79
80         PString sysloglevelconfig = GkConfig()->GetString(SyslogSec, "SyslogLevel", "LOG_INFO");
81         PString syslogfacilityconfig = GkConfig()->GetString(SyslogSec, "SyslogFacility", "LOG_USER");
82
83         
84         if (sysloglevelconfig == "LOG_EMERG") {
85                 syslog_level = LOG_EMERG;
86         } else if (sysloglevelconfig == "LOG_ALERT") {
87                 syslog_level = LOG_ALERT;
88         } else if (sysloglevelconfig == "LOG_CRIT") {
89                 syslog_level = LOG_CRIT;
90         } else if (sysloglevelconfig == "LOG_ERR") {
91                 syslog_level = LOG_ERR;
92         } else if (sysloglevelconfig == "LOG_WARNING") {
93                 syslog_level = LOG_WARNING;
94         } else if (sysloglevelconfig == "LOG_NOTICE") {
95                 syslog_level = LOG_NOTICE;
96         } else if (sysloglevelconfig == "LOG_INFO") {
97                 syslog_level = LOG_INFO;
98         } else if (sysloglevelconfig == "LOG_DEBUG") {
99                 syslog_level = LOG_DEBUG;
100         } else {
101                 syslog_level = LOG_INFO;
102         }
103
104         if (syslogfacilityconfig == "LOG_DAEMON") {
105                 syslog_facility = LOG_DAEMON;
106         } else if (syslogfacilityconfig == "LOG_USER") {
107                 syslog_facility = LOG_USER;
108         } else if (syslogfacilityconfig == "LOG_AUTH") {
109                 syslog_facility = LOG_AUTH;
110         } else if (syslogfacilityconfig == "LOG_LOCAL0") {
111                 syslog_facility = LOG_LOCAL0;
112         } else if (syslogfacilityconfig == "LOG_LOCAL1") {
113                 syslog_facility = LOG_LOCAL1;
114         } else if (syslogfacilityconfig == "LOG_LOCAL2") {
115                 syslog_facility = LOG_LOCAL2;
116         } else if (syslogfacilityconfig == "LOG_LOCAL3") {
117                 syslog_facility = LOG_LOCAL3;
118         } else if (syslogfacilityconfig == "LOG_LOCAL4") {
119                 syslog_facility = LOG_LOCAL4;
120         } else if (syslogfacilityconfig == "LOG_LOCAL5") {
121                 syslog_facility = LOG_LOCAL5;
122         } else if (syslogfacilityconfig == "LOG_LOCAL6") {
123                 syslog_facility = LOG_LOCAL6;
124         } else if (syslogfacilityconfig == "LOG_LOCAL7") {
125                 syslog_facility = LOG_LOCAL7;
126         } else {
127                 syslog_facility = LOG_USER;
128         }
129
130
131         PString eventTmpl;
132         if (evt == AcctStart) {
133                 eventTmpl = m_startEvent;
134         } else if (evt == AcctConnect) {
135                 eventTmpl = m_connectEvent;
136         } else if (evt == AcctUpdate) {
137                 eventTmpl = m_updateEvent;
138         } else if (evt == AcctStop) {
139                 eventTmpl = m_stopEvent;
140         }
141
142         if (!eventTmpl.IsEmpty()) {             // don't send event if the template string is empty
143                 std::map<PString, PString> params;
144                 SetupAcctParams(params, call, m_timestampFormat);
145                 PString msg = ReplaceAcctParams(eventTmpl, params);
146                 openlog("GnuGk", LOG_PID, syslog_facility);
147                 syslog(syslog_facility | syslog_level, "%s", (const char *)msg);
148                 closelog();
149         }
150
151         return Ok;
152 }
153
154 PString SyslogAcct::EscapeAcctParam(const PString& param) const
155 {
156         return "\"" + param + "\"";     // test: quote
157 }
158
159 namespace {
160         // append syslog accounting logger to the global list of loggers
161         GkAcctLoggerCreator<SyslogAcct> SyslogAcctCreator("SyslogAcct");
162 }
163
164 #endif // not _WIN32