Syslog.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $Header$
00004 
00005 */
00006 
00007 #include <ctype.h>
00008 
00009 #include "Error.h"
00010 #include "Config.h"
00011 
00012 #include "Syslog.h"
00013 
00014 // Public methods.
00015 
00016 int Syslog::log(const Severity severity, const String &msg)
00017 {
00018   char message[SYSLOG_LIMIT_MSG_SIZE_MAX];
00019   char timestamp[SYSLOG_LIMIT_TIMESTAMP_SIZE_MAX];
00020   time_t t = time(NULL);
00021   int res;
00022 
00023   strftime(timestamp, sizeof(timestamp), "%b %e %T", localtime(&t));
00024 
00025   snprintf(message, sizeof(message), "<%03u> %s %s %s[%u]: %s",
00026     _facility + severity, timestamp, _connection->localIp().c_str(), _tag.c_str(), getpid(), msg.c_str());
00027 
00028   if (FAILED(res = _connection->writeStr(String(message).replaceCtrlChars())))
00029     return ERROR_BACKTRACE(res);
00030 
00031   return OK;
00032 }
00033 
00034 int Syslog::log(const Severity &severity, const String &timestamp, const String &fileName,
00035                 const unsigned int lineNum, const String &functionName, const String &msg)
00036 {
00037   char message[SYSLOG_LIMIT_MSG_SIZE_MAX];
00038   int res;
00039 
00040 
00041   snprintf(message, sizeof(message), "<%03u> %s %s %s[%u]: [file: %s, line: %u, function: %s] %s",
00042     _facility + severity, CSTRING(timestamp), CSTRING(_connection->localIp()), CSTRING(_tag), getpid(),
00043     CSTRING(fileName), lineNum, CSTRING(functionName), CSTRING(msg));
00044 
00045   if (FAILED(res = _connection->writeStr(String(message).replaceCtrlChars())))
00046     return ERROR_BACKTRACE(res);
00047 
00048   return OK;
00049 }
00050 
00051 Facility Syslog::facilityId(const String &facility)
00052 {
00053   if (strcasecmp(facility.c_str(), "kernel") == 0)
00054     return FACILITY_KERNEL;
00055   if (strcasecmp(facility.c_str(), "user") == 0)
00056     return FACILITY_USER;
00057   if (strcasecmp(facility.c_str(), "mail") == 0)
00058     return FACILITY_MAIL;
00059   if (strcasecmp(facility.c_str(), "daemon") == 0)
00060     return FACILITY_DAEMON;
00061   if (strcasecmp(facility.c_str(), "security") == 0)
00062     return FACILITY_SECURITY;
00063   if (strcasecmp(facility.c_str(), "syslog") == 0)
00064     return FACILITY_SYSLOG;
00065   if (strcasecmp(facility.c_str(), "printer") == 0)
00066     return FACILITY_PRINTER;
00067   if (strcasecmp(facility.c_str(), "news") == 0)
00068     return FACILITY_NEWS;
00069   if (strcasecmp(facility.c_str(), "uucp") == 0)
00070     return FACILITY_UUCP;
00071   if (strcasecmp(facility.c_str(), "clock") == 0)
00072     return FACILITY_CLOCK;
00073   if (strcasecmp(facility.c_str(), "security2") == 0)
00074     return FACILITY_SECURITY2;
00075   if (strcasecmp(facility.c_str(), "ftp") == 0)
00076     return FACILITY_FTP;
00077   if (strcasecmp(facility.c_str(), "ntp") == 0)
00078     return FACILITY_NTP;
00079   if (strcasecmp(facility.c_str(), "log_audit") == 0)
00080     return FACILITY_LOG_AUDIT;
00081   if (strcasecmp(facility.c_str(), "log_alert") == 0)
00082     return FACILITY_LOG_ALERT;
00083   if (strcasecmp(facility.c_str(), "clock2") == 0)
00084     return FACILITY_CLOCK2;
00085   if (strcasecmp(facility.c_str(), "local0") == 0)
00086     return FACILITY_LOCAL0;
00087   if (strcasecmp(facility.c_str(), "local1") == 0)
00088     return FACILITY_LOCAL1;
00089   if (strcasecmp(facility.c_str(), "local2") == 0)
00090     return FACILITY_LOCAL2;
00091   if (strcasecmp(facility.c_str(), "local3") == 0)
00092     return FACILITY_LOCAL3;
00093   if (strcasecmp(facility.c_str(), "local4") == 0)
00094     return FACILITY_LOCAL4;
00095   if (strcasecmp(facility.c_str(), "local5") == 0)
00096     return FACILITY_LOCAL5;
00097   if (strcasecmp(facility.c_str(), "local6") == 0)
00098     return FACILITY_LOCAL6;
00099   if (strcasecmp(facility.c_str(), "local7") == 0)
00100     return FACILITY_LOCAL7;
00101 
00102   return FACILITY_LOCAL5;
00103 }
00104 
00105 String Syslog::facilityName(const Facility facility)
00106 {
00107   switch (facility) {
00108     case FACILITY_KERNEL:
00109       return "kernel";
00110       break;
00111     case FACILITY_USER:
00112       return "user";
00113       break;
00114     case FACILITY_MAIL:
00115       return "mail";
00116       break;
00117     case FACILITY_DAEMON:
00118       return "daemon";
00119       break;
00120     case FACILITY_SECURITY:
00121       return "security";
00122       break;
00123     case FACILITY_SYSLOG:
00124       return "syslog";
00125       break;
00126     case FACILITY_PRINTER:
00127       return "printer";
00128       break;
00129     case FACILITY_NEWS:
00130       return "news";
00131       break;
00132     case FACILITY_UUCP:
00133       return "uucp";
00134       break;
00135     case FACILITY_CLOCK:
00136       return "clock";
00137       break;
00138     case FACILITY_SECURITY2:
00139       return "security2";
00140       break;
00141     case FACILITY_FTP:
00142       return "ftp";
00143       break;
00144     case FACILITY_NTP:
00145       return "ntp";
00146       break;
00147     case FACILITY_LOG_AUDIT:
00148       return "log_audit";
00149       break;
00150     case FACILITY_LOG_ALERT:
00151       return "log_alert";
00152       break;
00153     case FACILITY_CLOCK2:
00154       return "clock2";
00155       break;
00156     case FACILITY_LOCAL0:
00157       return "local0";
00158       break;
00159     case FACILITY_LOCAL1:
00160       return "local1";
00161       break;
00162     case FACILITY_LOCAL2:
00163       return "local2";
00164       break;
00165     case FACILITY_LOCAL3:
00166       return "local3";
00167       break;
00168     case FACILITY_LOCAL4:
00169       return "local4";
00170       break;
00171     case FACILITY_LOCAL5:
00172       return "local5";
00173       break;
00174     case FACILITY_LOCAL6:
00175       return "local6";
00176       break;
00177     case FACILITY_LOCAL7:
00178       return "local7";
00179       break;
00180     default:
00181       return "local5";
00182       break;
00183   }
00184   return "local5";
00185 }
00186 
00187 String Syslog::severityName(const Severity severity)
00188 {
00189   switch (severity) {
00190     case SEVERITY_EMERGENCY:
00191       return "Emergency";
00192       break;
00193     case SEVERITY_ALERT:
00194       return "Alert";
00195       break;
00196     case SEVERITY_CRITICAL:
00197       return "Critical";
00198       break;
00199     case SEVERITY_ERROR:
00200       return "Error";
00201       break;
00202     case SEVERITY_WARNING:
00203       return "Warning";
00204       break;
00205     case SEVERITY_NOTICE:
00206       return "Notice";
00207       break;
00208     case SEVERITY_INFO:
00209       return "Info";
00210       break;
00211     case SEVERITY_DEBUG:
00212       return "Debug";
00213       break;
00214     default:
00215       return "Unknown";
00216       break;
00217   }
00218   return "Unknown";
00219 }
00220 
00221 int Syslog::setTag(const String &newTag)
00222 {
00223   _tag = newTag;
00224   return OK;
00225 }
00226 
00227 // Private methods.
00228 
00229 Syslog::Syslog() : Object(), Singleton<Syslog>()
00230 {
00231   BIND(tag);
00232   BIND(collector);
00233   BIND(facility);
00234 
00235   String collectorHost;
00236   int res;
00237 
00238   if (FAILED(res = REGISTRY_GET(Syslog, collector, collectorHost))) {
00239     failure(ERROR_BACKTRACE(res));
00240     return;
00241   }
00242 
00243   if (FAILED(res = REGISTRY_GET(Syslog, tag, _tag))) {
00244     failure(ERROR_BACKTRACE(res));
00245     return;
00246   }
00247 
00248   String facility;
00249   if (FAILED(res = REGISTRY_GET(Syslog, facility, facility))) {
00250     failure(ERROR_BACKTRACE(res));
00251     return;
00252   }
00253   _facility = facilityId(facility) << 3;
00254 
00255   if (!(_connection = new UdpConnection(CSTRING(collectorHost), SYSLOG_PORT))) {
00256     failure(ERROR(MSG_OBJECT_CANNOT_CREATE, "UdpConnection"));
00257     return;
00258   }
00259   if (FAILED(res = _connection->failureCode())) {
00260     failure(ERROR_BACKTRACE(res));
00261     return;
00262   }
00263   if (collectorHost == "255.255.255.255")
00264     if (FAILED(res = _connection->setBroadcast())) {
00265       failure(ERROR_BACKTRACE(res));
00266       return;
00267     }
00268 
00269   if (FAILED(res = CONNECT(tag(), set, this, Syslog, setTag))) {
00270     failure(ERROR_BACKTRACE(res));
00271     return;
00272   }
00273 
00274 }
00275 
00276 Syslog::~Syslog()
00277 {
00278   if (_connection)
00279     delete _connection;
00280 }

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1