]> vaikene.ee Git - evaf/blob - src/main/CLI/main.cpp
Started working on the LogView module.
[evaf] / src / main / CLI / main.cpp
1 /**
2 * @file main/CLI/main.cpp
3 * @brief The main eVaf CLI application class
4 *
5 * Copyright (c) 2011 Enar Vaikene
6 *
7 * This file is part of the eVaf C++ cross-platform application development framework.
8 *
9 * This file can be used under the terms of the GNU General Public License
10 * version 3.0 as published by the Free Software Foundation and appearing in
11 * the file LICENSE included in the packaging of this file. Please review the
12 * the following information to ensure the GNU General Public License version
13 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
14 *
15 * Alternatively, this file may be used in accordance with the Commercial License
16 * Agreement provided with the Software.
17 */
18
19 #include "main.h"
20 #include "exithandler.h"
21 //#include "version_p.h"
22 #include "version.h"
23
24 #include <Common/Globals>
25 #include <Common/iLogger>
26 #include <Common/iApp>
27
28 #include <Plugins/PluginManager>
29
30 #include <QtCore>
31
32 #ifdef Q_OS_LINUX
33 # include <sys/types.h>
34 # include <unistd.h>
35 #endif
36
37 using namespace eVaf;
38
39 //-------------------------------------------------------------------
40
41 namespace eVaf {
42 namespace CLI {
43 namespace Internal {
44
45 /**
46 * Qt message handler replacement.
47 * @param type Type of the message
48 * @param msg The message
49 *
50 * This function outputs messages to the console and to the log file.
51 */
52 static void messageOutput(QtMsgType type, char const * const msg)
53 {
54 static bool inHandler = false;
55
56 // Avoid recursions in case outputting a message causes another message to be output
57 if (inHandler)
58 return;
59 inHandler = true;
60
61 // Qt message type conversion to eVaf logger severity levels
62 Common::iLogger::Severity v;
63 switch (type) {
64 case QtWarningMsg:
65 v = Common::iLogger::Warning;
66 break;
67 case QtCriticalMsg:
68 v = Common::iLogger::Error;
69 break;
70 case QtFatalMsg:
71 v = Common::iLogger::Fatal;
72 break;
73 default:
74 v = Common::iLogger::Debug;
75 }
76
77 // Output to the log file and console
78 Common::iLogger::instance()->write(v, msg);
79
80 inHandler = false;
81 }
82
83 /**
84 * Fatal error message handler
85 * @param msg The error message
86 * @param source Source of the message
87 * @param where Where the error occurred
88 *
89 * This function shows a critical error message box on the screen if needed and then terminates
90 * the application.
91 *
92 * If the critical error message is shown, then the user has an option to ignore the error. In this
93 * case the application is not terminated.
94 */
95 static void fatalMsgHandler(QString const & msg, QString const & source, QString const & where)
96 {
97 #ifdef Q_OS_LINUX
98 abort();
99 #else
100 exit(1);
101 #endif
102 }
103
104 } // namespace eVaf::CLI::Internal
105 } // namespace eVaf::CLI
106 } // namespace eVaf
107
108 //-------------------------------------------------------------------
109
110 using namespace eVaf::CLI;
111
112 Application::Application(int & argc, char ** argv)
113 : QCoreApplication(argc, argv)
114 {
115 setObjectName(QString("%1-%2").arg(VER_MODULE_NAME_STR).arg(__FUNCTION__));
116 }
117
118 Application::~Application()
119 {
120 }
121
122 bool Application::processCommandLine(int argc, char ** argv)
123 {
124 Common::iLogger::Severity consoleSeverityLevel = Common::iLogger::Fatal;
125
126 QStringList args;
127 for (int i = 1; i < argc; ++i)
128 args += argv[i];
129
130 for (int i = 0; i < args.size(); ++i) {
131 // Get the argument and optional value
132 QStringList arg = args.at(i).simplified().split(QChar('='));
133
134 if (QRegExp("(-[-]?version)|([-//]V)").exactMatch(arg.at(0))) {
135 printVersion();
136 return false;
137 }
138 else if (QRegExp("(-[-]?help)|([-//][h/?])").exactMatch(arg.at(0))) {
139 printHelp();
140 return false;
141 }
142 else if (QRegExp("-[-]?verbose").exactMatch(arg.at(0)) && arg.size() > 1) {
143 QString v = arg.at(1).toLower();
144 if (v == "debug")
145 consoleSeverityLevel = Common::iLogger::Debug;
146 else if (v == "info")
147 consoleSeverityLevel = Common::iLogger::Info;
148 else if (v == "warning")
149 consoleSeverityLevel = Common::iLogger::Warning;
150 else if (v == "error")
151 consoleSeverityLevel = Common::iLogger::Error;
152 else if (v == "fatal")
153 consoleSeverityLevel = Common::iLogger::Fatal;
154 else if (v == "none") {
155 consoleSeverityLevel = Common::iLogger::None;
156 }
157 else {
158 printHelp();
159 return false;
160 }
161 }
162 else if (QRegExp("-[v]+").exactMatch(arg.at(0)) && arg.size() == 1) {
163 // The number of 'v's increases the verbosity
164 for (int j = 1; j < arg.at(0).size(); ++j) {
165 switch (consoleSeverityLevel) {
166 case Common::iLogger::None:
167 consoleSeverityLevel = Common::iLogger::Fatal;
168 break;
169 case Common::iLogger::Fatal:
170 consoleSeverityLevel = Common::iLogger::Error;
171 break;
172 case Common::iLogger::Error:
173 consoleSeverityLevel = Common::iLogger::Warning;
174 break;
175 case Common::iLogger::Warning:
176 consoleSeverityLevel = Common::iLogger::Info;
177 break;
178 case Common::iLogger::Info:
179 consoleSeverityLevel = Common::iLogger::Debug;
180 break;
181 case Common::iLogger::Debug:
182 break;
183 }
184 }
185 }
186 }
187
188 // Set the console severity
189 Common::iLogger::instance()->setConsoleSeverity(consoleSeverityLevel);
190
191 return true;
192 }
193
194 void Application::printHelp()
195 {
196 char const * const txt = QT_TR_NOOP(
197 "Usage: eVafCLI [options]\n"
198 "\n"
199 // General options
200 " -help Shows this help and quits.\n"
201 " -version Shows version information and quits.\n"
202 " -verbose=LEVEL Specifies the verbose level. LEVEL can be one of the\n"
203 " following: NONE, FATAL, ERROR, WARNING, INFO, DEBUG.\n"
204 " -v Makes the application more verbose. Can be repeated for\n"
205 " more verbosity.\n"
206 // Handled by the iApp interface implementation
207 " -appl[ication]=NAME Specifies the name of the application.\n"
208 " -lang[uage]=xx[_CC] Specifies the language, where xx is the ISO 639\n"
209 " language code followed by an optional ISO 3166 country\n"
210 " code.\n"
211 " -root[dir]=DIR Specifies the application's root directory.\n"
212 " -dataroot[dir]=DIR Specifies the data root directory.\n"
213 " -etc[dir]=DIR Specifies the configuration files directory.\n"
214 " -log[dir]=DIR Specifies the log files directory.\n"
215 " -doc[dir]=DIR Specifies the documentation directory.\n"
216 " -qtplugins[dir]=DIR Specifies the Qt plugins directory.\n"
217 );
218 ::fputs(tr(txt).toLocal8Bit().constData(), stdout);
219 }
220
221 void Application::printVersion()
222 {
223 ::printf("%s version %s release date %s, %s version %s\n",
224 VER_PRODUCT_NAME_STR,
225 VER_PRODUCT_VERSION_STR,
226 VER_PRODUCT_DATE_STR,
227 VER_MODULE_NAME_STR,
228 VER_FILE_VERSION_STR
229 );
230 }
231
232
233 //-------------------------------------------------------------------
234
235 int main(int argc, char ** argv)
236 {
237 Common::iLogger::instance()->setSeverity(Common::iLogger::Warning);
238
239 // Install our onw message handlers
240 Common::iLogger::instance()->installFatalMsgHandler(Internal::fatalMsgHandler);
241 qInstallMsgHandler(Internal::messageOutput);
242
243 // Process command-line arguments
244 if (!Application::processCommandLine(argc, argv))
245 return 1;
246
247 EVAF_INFO("%s version %s release date %s, %s version %s",
248 VER_PRODUCT_NAME_STR,
249 VER_PRODUCT_VERSION_STR,
250 VER_PRODUCT_DATE_STR,
251 VER_MODULE_NAME_STR,
252 VER_FILE_VERSION_STR);
253
254 #ifdef Q_OS_LINUX
255 EVAF_INFO("%s application pid = %d", VER_MODULE_NAME_STR, getpid());
256 #endif
257
258 Application app(argc, argv);
259
260 // Install the exit handler
261 if (!Internal::installExitHandler())
262 return 1;
263
264 // Plugin manager
265 Plugins::PluginManager pluginManager;
266
267 // The main run loop
268 bool quit = false;
269 int rval;
270 while (!quit) {
271
272 EVAF_INFO("%s is starting up", VER_MODULE_NAME_STR);
273
274 // Initialize the common library
275 if (!Common::init())
276 return 1;
277
278 // Initialize the plugin manager and load plugins
279 if (!pluginManager.init())
280 return 1;
281
282 // Run the application
283 EVAF_INFO("Running %s", VER_MODULE_NAME_STR);
284 rval = app.exec();
285
286 quit = rval != Common::iApp::RC_Restart;
287
288 EVAF_INFO("%s is %s", VER_MODULE_NAME_STR, quit ? "exiting" : "restarting");
289
290 // Unload plugins and finalize the plugin manager
291 pluginManager.done();
292 }
293
294 EVAF_INFO("%s exit with code %d", VER_MODULE_NAME_STR, rval);
295
296 return rval;
297 }