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

Annotation of /smsdaemon/Common.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 146 - (hide annotations) (download)
Sun Dec 7 20:06:12 2008 UTC (15 years, 5 months ago) by torben
Original Path: smsdaemon/common.cpp
File size: 3187 byte(s)
Added configuration module

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

  ViewVC Help
Powered by ViewVC 1.1.20