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