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

Annotation of /smsdaemon/Common.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 114 - (hide annotations) (download)
Sun Nov 2 20:14:20 2008 UTC (15 years, 6 months ago) by torben
Original Path: smsdaemon/common.cpp
File size: 2823 byte(s)
Enable compilation 4.3

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    
17 torben 26 using namespace std;
18 torben 109 const char* Months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"};
19 torben 26
20     Common* Common::instance()
21     {
22     static Common store;
23     return &store;
24     }
25    
26    
27 torben 92 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 torben 26 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 torben 106 cout << VERSION << endl;
89     cout << SVNVERSION << endl << endl;
90 torben 26 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 torben 106 cout << "Unknown argument : " << current << endl << endl;
126 torben 26 }
127     }
128    
129     if (! ( isDaemon || isDebug))
130     printUsage();
131     }
132 torben 36
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     }

  ViewVC Help
Powered by ViewVC 1.1.20