1 |
#include "common.h" |
#include "Common.h" |
2 |
|
|
3 |
|
|
4 |
#include "version.h" |
#include "version.h" |
7 |
#include <string> |
#include <string> |
8 |
#include <iostream> |
#include <iostream> |
9 |
#include <iomanip> |
#include <iomanip> |
|
#include <fstream> |
|
10 |
#include <sstream> |
#include <sstream> |
11 |
#include <time.h> |
#include <time.h> |
12 |
|
|
15 |
#include "ConfigFile.h" |
#include "ConfigFile.h" |
16 |
|
|
17 |
using namespace std; |
using namespace std; |
|
const char* Months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"}; |
|
18 |
|
|
19 |
Common* Common::instance() |
Common* Common::instance() |
20 |
{ |
{ |
30 |
_taskManager = new TaskManager(); |
_taskManager = new TaskManager(); |
31 |
configFilePath = "/etc/smsdaemon.conf"; |
configFilePath = "/etc/smsdaemon.conf"; |
32 |
_configFile = new ConfigFile(); |
_configFile = new ConfigFile(); |
33 |
|
|
34 |
|
daemonized = false; |
35 |
} |
} |
36 |
|
|
37 |
|
|
59 |
return _configFile; |
return _configFile; |
60 |
} |
} |
61 |
|
|
|
void Common::logMessage(string msg) |
|
|
{ |
|
|
time_t t = time(0); |
|
|
tm now; |
|
|
localtime_r(&t, &now); |
|
|
|
|
|
ostringstream out; |
|
|
out << Months[ now.tm_mon ] << " " << setw(2) << setfill('0') << now.tm_mday; |
|
|
out << " " << setw(2) << setfill('0') << now.tm_hour; |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_min; |
|
|
out << ":" << setw(2) << setfill('0') << now.tm_sec; |
|
|
out << " " << msg << endl; |
|
|
|
|
|
if (this->isDaemon && _logFile != "") |
|
|
{ |
|
|
seteuid(0); |
|
|
ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode |
|
|
if (of) |
|
|
{ |
|
|
of << out.str(); |
|
|
of.flush(); |
|
|
of.close(); |
|
|
} |
|
|
seteuid( this->uid); |
|
|
} |
|
|
else |
|
|
{ |
|
|
cout << out.str(); |
|
|
cout.flush(); |
|
|
} |
|
|
} |
|
62 |
|
|
63 |
void Common::printUsage() |
void Common::printUsage() |
64 |
{ |
{ |
97 |
} |
} |
98 |
else if (current == "--config") |
else if (current == "--config") |
99 |
{ |
{ |
100 |
|
i++; |
101 |
|
if ( i<argc) |
102 |
|
{ |
103 |
|
configFilePath = argv[i]; |
104 |
|
} |
105 |
|
else |
106 |
|
{ |
107 |
|
printUsage(); |
108 |
|
exit(1); |
109 |
|
} |
110 |
|
|
111 |
} |
} |
112 |
else if (current == "-h" || current == "--help") |
else if (current == "-h" || current == "--help") |
113 |
{ |
{ |