-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
183 lines (168 loc) · 6.44 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//------------------------------------------------------------------------------
// Home Office
// Nürnberg, Germany
// E-Mail: sergej1@email.ua
//
// Copyright (C) 2017/2018 free Project Crypto. All rights reserved.
//------------------------------------------------------------------------------
// Project: Crypto - Advanced File Encryptor, based on simple XOR and
// reliable AES methods
//------------------------------------------------------------------------------
/**
* @mainpage Data encryption program Crypto.
*
* Crypto - Advanced File Encryptor, based on simple XOR and reliable AES methods
*
* @author SergejBre sergej1@email.ua
*/
/**
* @file main.cpp
*
* @brief The file contains two important functions, main() and logMessageOutput()
*
* The main function executes an instance of a GUI Qt application,
* sets it up with the specified special parameters, and installs
* a Qt message handler defined in the logMessageOutput function.
* In addition, a log journal of the application messages is set up.
*/
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "mainwindow.h"
#include "settings.h"
#include <QApplication>
#include <QLoggingCategory>
#include <QDateTime>
#include <QFile>
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
#define ONEKB 1024
Q_LOGGING_CATEGORY(logMain, "main")
// Smart pointer an the log-file
static QScopedPointer<QFile> m_logFile;
//------------------------------------------------------------------------------
// Function Prototypes
//------------------------------------------------------------------------------
void logMessageOutput( const QtMsgType type, const QMessageLogContext &context, const QString &msg );
/**
* @brief main function
*
* In this function, an instance of a GUI Qt application app is executed and
* set up with the parameters entered.
*
* @param argc this parameter is ignored because it is a GUI application.
* @param argv this parameter is ignored because it is a GUI application.
*
* @return value of the function QApplication::exec()
* Enters the main event loop and waits until exit() is called.
* Returns the value that was set to exit() (which is 0 if exit() is called
* via quit()).
*
* @note
* The program parameters (argc, argv) are ignored.
* @warning
* none
*/
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setOrganizationName( "FreeProject" );
app.setOrganizationDomain( "free.project.org" );
app.setApplicationName( "Crypto" );
app.setApplicationDisplayName( "Crypto - Advanced File Encryptor." );
app.setApplicationVersion( "1.0.1.0, built on: " + QString(__DATE__).simplified() );
MainWindow w;
w.show();
bool errorFlag = false;
if( w.getSettings()->enableLog )
{
// Set the log file in the work directory
m_logFile.reset( new QFile( w.getSettings()->pathToLog ) );
if ( m_logFile.data()->size() < w.getSettings()->maxSizeLog * ONEKB )
{
errorFlag = m_logFile.data()->open( QIODevice::Append | QIODevice::Text );
}
else
{
errorFlag = m_logFile.data()->open( QIODevice::WriteOnly | QIODevice::Text );
}
if ( errorFlag )
{
qInstallMessageHandler( logMessageOutput );
}
else
{
qInstallMessageHandler( 0 );
qCritical( logMain ) << QObject::tr( "To the log file %1 can not be added." ).arg( m_logFile.data()->fileName() );
w.wErrorMessage( QObject::tr( "To the log file %1 can not be added." ).arg( m_logFile.data()->fileName() ) );
m_logFile.reset();
}
}
qInfo( logMain ) << QObject::tr( "App Crypto is running, ver%1" ).arg( app.applicationVersion() );
return app.exec();
}
/**
* @brief The function logMessageOutput is a message handler.
*
* This function redirects the messages by their category (QtDebugMsg,
* QtInfoMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg) to the log file (m_logFile).
* The message handler is a function that prints out debug messages,
* warnings, critical and fatal error messages. The Qt library (debug mode)
* contains hundreds of warning messages that are printed when internal errors
* (usually invalid function arguments) occur. Qt built in release mode
* also contains such warnings unless QT_NO_WARNING_OUTPUT and/
* or QT_NO_DEBUG_OUTPUT have been set during compilation.
* If you implement your own message handler, you get total control of these messages.
*
* @param[in] type of the type QtMsgType
* @param[in] context of type QMessageLogContext
* @param[in] msg of the type QString
*
* @note
* - The output of messages is also output to the terminal console. This is for debugging purposes.
* - Additional information, such as a line of code, the name of the source file, function names
* cannot be displayed for the release of the program.
*
* @warning
* - The default message handler prints the message to the standard output
* under X11 or to the debugger under Windows. If it is a fatal message,
* the application aborts immediately.
* - Only one message handler can be defined, since this is usually done on
* an application-wide basis to control debug output.
*/
void logMessageOutput( const QtMsgType type, const QMessageLogContext &context, const QString &msg )
{
QTextStream out( m_logFile.data() );
// Write the date of the recording
out << QDateTime::currentDateTime().toString( "yyyy-MM-dd hh:mm:ss.zzz " );
QByteArray localMsg = msg.toLocal8Bit();
switch ( type )
{
case QtDebugMsg:
out << "DBG ";
break;
case QtInfoMsg:
out << "INF ";
break;
case QtWarningMsg:
out << "WRN ";
break;
case QtCriticalMsg:
out << "CRT ";
break;
case QtFatalMsg:
out << "FTL ";
break;
default :
out << "ERR ";
}
#ifdef DEBUG_OUTPUT
out << context.category << ": " << msg << " (" << context.file << ":" << context.line << ", " << context.function << ")" << "<br />" << endl;
#else // only for release
out << context.category << ": " << msg << "<br />" << endl;
#endif
out.flush();
// Output messages to the terminal console. Only for debugging purposes.
fprintf( stdout, "%s\n", localMsg.constData() );
}