1 |
#include "common.h" |
#include "Common.h" |
2 |
|
|
3 |
|
|
4 |
|
#include "version.h" |
5 |
|
|
6 |
|
#include <stdlib.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 |
|
|
13 |
|
#include "TaskManager.h" |
14 |
|
#include "PluginManager.h" |
15 |
|
#include "ConfigFile.h" |
16 |
|
|
17 |
using namespace std; |
using namespace std; |
|
char* Months[] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"}; |
|
18 |
|
|
19 |
Common* Common::instance() |
Common* Common::instance() |
20 |
{ |
{ |
23 |
} |
} |
24 |
|
|
25 |
|
|
26 |
void Common::logMessage(string msg) |
Common::Common() |
27 |
{ |
: _pluginManager(0), _taskManager(0) |
28 |
time_t t = time(0); |
{ |
29 |
tm now; |
_pluginManager = new PluginManager(); |
30 |
localtime_r(&t, &now); |
_taskManager = new TaskManager(); |
31 |
|
configFilePath = "/etc/smsdaemon.conf"; |
32 |
ostringstream out; |
_configFile = new ConfigFile(); |
|
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; |
|
33 |
|
|
34 |
if (this->isDaemon && _logFile != "") |
daemonized = false; |
35 |
{ |
reloadConfig = false; |
36 |
seteuid(0); |
} |
37 |
ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode |
|
38 |
if (of) |
|
39 |
{ |
Common::~Common() |
40 |
of << out.str(); |
{ |
41 |
of.flush(); |
if (_pluginManager) |
42 |
of.close(); |
delete _pluginManager; |
43 |
} |
|
44 |
seteuid( this->uid); |
if (_taskManager) |
45 |
} |
delete _taskManager; |
|
else |
|
|
{ |
|
|
cout << out.str(); |
|
|
cout.flush(); |
|
|
} |
|
46 |
} |
} |
47 |
|
|
48 |
|
PluginManager* Common::GetPluginManager() |
49 |
|
{ |
50 |
|
return _pluginManager; |
51 |
|
} |
52 |
|
|
53 |
|
TaskManager* Common::GetTaskManager() |
54 |
|
{ |
55 |
|
return _taskManager; |
56 |
|
} |
57 |
|
|
58 |
|
ConfigFile* Common::GetConfigfile() |
59 |
|
{ |
60 |
|
return _configFile; |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
void Common::printUsage() |
void Common::printUsage() |
65 |
{ |
{ |
66 |
cout << VERSION << endl; |
cout << VERSION << endl; |
67 |
|
cout << SVNVERSION << endl << endl; |
68 |
cout << "Usage --daemon|--debug [arguments]" << endl; |
cout << "Usage --daemon|--debug [arguments]" << endl; |
69 |
cout << "Commandline arguments :" << endl; |
cout << "Commandline arguments :" << endl; |
70 |
cout << " --daemon : Run in daemon (background) mode" << endl; |
cout << " --daemon : Run in daemon (background) mode" << endl; |
71 |
cout << " --debug : Run as normal (frontground) process," << endl; |
cout << " --debug : Run as normal (frontground) process," << endl; |
72 |
cout << " with all messages written to std out" << endl; |
cout << " with all messages written to std out" << endl; |
73 |
cout << " --help : Show this help screen" << endl; |
cout << " --config <file>: Specify which config file to use " << endl; |
74 |
|
cout << " default" << configFilePath << endl; |
75 |
|
cout << " --help : Show this help screen" << endl; |
76 |
|
|
77 |
exit(0); |
exit(0); |
78 |
} |
} |
85 |
for (int i = 1; i< argc; i++) |
for (int i = 1; i< argc; i++) |
86 |
{ |
{ |
87 |
string current(argv[i]); |
string current(argv[i]); |
88 |
if (current == "--daemon") |
if (current == "--daemon") |
89 |
{ |
{ |
90 |
isDaemon = true; |
isDaemon = true; |
91 |
isDebug = false; |
isDebug = false; |
92 |
|
|
93 |
} |
} |
94 |
else if (current == "--debug") |
else if (current == "--debug") |
95 |
{ |
{ |
96 |
isDaemon = false; |
isDaemon = false; |
97 |
isDebug = true; |
isDebug = true; |
98 |
} |
} |
99 |
|
else if (current == "--config") |
100 |
|
{ |
101 |
|
i++; |
102 |
|
if ( i<argc) |
103 |
|
{ |
104 |
|
configFilePath = argv[i]; |
105 |
|
} |
106 |
|
else |
107 |
|
{ |
108 |
|
printUsage(); |
109 |
|
exit(1); |
110 |
|
} |
111 |
|
|
112 |
|
} |
113 |
else if (current == "-h" || current == "--help") |
else if (current == "-h" || current == "--help") |
114 |
{ |
{ |
115 |
printUsage(); |
printUsage(); |
116 |
} |
} |
117 |
else |
else |
118 |
{ |
{ |
119 |
cout << VERSION; |
cout << "Unknown argument : " << current << endl << endl; |
|
cout << "Unknown argument : " << current << endl; |
|
120 |
} |
} |
121 |
} |
} |
122 |
|
|
136 |
out << "Uptime " << diff << " seconds. "; |
out << "Uptime " << diff << " seconds. "; |
137 |
out << "Messages received:" << this->smsCounter.incomming << ". "; |
out << "Messages received:" << this->smsCounter.incomming << ". "; |
138 |
out << "Messages sent:" << this->smsCounter.outgoing << "."; |
out << "Messages sent:" << this->smsCounter.outgoing << "."; |
139 |
|
|
140 |
return out.str(); |
return out.str(); |
141 |
} |
} |