1 |
torben |
159 |
#include "Logger.h" |
2 |
|
|
|
3 |
|
|
#include "Common.h" |
4 |
|
|
#include <iostream> |
5 |
|
|
#include <sstream> |
6 |
|
|
#include <fstream> |
7 |
|
|
#include <iomanip> |
8 |
|
|
|
9 |
|
|
using namespace std; |
10 |
|
|
|
11 |
|
|
const char* Months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"}; |
12 |
|
|
|
13 |
|
|
namespace Logger |
14 |
|
|
{ |
15 |
|
|
string _logFile; |
16 |
|
|
void setLogfile(std::string file) |
17 |
|
|
{ |
18 |
|
|
_logFile = file; |
19 |
|
|
} |
20 |
|
|
|
21 |
|
|
void logMessage(string msg) |
22 |
|
|
{ |
23 |
|
|
Common* cmn = Common::instance(); |
24 |
|
|
time_t t = time(0); |
25 |
|
|
tm now; |
26 |
|
|
localtime_r(&t, &now); |
27 |
|
|
|
28 |
|
|
ostringstream out; |
29 |
|
|
out << Months[ now.tm_mon ] << " " << setw(2) << setfill('0') << now.tm_mday; |
30 |
|
|
out << " " << setw(2) << setfill('0') << now.tm_hour; |
31 |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_min; |
32 |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_sec; |
33 |
|
|
out << " " << msg << endl; |
34 |
|
|
|
35 |
|
|
if (cmn->isDaemon && _logFile != "" && cmn->daemonized) |
36 |
|
|
{ |
37 |
|
|
seteuid(0); |
38 |
|
|
ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode |
39 |
|
|
if (of) |
40 |
|
|
{ |
41 |
|
|
of << out.str(); |
42 |
|
|
of.flush(); |
43 |
|
|
of.close(); |
44 |
|
|
} |
45 |
|
|
seteuid( cmn->uid); |
46 |
|
|
} |
47 |
|
|
else |
48 |
|
|
{ |
49 |
|
|
cout << out.str(); |
50 |
|
|
cout.flush(); |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
} |