forked from Stellarium/stellarium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StelLogger.cpp
290 lines (255 loc) · 8.53 KB
/
StelLogger.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Stellarium
* Copyright (C) 2009 Fabien Chereau
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/
#include "StelLogger.hpp"
#include "StelUtils.hpp"
#include <QDateTime>
#include <QProcess>
#ifdef Q_OS_WIN
#include <Windows.h>
#endif
// Init statics variables.
QFile StelLogger::logFile;
QString StelLogger::log;
QMutex StelLogger::fileMutex;
void StelLogger::init(const QString& logFilePath)
{
logFile.setFileName(logFilePath);
if (logFile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text | QIODevice::Unbuffered))
qInstallMessageHandler(StelLogger::debugLogHandler);
// write timestamp
writeLog(QString("%1").arg(QDateTime::currentDateTime().toString(Qt::ISODate)));
// write info about operating system
writeLog(QString("Operating System: %1").arg(StelUtils::getOperatingSystemInfo()));
// write compiler version
QString compiler = StelUtils::getCompilerInfo();
writeLog(compiler.isEmpty() ? "Unknown compiler" : QString("Compiled using %1").arg(compiler));
// write Qt version
writeLog(QString("Qt runtime version: %1").arg(qVersion()));
writeLog(QString("Qt compilation version: %1").arg(QT_VERSION_STR));
// write addressing mode
#if defined(__LP64__) || defined(_WIN64)
writeLog("Addressing mode: 64-bit");
#else
writeLog("Addressing mode: 32-bit");
#endif
// write memory and CPU info
#ifdef Q_OS_LINUX
#ifndef BUILD_FOR_MAEMO
QFile infoFile("/proc/meminfo");
if(!infoFile.open(QIODevice::ReadOnly | QIODevice::Text))
writeLog("Could not get memory info.");
else
{
while(!infoFile.peek(1).isEmpty())
{
QString line = infoFile.readLine();
line.chop(1);
if (line.startsWith("Mem") || line.startsWith("SwapTotal"))
writeLog(line);
}
infoFile.close();
}
infoFile.setFileName("/proc/cpuinfo");
if (!infoFile.open(QIODevice::ReadOnly | QIODevice::Text))
writeLog("Could not get CPU info.");
else
{
while(!infoFile.peek(1).isEmpty())
{
QString line = infoFile.readLine();
line.chop(1);
if(line.startsWith("model name") || line.startsWith("cpu MHz"))
writeLog(line);
}
infoFile.close();
}
QProcess lspci;
lspci.start("lspci", { "-v" }, QIODevice::ReadOnly);
lspci.waitForFinished(300);
const QString pciData(lspci.readAll());
#if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0))
QStringList pciLines = pciData.split('\n', Qt::SkipEmptyParts);
#else
QStringList pciLines = pciData.split('\n', QString::SkipEmptyParts);
#endif
for (int i = 0; i<pciLines.size(); i++)
{
if(pciLines.at(i).contains("VGA compatible controller"))
{
writeLog(pciLines.at(i));
i++;
while(i < pciLines.size() && pciLines.at(i).startsWith('\t'))
{
if(pciLines.at(i).contains("Kernel driver in use"))
writeLog(pciLines.at(i).trimmed());
else if(pciLines.at(i).contains("Kernel modules"))
writeLog(pciLines.at(i).trimmed());
i++;
}
}
}
#endif
// Aargh Windows API
#elif defined Q_OS_WIN
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx(&statex);
writeLog(QString("Total physical memory: %1 MB").arg(statex.ullTotalPhys/(1024<<10)));
writeLog(QString("Available physical memory: %1 MB").arg(statex.ullAvailPhys/(1024<<10)));
writeLog(QString("Physical memory in use: %1%").arg(statex.dwMemoryLoad));
#ifndef _WIN64
// This always reports about 8TB on Win64, not really useful to show.
writeLog(QString("Total virtual memory: %1 MB").arg(statex.ullTotalVirtual/(1024<<10)));
writeLog(QString("Available virtual memory: %1 MB").arg(statex.ullAvailVirtual/(1024<<10)));
#endif
HKEY hKey = Q_NULLPTR;
DWORD dwType = REG_DWORD;
DWORD numVal = 0;
DWORD dwSize = sizeof(numVal);
// iterate over the processors listed in the registry
QString procKey = "Hardware\\Description\\System\\CentralProcessor";
LONG lRet = ERROR_SUCCESS;
int i;
for(i = 0; lRet == ERROR_SUCCESS; i++)
{
lRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
qPrintable(QString("%1\\%2").arg(procKey).arg(i)),
0, KEY_QUERY_VALUE, &hKey);
if(lRet == ERROR_SUCCESS)
{
if(RegQueryValueExA(hKey, "~MHz", Q_NULLPTR, &dwType, reinterpret_cast<LPBYTE>(&numVal), &dwSize) == ERROR_SUCCESS)
writeLog(QString("Processor speed: %1 MHz").arg(numVal));
else
writeLog("Could not get processor speed.");
}
// can you believe this trash?
dwType = REG_SZ;
char nameStr[512];
DWORD nameSize = sizeof(nameStr);
if (lRet == ERROR_SUCCESS)
{
if (RegQueryValueExA(hKey, "ProcessorNameString", Q_NULLPTR, &dwType, reinterpret_cast<LPBYTE>(&nameStr), &nameSize) == ERROR_SUCCESS)
writeLog(QString("Processor name: %1").arg(nameStr));
else
writeLog("Could not get processor name.");
}
RegCloseKey(hKey);
}
if(i == 0)
writeLog("Could not get processor info.");
#elif defined Q_OS_MACOS
QProcess systemProfiler;
systemProfiler.start("/usr/sbin/system_profiler", {"-detailLevel mini SPHardwareDataType SPDisplaysDataType"});
systemProfiler.waitForStarted();
systemProfiler.waitForFinished();
const QString systemData(systemProfiler.readAllStandardOutput());
#if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0))
QStringList systemLines = systemData.split('\n', Qt::SkipEmptyParts);
#else
QStringList systemLines = systemData.split('\n', QString::SkipEmptyParts);
#endif
for (int i = 0; i<systemLines.size(); i++)
{
// hardware overview
if(systemLines.at(i).contains("Model", Qt::CaseInsensitive))
writeLog(systemLines.at(i).trimmed());
if(systemLines.at(i).contains("Chip:", Qt::CaseInsensitive))
writeLog(systemLines.at(i).trimmed());
if(systemLines.at(i).contains("Processor", Qt::CaseInsensitive))
writeLog(systemLines.at(i).trimmed().replace("Unknown", "Apple M1"));
if(systemLines.at(i).contains("Memory", Qt::CaseInsensitive))
writeLog(systemLines.at(i).trimmed());
// graphics/display overview
if(systemLines.at(i).contains("Resolution", Qt::CaseInsensitive))
writeLog(systemLines.at(i).trimmed());
if(systemLines.at(i).contains("VRAM", Qt::CaseInsensitive))
writeLog(systemLines.at(i).trimmed());
}
#elif defined Q_OS_BSD4
QProcess dmesg;
dmesg.start("/sbin/dmesg", {}, QIODevice::ReadOnly);
dmesg.waitForStarted();
dmesg.waitForFinished();
const QString dmesgData(dmesg.readAll());
#if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0))
QStringList dmesgLines = dmesgData.split('\n', QString::SkipEmptyParts);
#else
QStringList dmesgLines = dmesgData.split('\n', Qt::SkipEmptyParts);
#endif
for (int i = 0; i<dmesgLines.size(); i++)
{
if (dmesgLines.at(i).contains("memory"))
{
writeLog(dmesgLines.at(i).trimmed());
}
if (dmesgLines.at(i).contains("CPU"))
{
writeLog(dmesgLines.at(i).trimmed());
}
if (dmesgLines.at(i).contains("VGA"))
{
writeLog(dmesgLines.at(i).trimmed());
}
}
#endif
}
void StelLogger::deinit()
{
qInstallMessageHandler(Q_NULLPTR);
logFile.close();
}
void StelLogger::debugLogHandler(QtMsgType type, const QMessageLogContext& ctx, const QString& msg)
{
// *** NOTE: see original Qt source in qlogging.cpp (qDefaultMessageHandler) for sensible default code
//use Qt to format the log message, if possible
//this uses the format set by qSetMessagePattern
//or QT_MESSAGE_PATTERN environment var
QString fmt = qFormatLogMessage(type,ctx,msg);
//do nothing for null messages
if(fmt.isNull())
return;
//always append newline
fmt.append(QLatin1Char('\n'));
#ifdef Q_OS_WIN
//Send debug messages to Debugger, if one is attached, instead of stderr
//This seems to avoid output delays in Qt Creator, allowing for easier debugging
//Seems to work fine with MSVC and MinGW
if (IsDebuggerPresent())
{
OutputDebugStringW(reinterpret_cast<const wchar_t *>(fmt.utf16()));
}
else
#endif
{
//this does the same as the default handler in qlogging.cpp
fprintf(stderr, "%s", qPrintable(fmt));
fflush(stderr);
}
writeLog(fmt);
}
void StelLogger::writeLog(QString msg)
{
if (!msg.endsWith('\n'))
msg.append(QLatin1Char('\n'));
fileMutex.lock();
const auto utf8 = msg.toUtf8();
logFile.write(utf8.constData(), utf8.size());
log += msg;
fileMutex.unlock();
}