2 * @file main/CLI/main.cpp
3 * @brief The main eVaf CLI application class
5 * Copyright (c) 2011 Enar Vaikene
7 * This file is part of the eVaf C++ cross-platform application development framework.
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.
15 * Alternatively, this file may be used in accordance with the Commercial License
16 * Agreement provided with the Software.
20 #include "exithandler.h"
21 //#include "version_p.h"
24 #include <Common/Globals>
25 #include <Common/iLogger>
26 #include <Common/iApp>
28 #include <Plugins/PluginManager>
33 # include <sys/types.h>
39 //-------------------------------------------------------------------
46 * Qt message handler replacement.
47 * @param type Type of the message
48 * @param msg The message
50 * This function outputs messages to the console and to the log file.
52 static void messageOutput(QtMsgType type
, char const * const msg
)
54 static bool inHandler
= false;
56 // Avoid recursions in case outputting a message causes another message to be output
61 // Qt message type conversion to eVaf logger severity levels
62 Common::iLogger::Severity v
;
65 v
= Common::iLogger::Warning
;
68 v
= Common::iLogger::Error
;
71 v
= Common::iLogger::Fatal
;
74 v
= Common::iLogger::Debug
;
77 // Output to the log file and console
78 Common::iLogger::instance()->write(v
, msg
);
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
89 * This function shows a critical error message box on the screen if needed and then terminates
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.
95 static void fatalMsgHandler(QString
const & msg
, QString
const & source
, QString
const & where
)
104 } // namespace eVaf::CLI::Internal
105 } // namespace eVaf::CLI
108 //-------------------------------------------------------------------
110 using namespace eVaf::CLI
;
112 Application::Application(int & argc
, char ** argv
)
113 : QCoreApplication(argc
, argv
)
115 setObjectName(QString("%1-%2").arg(VER_MODULE_NAME_STR
).arg(__FUNCTION__
));
118 Application::~Application()
122 bool Application::processCommandLine(int argc
, char ** argv
)
124 Common::iLogger::Severity consoleSeverityLevel
= Common::iLogger::Fatal
;
127 for (int i
= 1; i
< argc
; ++i
)
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('='));
134 if (QRegExp("(-[-]?version)|([-//]V)").exactMatch(arg
.at(0))) {
138 else if (QRegExp("(-[-]?help)|([-//][h/?])").exactMatch(arg
.at(0))) {
142 else if (QRegExp("-[-]?verbose").exactMatch(arg
.at(0)) && arg
.size() > 1) {
143 QString v
= arg
.at(1).toLower();
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
;
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
;
169 case Common::iLogger::Fatal
:
170 consoleSeverityLevel
= Common::iLogger::Error
;
172 case Common::iLogger::Error
:
173 consoleSeverityLevel
= Common::iLogger::Warning
;
175 case Common::iLogger::Warning
:
176 consoleSeverityLevel
= Common::iLogger::Info
;
178 case Common::iLogger::Info
:
179 consoleSeverityLevel
= Common::iLogger::Debug
;
181 case Common::iLogger::Debug
:
188 // Set the console severity
189 Common::iLogger::instance()->setConsoleSeverity(consoleSeverityLevel
);
194 void Application::printHelp()
196 char const * const txt
= QT_TR_NOOP(
197 "Usage: eVafCLI [options]\n"
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"
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"
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"
218 ::fputs(tr(txt
).toLocal8Bit().constData(), stdout
);
221 void Application::printVersion()
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
,
233 //-------------------------------------------------------------------
235 int main(int argc
, char ** argv
)
237 Common::iLogger::instance()->setSeverity(Common::iLogger::Warning
);
239 // Install our onw message handlers
240 Common::iLogger::instance()->installFatalMsgHandler(Internal::fatalMsgHandler
);
241 qInstallMsgHandler(Internal::messageOutput
);
243 // Process command-line arguments
244 if (!Application::processCommandLine(argc
, argv
))
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
,
252 VER_FILE_VERSION_STR
);
255 EVAF_INFO("%s application pid = %d", VER_MODULE_NAME_STR
, getpid());
258 Application
app(argc
, argv
);
260 // Install the exit handler
261 if (!Internal::installExitHandler())
265 Plugins::PluginManager pluginManager
;
272 EVAF_INFO("%s is starting up", VER_MODULE_NAME_STR
);
274 // Initialize the common library
278 // Initialize the plugin manager and load plugins
279 if (!pluginManager
.init())
282 // Run the application
283 EVAF_INFO("Running %s", VER_MODULE_NAME_STR
);
286 quit
= rval
!= Common::iApp::RC_Restart
;
288 EVAF_INFO("%s is %s", VER_MODULE_NAME_STR
, quit
? "exiting" : "restarting");
290 // Unload plugins and finalize the plugin manager
291 pluginManager
.done();
294 EVAF_INFO("%s exit with code %d", VER_MODULE_NAME_STR
, rval
);