Files
FocusIME/Redcraft.FocusIME/Source/Private/Main.cpp

108 lines
2.2 KiB
C++
Raw Normal View History

2025-06-21 20:23:05 +08:00
#include "Defines.h"
#include <windows.h>
2025-06-21 19:08:54 +08:00
#include <iostream>
2025-06-21 20:23:05 +08:00
#include <fstream>
2025-06-21 19:08:54 +08:00
#include <thread>
#include <chrono>
2025-06-21 20:23:05 +08:00
#include <string>
#include <filesystem>
#include <ctime>
#include <iomanip>
#include <mutex>
2025-06-21 19:08:54 +08:00
#pragma warning(disable: 4996)
// ReSharper disable CppDeprecatedEntity
// ReSharper disable CppClangTidyCertErr33C
2025-06-21 20:23:05 +08:00
std::ofstream GLogFile;
void PrintLog(const std::string& Text)
{
const auto Now = std::chrono::system_clock::now();
const auto Time = std::chrono::system_clock::to_time_t(Now);
const long long Milliseconds = (std::chrono::duration_cast<std::chrono::milliseconds>(Now.time_since_epoch()) % 1000).count();
static std::mutex Mutex; std::lock_guard Lock(Mutex);
const std::tm* LocalTime = std::localtime(&Time); // NOLINT(concurrency-mt-unsafe)
char Buffer[] = "[2025-06-21 20:05:04.305]: ";
std::strftime(&Buffer[1], 20, "%Y-%m-%d %H:%M:%S", LocalTime);
Buffer[20] = '.';
constexpr char Digits[] = "0123456789";
Buffer[21] = Digits[Milliseconds / 100 % 10];
Buffer[22] = Digits[Milliseconds / 10 % 10];
Buffer[23] = Digits[Milliseconds / 1 % 10];
GLogFile << Buffer << Text << std::endl;
}
2025-06-21 19:08:54 +08:00
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
using namespace std::chrono_literals;
2025-06-21 20:23:05 +08:00
const HANDLE Mutex = CreateMutex(nullptr, TRUE, "FocusIME_SingleInstance_Mutex");
if (Mutex == nullptr)
{
MessageBox(nullptr, "Failed to create mutex.", "FocusIME", MB_OK | MB_ICONERROR);
return 1;
}
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
MessageBox(nullptr, "FocusIME is already running.", "FocusIME", MB_OK | MB_ICONINFORMATION);
CloseHandle(Mutex);
return 0;
}
GLogFile.open("Log.txt", std::ios::out | std::ios::app);
if (!GLogFile.is_open())
{
MessageBox(nullptr, "Failed to open log file.", "FocusIME", MB_OK | MB_ICONERROR);
CloseHandle(Mutex);
return 1;
}
PrintLog("FocusIME started successfully");
MSG Message = { };
while (true)
{
BOOL bResult = GetMessage(&Message, nullptr, 0, 0);
2025-06-21 19:08:54 +08:00
2025-06-21 20:23:05 +08:00
if (bResult > 0)
{
TranslateMessage(&Message);
DispatchMessage (&Message);
}
2025-06-21 19:08:54 +08:00
2025-06-21 20:23:05 +08:00
else break;
}
2025-06-21 19:08:54 +08:00
2025-06-21 20:23:05 +08:00
PrintLog("FocusIME is shutting down...");
2025-06-21 19:08:54 +08:00
2025-06-21 20:23:05 +08:00
GLogFile.close();
2025-06-21 19:08:54 +08:00
2025-06-21 20:23:05 +08:00
CloseHandle(Mutex);
2025-06-21 19:08:54 +08:00
return 0;
}
// ReSharper restore CppDeprecatedEntity
// ReSharper restore CppClangTidyCertErr33C