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