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