/[projects]/smsdaemon/Common.cpp
ViewVC logotype

Annotation of /smsdaemon/Common.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 94 - (hide annotations) (download)
Mon Jun 16 11:57:48 2008 UTC (15 years, 11 months ago) by torben
Original Path: smsdaemon/common.cpp
File size: 2790 byte(s)
Testing keyword property substitution

1 torben 26 #include "common.h"
2    
3 torben 94
4     #include "version.h"
5    
6 torben 26 #include <string>
7     #include <iostream>
8     #include <iomanip>
9     #include <fstream>
10     #include <sstream>
11     #include <time.h>
12    
13 torben 92 #include "TaskManager.h"
14     #include "PluginManager.h"
15    
16 torben 26 using namespace std;
17 torben 55 const char* Months[] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"};
18 torben 26
19     Common* Common::instance()
20     {
21     static Common store;
22     return &store;
23     }
24    
25    
26 torben 92 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 torben 26 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 torben 94 cout << VERSION << endl << endl;
88 torben 26 cout << "Usage --daemon|--debug [arguments]" << endl;
89     cout << "Commandline arguments :" << endl;
90     cout << " --daemon : Run in daemon (background) mode" << endl;
91     cout << " --debug : Run as normal (frontground) process," << endl;
92     cout << " with all messages written to std out" << endl;
93     cout << " --help : Show this help screen" << endl;
94    
95     exit(0);
96     }
97    
98     void Common::loadConfig(int argc, char* argv[])
99     {
100     isDaemon = false;
101     isDebug = false;
102    
103     for (int i = 1; i< argc; i++)
104     {
105     string current(argv[i]);
106     if (current == "--daemon")
107     {
108     isDaemon = true;
109     isDebug = false;
110    
111     }
112     else if (current == "--debug")
113     {
114     isDaemon = false;
115     isDebug = true;
116     }
117     else if (current == "-h" || current == "--help")
118     {
119     printUsage();
120     }
121     else
122     {
123     cout << VERSION;
124     cout << "Unknown argument : " << current << endl;
125     }
126     }
127    
128     if (! ( isDaemon || isDebug))
129     printUsage();
130     }
131 torben 36
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     }

  ViewVC Help
Powered by ViewVC 1.1.20