1 |
#include "common.h" |
2 |
|
3 |
|
4 |
#include "version.h" |
5 |
|
6 |
#include <string> |
7 |
#include <iostream> |
8 |
#include <iomanip> |
9 |
#include <fstream> |
10 |
#include <sstream> |
11 |
#include <time.h> |
12 |
|
13 |
#include "TaskManager.h" |
14 |
#include "PluginManager.h" |
15 |
|
16 |
using namespace std; |
17 |
const char* Months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"}; |
18 |
|
19 |
Common* Common::instance() |
20 |
{ |
21 |
static Common store; |
22 |
return &store; |
23 |
} |
24 |
|
25 |
|
26 |
Common::Common() |
27 |
: _pluginManager(0), _taskManager(0) |
28 |
{ |
29 |
_pluginManager = new PluginManager(); |
30 |
_taskManager = new TaskManager(); |
31 |
} |
32 |
|
33 |
|
34 |
Common::~Common() |
35 |
{ |
36 |
if (_pluginManager) |
37 |
delete _pluginManager; |
38 |
|
39 |
if (_taskManager) |
40 |
delete _taskManager; |
41 |
} |
42 |
|
43 |
PluginManager* Common::GetPluginManager() |
44 |
{ |
45 |
return _pluginManager; |
46 |
} |
47 |
|
48 |
TaskManager* Common::GetTaskManager() |
49 |
{ |
50 |
return _taskManager; |
51 |
} |
52 |
|
53 |
void Common::logMessage(string msg) |
54 |
{ |
55 |
time_t t = time(0); |
56 |
tm now; |
57 |
localtime_r(&t, &now); |
58 |
|
59 |
ostringstream out; |
60 |
out << Months[ now.tm_mon ] << " " << setw(2) << setfill('0') << now.tm_mday; |
61 |
out << " " << setw(2) << setfill('0') << now.tm_hour; |
62 |
out << ":" << setw(2) << setfill('0') << now.tm_min; |
63 |
out << ":" << setw(2) << setfill('0') << now.tm_sec; |
64 |
out << " " << msg << endl; |
65 |
|
66 |
if (this->isDaemon && _logFile != "") |
67 |
{ |
68 |
seteuid(0); |
69 |
ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode |
70 |
if (of) |
71 |
{ |
72 |
of << out.str(); |
73 |
of.flush(); |
74 |
of.close(); |
75 |
} |
76 |
seteuid( this->uid); |
77 |
} |
78 |
else |
79 |
{ |
80 |
cout << out.str(); |
81 |
cout.flush(); |
82 |
} |
83 |
} |
84 |
|
85 |
void Common::printUsage() |
86 |
{ |
87 |
cout << VERSION << endl; |
88 |
cout << SVNVERSION << endl << endl; |
89 |
cout << "Usage --daemon|--debug [arguments]" << endl; |
90 |
cout << "Commandline arguments :" << endl; |
91 |
cout << " --daemon : Run in daemon (background) mode" << endl; |
92 |
cout << " --debug : Run as normal (frontground) process," << endl; |
93 |
cout << " with all messages written to std out" << endl; |
94 |
cout << " --help : Show this help screen" << endl; |
95 |
|
96 |
exit(0); |
97 |
} |
98 |
|
99 |
void Common::loadConfig(int argc, char* argv[]) |
100 |
{ |
101 |
isDaemon = false; |
102 |
isDebug = false; |
103 |
|
104 |
for (int i = 1; i< argc; i++) |
105 |
{ |
106 |
string current(argv[i]); |
107 |
if (current == "--daemon") |
108 |
{ |
109 |
isDaemon = true; |
110 |
isDebug = false; |
111 |
|
112 |
} |
113 |
else if (current == "--debug") |
114 |
{ |
115 |
isDaemon = false; |
116 |
isDebug = true; |
117 |
} |
118 |
else if (current == "-h" || current == "--help") |
119 |
{ |
120 |
printUsage(); |
121 |
} |
122 |
else |
123 |
{ |
124 |
cout << "Unknown argument : " << current << endl << endl; |
125 |
} |
126 |
} |
127 |
|
128 |
if (! ( isDaemon || isDebug)) |
129 |
printUsage(); |
130 |
} |
131 |
|
132 |
|
133 |
std::string Common::getStatusMessage() |
134 |
{ |
135 |
long int now = time(0); |
136 |
|
137 |
long int diff = now - this->daemonStart; |
138 |
|
139 |
|
140 |
std::ostringstream out; |
141 |
out << "Uptime " << diff << " seconds. "; |
142 |
out << "Messages received:" << this->smsCounter.incomming << ". "; |
143 |
out << "Messages sent:" << this->smsCounter.outgoing << "."; |
144 |
|
145 |
return out.str(); |
146 |
} |