forked from apsystems/GrblHoming
-
Notifications
You must be signed in to change notification settings - Fork 190
/
main.cpp
204 lines (177 loc) · 5.11 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/****************************************************************
* main.cpp
* GrblHoming - zapmaker fork on github
*
* 15 Nov 2012
* GPL License (see LICENSE file)
* Software is provided AS-IS
****************************************************************/
#include "mainwindow.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include <QtGui/QApplication>
#else
#include <QtWidgets/QApplication>
#endif
enum GC_LOG_TYPES
{
LOG_DEBUG_TYPE = 1,
LOG_ERROR_TYPE,
LOG_WARN_TYPE,
LOG_INFO_TYPE,
LOG_STATUS_TYPE,
};
void logit(GC_LOG_TYPES type, const char *str, va_list args);
FILE *pDebugLogFile = NULL;
AtomicIntBool g_enableDebugLog;
Log4Qt::PatternLayout *p_layout;
Log4Qt::FileAppender *p_fappender;
int main(int argc, char *argv[])
{
// setup logging
Log4Qt::LogManager::rootLogger();
//Log4Qt::TTCCLayout *p_layout = new Log4Qt::TTCCLayout(Log4Qt::TTCCLayout::ISO8601);
//Log4Qt::PatternLayout *p_layout = new Log4Qt::PatternLayout(Log4Qt::PatternLayout::TTCC_CONVERSION_PATTERN);
p_layout = new Log4Qt::PatternLayout("%d %p (%c) - %m%n");
p_layout->setName(QLatin1String("GC Basic Layout"));
p_layout->activateOptions();
// Create a console appender
Log4Qt::ConsoleAppender *p_appender = new Log4Qt::ConsoleAppender(p_layout, Log4Qt::ConsoleAppender::STDOUT_TARGET);
p_appender->setName(QLatin1String("GC Basic Console Appender"));
#ifdef QT_DEBUG
p_appender->setThreshold(Log4Qt::Level::TRACE_INT);
#else
p_appender->setThreshold(Log4Qt::Level::WARN_INT);
#endif
p_appender->activateOptions();
Log4Qt::Logger::rootLogger()->addAppender(p_appender);
// Create a file appender
p_fappender = new Log4Qt::FileAppender();
p_fappender->setLayout(p_layout);
p_fappender->setThreshold(Log4Qt::Level::TRACE_INT);
p_fappender->setFile(QDir::homePath() + "/GrblController.log");
p_fappender->setName(QLatin1String("GC Basic File Appender"));
// don't open until ready (later during startup)
//p_fappender->activateOptions();
//Log4Qt::Logger::rootLogger()->addAppender(p_fappender);
QApplication a(argc, argv);
QString locale = QLocale::system().name().section('_', 0, 0);
QString dir = QDir::currentPath() + "/trlocale";
QString file = QString("GrblController_") + locale;
QTranslator translator;
bool r = translator.load(file, dir);
if (!r)
{
dir = QDir::currentPath() + "/../GrblHoming/trlocale";
translator.load(file, dir);
}
a.installTranslator(&translator);
QString xlatpath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
file = QString("qt_") + locale ;
QTranslator qtTranslator;
r = qtTranslator.load(file , xlatpath);
if (!r)
{
xlatpath = QDir::currentPath() + "/trlocale";
qtTranslator.load(file, xlatpath);
}
a.installTranslator(&qtTranslator);
MainWindow w;
w.show();
int result = a.exec();
if (pDebugLogFile != NULL)
{
fclose(pDebugLogFile);
pDebugLogFile = NULL;
}
return result;
}
//------------------------------
void status(const char *str, ...)
{
#ifndef QT_DEBUG
if (g_enableDebugLog.get()) {
#endif
va_list args;
va_start(args, str );
logit(LOG_STATUS_TYPE, str, args);
va_end(args);
#ifndef QT_DEBUG
}
#endif
}
void err(const char *str, ...)
{
va_list args;
va_start(args, str );
logit(LOG_ERROR_TYPE, str, args);
va_end(args);
}
void warn(const char *str, ...)
{
va_list args;
va_start(args, str );
logit(LOG_WARN_TYPE, str, args);
va_end(args);
}
void info(const char *str, ...)
{
va_list args;
va_start(args, str );
logit(LOG_INFO_TYPE, str, args);
va_end(args);
}
void diag(const char *str, ...)
{
#ifndef QT_DEBUG
if (g_enableDebugLog.get()) {
#endif
va_list args;
va_start(args, str );
logit(LOG_DEBUG_TYPE, str, args);
va_end(args);
#ifndef QT_DEBUG
}
#endif
}
void logit(GC_LOG_TYPES type, const char *str, va_list args)
{
#define PRNTBUFSIZE 500
char buf[PRNTBUFSIZE];
buf[PRNTBUFSIZE-1] = '\0';
vsnprintf(buf, sizeof(buf) - 1, str, args);
int len = strlen(buf);
if (len > 0)
{
if (len == 1 && (buf[0] == '\r' || buf[0] == '\n'))
{
buf[0] = '\0';
}
else if (len > 1)
{
if (buf[len - 2] == '\r' || buf[len - 2] == '\n')
buf[len - 2] = '\0';
else if (buf[len - 1] == '\r' || buf[len - 1] == '\n')
buf[len - 1] = '\0';
}
}
else if (len == 0)
return;
switch (type)
{
case LOG_STATUS_TYPE:
Log4Qt::Logger::logger(LOG_MSG_TYPE_STATUS)->info(buf);
break;
case LOG_DEBUG_TYPE:
Log4Qt::Logger::logger(LOG_MSG_TYPE_DIAG)->debug(buf);
break;
case LOG_ERROR_TYPE:
Log4Qt::Logger::logger(LOG_MSG_TYPE_DIAG)->error(buf);
break;
case LOG_WARN_TYPE:
Log4Qt::Logger::logger(LOG_MSG_TYPE_DIAG)->warn(buf);
break;
case LOG_INFO_TYPE:
Log4Qt::Logger::logger(LOG_MSG_TYPE_DIAG)->info(buf);
break;
}
}