diff options
| -rw-r--r-- | include/soyuz/library.hh | 13 | ||||
| -rw-r--r-- | soyuz/library.cc | 38 | ||||
| -rw-r--r-- | soyuz/soyuz.cc | 17 | ||||
| -rw-r--r-- | soyuz/tray.cc | 16 |
4 files changed, 71 insertions, 13 deletions
diff --git a/include/soyuz/library.hh b/include/soyuz/library.hh index ae7ce13..db88870 100644 --- a/include/soyuz/library.hh +++ b/include/soyuz/library.hh @@ -6,6 +6,7 @@ #pragma once +#include <string> #include <Windows.h> #define NT_SUCCESS(status) (status >= 0) @@ -13,9 +14,21 @@ namespace soyuz { +// enum log_level { +// low, // blue +// medium, // orange +// high, // red +// }; + static auto enum_windows_proc(HWND hwnd, LPARAM lparam) -> BOOL; auto find_lunar() -> DWORD; auto delete_handle(DWORD pid) -> int; +auto write_log_file(const std::string &message) -> void; +auto init_log_file() -> void; +auto close_log_file() -> void; +auto exit(int exit_code) -> void; +// https://stackoverflow.com/a/10467633/14452787 +auto current_date_time() -> std::string; } diff --git a/soyuz/library.cc b/soyuz/library.cc index bf2b451..a0bfbd7 100644 --- a/soyuz/library.cc +++ b/soyuz/library.cc @@ -2,6 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-only #include <cstdio> +#include <fstream> #include <memory> #include <sstream> #include <string> @@ -14,6 +15,8 @@ namespace soyuz { +std::ofstream log_file("soyuz.log"); + static auto enum_windows_proc(HWND hwnd, LPARAM lparam) -> BOOL { int length = GetWindowTextLength(hwnd); auto title = new CHAR[length + 1]; @@ -42,6 +45,7 @@ auto find_lunar() -> DWORD { DWORD pid; GetWindowThreadProcessId(window, &pid); + log("hooked lunar client"); log("you may now close this window"); return pid; } @@ -135,4 +139,38 @@ auto delete_handle(DWORD pid) -> int { return 0; } +auto write_log_file(const std::string &message) -> void { + log_file << message << std::endl; +} + +auto init_log_file() -> void { + if (!log_file.is_open()) { + soyuz::log("could not open 'soyuz.log'"); + soyuz::log("proceeding without logging to file"); + + return; + } + + soyuz::log("opened 'soyuz.log'"); +} + +auto close_log_file() -> void { + soyuz::log("closing 'soyuz.log'"); log_file.close(); +} + +auto exit(int exit_code) -> void { + if (log_file.is_open()) { close_log_file(); } + ::exit(exit_code); +} + +auto current_date_time() -> std::string { + time_t now = time(nullptr); + struct tm t_struct{}; + char buffer[80]; + localtime_s(&t_struct, &now); // t_struct = *localtime(&now); + strftime(buffer, sizeof(buffer), "%Y-%m-%d.%X", &t_struct); + + return buffer; +} + } diff --git a/soyuz/soyuz.cc b/soyuz/soyuz.cc index b030bd5..00457b8 100644 --- a/soyuz/soyuz.cc +++ b/soyuz/soyuz.cc @@ -19,6 +19,8 @@ extern char class_name[]; extern std::vector<std::string> logs; auto WinMain(HINSTANCE instance, HINSTANCE previous, LPSTR argument, int show) -> int { // WINAPI + soyuz::init_log_file(); + MSG messages; WNDCLASSEX wincl; WM_TASKBAR = RegisterWindowMessageA("TaskbarCreated"); @@ -29,22 +31,20 @@ auto WinMain(HINSTANCE instance, HINSTANCE previous, LPSTR argument, int show) - wincl.style = CS_DBLCLKS; wincl.cbSize = sizeof(WNDCLASSEX); - wincl.hIcon = LoadIcon (GetModuleHandle(nullptr), MAKEINTRESOURCE(ICO1)); - wincl.hIconSm = LoadIcon (GetModuleHandle(nullptr), MAKEINTRESOURCE(ICO1)); - wincl.hCursor = LoadCursor (nullptr, IDC_ARROW); + wincl.hIcon = LoadIcon(GetModuleHandle(nullptr), MAKEINTRESOURCE(ICO1)); + wincl.hIconSm = LoadIcon(GetModuleHandle(nullptr), MAKEINTRESOURCE(ICO1)); + wincl.hCursor = LoadCursor(nullptr, IDC_ARROW); wincl.lpszMenuName = nullptr; wincl.cbClsExtra = 0; wincl.cbWndExtra = 0; wincl.hbrBackground = (HBRUSH)(CreateSolidBrush(RGB(255, 255, 255))); - if (!RegisterClassEx (&wincl)) { - return 0; - } + if (!RegisterClassEx(&wincl)) { return 0; } window = CreateWindowEx( 0, class_name, class_name, - WS_OVERLAPPEDWINDOW, + WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME, CW_USEDEFAULT, CW_USEDEFAULT, 544, @@ -63,7 +63,7 @@ auto WinMain(HINSTANCE instance, HINSTANCE previous, LPSTR argument, int show) - if (pid == 0 || pid == 3435973836) { soyuz::log("could not locate lunar client"); - exit(1); + soyuz::exit(1); } std::stringstream ss; ss << "located lunar client: pid " << pid; @@ -85,5 +85,6 @@ auto WinMain(HINSTANCE instance, HINSTANCE previous, LPSTR argument, int show) - soyuz.request_stop(); soyuz.join(); soyuz::log("exiting"); + soyuz::close_log_file(); return (int)messages.wParam; } diff --git a/soyuz/tray.cc b/soyuz/tray.cc index 1796bf0..e57771c 100644 --- a/soyuz/tray.cc +++ b/soyuz/tray.cc @@ -1,9 +1,12 @@ // Copyright (C) 2021-2021 Fuwn // SPDX-License-Identifier: GPL-3.0-only -#include "soyuz/tray.hh" +#include <sstream> + +#include "soyuz/library.hh" #include "soyuz/resource.hh" #include "soyuz/soyuz.hh" +#include "soyuz/tray.hh" UINT WM_TASKBAR = 0; HWND window; @@ -11,9 +14,7 @@ HMENU menu; NOTIFYICONDATA data; TCHAR tip[64] = TEXT(WINDOW_TRAY_NAME); char class_name[] = WINDOW_TRAY_NAME; -std::vector<std::string> logs = { - "lunar client has been hooked, you may now close this window", -}; +std::vector<std::string> logs; auto WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) -> LRESULT { // CALLBACK if (message == WM_TASKBAR && !IsWindowVisible(window)) { @@ -128,7 +129,12 @@ void InitNotifyIconData() { namespace soyuz { auto log(const std::string &message) -> void { - LOG(message) + if (logs.size() == 16) { logs.erase(logs.begin()); } + + std::ostringstream ss; + ss << "[" << current_date_time() << "] " << message; + + LOG(ss.str()) write_log_file(ss.str()); RedrawWindow(window, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW); } |