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 |
196 |
enum LogDest |
22 |
torben |
175 |
{ |
23 |
|
|
LogFile, |
24 |
|
|
LogSyslog, |
25 |
|
|
LogStdout |
26 |
|
|
}; |
27 |
|
|
|
28 |
|
|
LogDest logger; |
29 |
|
|
|
30 |
|
|
void initLog() |
31 |
|
|
{ |
32 |
|
|
Common* cmn = Common::instance(); |
33 |
torben |
214 |
string logmethod = cmn->GetConfigfile()->GetValue("smsdaemon","log",""); |
34 |
torben |
196 |
if (logmethod == "syslog") |
35 |
torben |
175 |
{ |
36 |
|
|
logger = LogSyslog; |
37 |
|
|
|
38 |
|
|
openlog("smsdaemon", 0 , LOG_DAEMON); |
39 |
torben |
196 |
} |
40 |
torben |
175 |
else if (logmethod == "logfile") |
41 |
|
|
{ |
42 |
|
|
logger = LogFile; |
43 |
torben |
196 |
} |
44 |
torben |
175 |
else |
45 |
|
|
{ |
46 |
|
|
cerr << "Invalid config file setting for 'log' : " << logmethod << endl; |
47 |
|
|
exit(1); |
48 |
|
|
} |
49 |
|
|
|
50 |
torben |
196 |
if (cmn->isDebug) |
51 |
torben |
175 |
{ |
52 |
|
|
logger = LogStdout; |
53 |
|
|
} |
54 |
|
|
} |
55 |
|
|
|
56 |
torben |
159 |
void setLogfile(std::string file) |
57 |
|
|
{ |
58 |
|
|
_logFile = file; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
void logMessage(string msg) |
62 |
|
|
{ |
63 |
|
|
Common* cmn = Common::instance(); |
64 |
|
|
time_t t = time(0); |
65 |
|
|
tm now; |
66 |
|
|
localtime_r(&t, &now); |
67 |
|
|
|
68 |
|
|
ostringstream out; |
69 |
torben |
196 |
if (logger == LogFile || logger == LogStdout) |
70 |
torben |
175 |
{ |
71 |
|
|
out << Months[ now.tm_mon ] << " " << setw(2) << setfill('0') << now.tm_mday; |
72 |
|
|
out << " " << setw(2) << setfill('0') << now.tm_hour; |
73 |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_min; |
74 |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_sec; |
75 |
|
|
out << " " << msg << endl; |
76 |
torben |
159 |
|
77 |
torben |
175 |
if (cmn->isDaemon && _logFile != "" && cmn->daemonized) |
78 |
torben |
159 |
{ |
79 |
torben |
175 |
seteuid(0); |
80 |
|
|
ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode |
81 |
|
|
if (of) |
82 |
|
|
{ |
83 |
|
|
of << out.str(); |
84 |
|
|
of.flush(); |
85 |
|
|
of.close(); |
86 |
|
|
} |
87 |
|
|
seteuid( cmn->uid); |
88 |
torben |
159 |
} |
89 |
torben |
175 |
else |
90 |
|
|
{ |
91 |
|
|
cout << out.str(); |
92 |
|
|
cout.flush(); |
93 |
|
|
} |
94 |
torben |
196 |
} |
95 |
torben |
175 |
else // logSyslog |
96 |
torben |
159 |
{ |
97 |
torben |
175 |
syslog(LOG_INFO, "%s", msg.c_str()); |
98 |
torben |
159 |
} |
99 |
|
|
} |
100 |
|
|
} |