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