/[projects]/smsdaemon/Logger.cpp
ViewVC logotype

Diff of /smsdaemon/Logger.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 159 by torben, Mon Dec 8 22:05:16 2008 UTC revision 175 by torben, Wed Dec 10 22:02:28 2008 UTC
# Line 1  Line 1 
1  #include "Logger.h"  #include "Logger.h"
2    
3  #include "Common.h"  #include "Common.h"
4    #include "ConfigFile.h"
5  #include <iostream>  #include <iostream>
6  #include <sstream>  #include <sstream>
7  #include <fstream>  #include <fstream>
8  #include <iomanip>  #include <iomanip>
9    
10    #include <stdlib.h>
11    #include <syslog.h>
12    
13  using namespace std;  using namespace std;
14    
15  const char* Months[] = {  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"};  const char* Months[] = {  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"};
# Line 13  const char* Months[] = {  "Jan", "Feb", Line 17  const char* Months[] = {  "Jan", "Feb",
17  namespace Logger  namespace Logger
18  {  {
19          string _logFile;          string _logFile;
20    
21            enum LogDest
22            {
23                    LogFile,
24                    LogSyslog,
25                    LogStdout
26            };
27    
28            LogDest logger;
29    
30            void initLog()
31            {
32                    Common* cmn = Common::instance();
33                    string logmethod = cmn->GetConfigfile()->GetValue("smsdaemon","log");
34                    if (logmethod == "syslog")
35                    {
36                            logger = LogSyslog;
37    
38                            openlog("smsdaemon", 0 , LOG_DAEMON);
39                    }
40                    else if (logmethod == "logfile")
41                    {
42                            logger = LogFile;
43                    }
44                    else
45                    {
46                            cerr << "Invalid config file setting for 'log' : " << logmethod << endl;
47                            exit(1);
48                    }
49    
50                    if (cmn->isDebug)
51                    {
52                            logger = LogStdout;
53                    }
54            }
55    
56          void setLogfile(std::string file)          void setLogfile(std::string file)
57          {          {
58                  _logFile = file;                  _logFile = file;
# Line 26  namespace Logger Line 66  namespace Logger
66                  localtime_r(&t, &now);                  localtime_r(&t, &now);
67    
68                  ostringstream out;                  ostringstream out;
69                  out << Months[ now.tm_mon ] << " " << setw(2) << setfill('0') << now.tm_mday;                  if (logger == LogFile || logger == LogStdout)
70                  out << " " << setw(2) << setfill('0') << now.tm_hour;                  {
71                  out << ":" << setw(2) << setfill('0') << now.tm_min;                          out << Months[ now.tm_mon ] << " " << setw(2) << setfill('0') << now.tm_mday;
72                  out << ":" << setw(2) << setfill('0') << now.tm_sec;                          out << " " << setw(2) << setfill('0') << now.tm_hour;
73                  out << "  " << msg << endl;                          out << ":" << setw(2) << setfill('0') << now.tm_min;
74                            out << ":" << setw(2) << setfill('0') << now.tm_sec;
75                  if (cmn->isDaemon && _logFile != "" && cmn->daemonized)                          out << "  " << msg << endl;
76                  {  
77                          seteuid(0);                          if (cmn->isDaemon && _logFile != "" && cmn->daemonized)
                         ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode  
                         if (of)  
78                          {                          {
79                                  of << out.str();                                  seteuid(0);
80                                  of.flush();                                  ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode
81                                  of.close();                                  if (of)
82                                    {
83                                            of << out.str();
84                                            of.flush();
85                                            of.close();
86                                    }
87                                    seteuid( cmn->uid);
88                          }                          }
89                          seteuid( cmn->uid);                          else
90                  }                          {
91                  else                                  cout << out.str();
92                                    cout.flush();
93                            }
94                    }
95                    else  // logSyslog
96                  {                  {
97                          cout << out.str();                          syslog(LOG_INFO, "%s", msg.c_str());
                         cout.flush();  
98                  }                  }
99          }          }
100  }  }

Legend:
Removed from v.159  
changed lines
  Added in v.175

  ViewVC Help
Powered by ViewVC 1.1.20