添加项目文件。
This commit is contained in:
@@ -0,0 +1,237 @@
|
||||
#include <QApplication>
|
||||
#include <QIcon>
|
||||
#include <QDebug>
|
||||
#include <QByteArray>
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# include <windows.h>
|
||||
# include <io.h>
|
||||
# include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
namespace {
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
HANDLE g_stdoutTarget = nullptr;
|
||||
|
||||
UINT codePageFor(HANDLE target)
|
||||
{
|
||||
if (target != nullptr && target != INVALID_HANDLE_VALUE
|
||||
&& GetFileType(target) == FILE_TYPE_CHAR) {
|
||||
const UINT codePage = GetConsoleOutputCP();
|
||||
if (codePage != 0) {
|
||||
return codePage;
|
||||
}
|
||||
}
|
||||
return GetACP();
|
||||
}
|
||||
|
||||
QByteArray toCodePage(const QString &text, UINT codePage)
|
||||
{
|
||||
const int length = WideCharToMultiByte(
|
||||
codePage, 0, reinterpret_cast<LPCWSTR>(text.utf16()), text.size(),
|
||||
nullptr, 0, nullptr, nullptr);
|
||||
if (length <= 0) {
|
||||
return text.toLocal8Bit();
|
||||
}
|
||||
QByteArray encoded(length, Qt::Uninitialized);
|
||||
WideCharToMultiByte(
|
||||
codePage, 0, reinterpret_cast<LPCWSTR>(text.utf16()), text.size(),
|
||||
encoded.data(), length, nullptr, nullptr);
|
||||
return encoded;
|
||||
}
|
||||
|
||||
void cavityMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
|
||||
{
|
||||
const HANDLE stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
|
||||
const QByteArray text = toCodePage(
|
||||
qFormatLogMessage(type, context, message), codePageFor(stderrHandle));
|
||||
|
||||
std::fwrite(text.constData(), 1, static_cast<size_t>(text.size()), stderr);
|
||||
std::fputc('\n', stderr);
|
||||
std::fflush(stderr);
|
||||
|
||||
OutputDebugStringW(reinterpret_cast<const wchar_t *>(message.utf16()));
|
||||
OutputDebugStringW(L"\n");
|
||||
}
|
||||
|
||||
void writeVendorLine(const std::string &utf8Line)
|
||||
{
|
||||
if (g_stdoutTarget == nullptr) {
|
||||
return;
|
||||
}
|
||||
const int wideLength = MultiByteToWideChar(
|
||||
CP_UTF8, 0, utf8Line.data(), static_cast<int>(utf8Line.size()), nullptr, 0);
|
||||
if (wideLength <= 0) {
|
||||
return;
|
||||
}
|
||||
std::wstring wide(static_cast<size_t>(wideLength), L'\0');
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8Line.data(), static_cast<int>(utf8Line.size()),
|
||||
&wide[0], wideLength);
|
||||
|
||||
const UINT codePage = codePageFor(g_stdoutTarget);
|
||||
const int length = WideCharToMultiByte(
|
||||
codePage, 0, wide.data(), wideLength, nullptr, 0, nullptr, nullptr);
|
||||
if (length <= 0) {
|
||||
return;
|
||||
}
|
||||
std::string local(static_cast<size_t>(length), '\0');
|
||||
WideCharToMultiByte(codePage, 0, wide.data(), wideLength, &local[0], length, nullptr, nullptr);
|
||||
local.push_back('\n');
|
||||
|
||||
DWORD written = 0;
|
||||
WriteFile(g_stdoutTarget, local.data(), static_cast<DWORD>(local.size()), &written, nullptr);
|
||||
}
|
||||
|
||||
void startStdoutBridge()
|
||||
{
|
||||
// 注意:_dup2() 会关闭 fd 1 原来指向的句柄,所以这里先复制一份用于输出
|
||||
const HANDLE originalStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if (originalStdout == nullptr || originalStdout == INVALID_HANDLE_VALUE
|
||||
|| !DuplicateHandle(GetCurrentProcess(), originalStdout, GetCurrentProcess(),
|
||||
&g_stdoutTarget, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
HANDLE readEnd = nullptr;
|
||||
HANDLE writeEnd = nullptr;
|
||||
if (!CreatePipe(&readEnd, &writeEnd, nullptr, 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const int pipeFd = _open_osfhandle(reinterpret_cast<intptr_t>(writeEnd), _O_WRONLY | _O_TEXT);
|
||||
const int stdoutFd = _fileno(stdout);
|
||||
const int dupResult = (pipeFd == -1) ? -1 : _dup2(pipeFd, stdoutFd);
|
||||
|
||||
if (pipeFd == -1 || dupResult == -1) {
|
||||
CloseHandle(readEnd);
|
||||
if (pipeFd != -1) {
|
||||
_close(pipeFd);
|
||||
}
|
||||
return;
|
||||
}
|
||||
_close(pipeFd);
|
||||
std::setvbuf(stdout, nullptr, _IONBF, 0);
|
||||
|
||||
std::thread([readEnd]() {
|
||||
std::string pending;
|
||||
char buffer[4096];
|
||||
DWORD read = 0;
|
||||
while (ReadFile(readEnd, buffer, sizeof(buffer), &read, nullptr) && read > 0) {
|
||||
pending.append(buffer, read);
|
||||
size_t position = 0;
|
||||
while ((position = pending.find('\n')) != std::string::npos) {
|
||||
std::string line = pending.substr(0, position);
|
||||
pending.erase(0, position + 1);
|
||||
if (!line.empty() && line.back() == '\r') {
|
||||
line.pop_back();
|
||||
}
|
||||
writeVendorLine(line);
|
||||
}
|
||||
}
|
||||
CloseHandle(readEnd);
|
||||
}).detach();
|
||||
}
|
||||
|
||||
void bindConsoleStreams()
|
||||
{
|
||||
HANDLE consoleOut = CreateFileW(
|
||||
L"CONOUT$", GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
|
||||
if (consoleOut != INVALID_HANDLE_VALUE) {
|
||||
SetStdHandle(STD_OUTPUT_HANDLE, consoleOut);
|
||||
}
|
||||
HANDLE consoleErr = CreateFileW(
|
||||
L"CONOUT$", GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
|
||||
if (consoleErr != INVALID_HANDLE_VALUE) {
|
||||
SetStdHandle(STD_ERROR_HANDLE, consoleErr);
|
||||
}
|
||||
HANDLE consoleIn = CreateFileW(
|
||||
L"CONIN$", GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
|
||||
if (consoleIn != INVALID_HANDLE_VALUE) {
|
||||
SetStdHandle(STD_INPUT_HANDLE, consoleIn);
|
||||
}
|
||||
|
||||
FILE *stream = nullptr;
|
||||
freopen_s(&stream, "CONOUT$", "w", stdout);
|
||||
freopen_s(&stream, "CONOUT$", "w", stderr);
|
||||
freopen_s(&stream, "CONIN$", "r", stdin);
|
||||
}
|
||||
|
||||
void createLogConsole()
|
||||
{
|
||||
if (AllocConsole()) {
|
||||
bindConsoleStreams();
|
||||
SetConsoleTitleW(L"CavityLIBS 运行日志 - 实时指令/应答");
|
||||
}
|
||||
}
|
||||
|
||||
// 当前控制台是否被多个进程共用(例如从 cmd / .bat 启动时)
|
||||
bool consoleIsShared()
|
||||
{
|
||||
DWORD processes[16];
|
||||
const DWORD count = GetConsoleProcessList(processes, 16);
|
||||
return count > 1;
|
||||
}
|
||||
|
||||
void cavityPrepareConsole()
|
||||
{
|
||||
// 有可用的输出句柄(Qt Creator 的管道、命令行重定向)就沿用;
|
||||
// 完全没有输出通道(双击运行)就单独创建一个日志控制台。
|
||||
const HANDLE existingOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
const HANDLE existingErr = GetStdHandle(STD_ERROR_HANDLE);
|
||||
const bool hasStdStreams =
|
||||
existingOut != nullptr && existingOut != INVALID_HANDLE_VALUE
|
||||
&& existingErr != nullptr && existingErr != INVALID_HANDLE_VALUE;
|
||||
|
||||
if (!hasStdStreams) {
|
||||
createLogConsole();
|
||||
return;
|
||||
}
|
||||
|
||||
// 从 cmd / .bat 启动时继承的是“共享控制台”,批处理窗口一关日志就没了;
|
||||
// 这里换成独立控制台窗口,保证打包后双击脚本也能一直看到日志。
|
||||
const bool outIsConsole = GetFileType(existingOut) == FILE_TYPE_CHAR;
|
||||
const bool errIsConsole = GetFileType(existingErr) == FILE_TYPE_CHAR;
|
||||
if (outIsConsole && errIsConsole && consoleIsShared()) {
|
||||
FreeConsole();
|
||||
createLogConsole();
|
||||
return;
|
||||
}
|
||||
|
||||
if (GetConsoleWindow() != nullptr) {
|
||||
SetConsoleTitleW(L"CavityLIBS 运行日志 - 实时指令/应答");
|
||||
}
|
||||
}
|
||||
#endif // Q_OS_WIN
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
cavityPrepareConsole();
|
||||
startStdoutBridge();
|
||||
std::fflush(stdout);
|
||||
#endif
|
||||
|
||||
qInstallMessageHandler(cavityMessageHandler);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
app.setWindowIcon(QIcon("app_icon.ico"));
|
||||
qDebug() << "开始运行";
|
||||
qRegisterMetaType<QVector<float>>("QVector<float>");
|
||||
MainWindow mainWindow;
|
||||
mainWindow.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user