diff options
| author | Fuwn <[email protected]> | 2022-02-11 03:58:10 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-02-11 03:58:10 -0800 |
| commit | 87bafca66a145d547bbe927e6dddf8192e7a5d99 (patch) | |
| tree | 55083235e6ef4f222d4e48961506a44461124584 | |
| parent | fix(#1): detach instead of join, we don't care about NtQueryObject (diff) | |
| download | soyuz-87bafca66a145d547bbe927e6dddf8192e7a5d99.tar.xz soyuz-87bafca66a145d547bbe927e6dddf8192e7a5d99.zip | |
feat(library): implement colourful logging
| -rw-r--r-- | include/soyuz/library.hh | 24 | ||||
| -rw-r--r-- | include/soyuz/tray.hh | 3 | ||||
| -rw-r--r-- | soyuz/library.cc | 11 | ||||
| -rw-r--r-- | soyuz/tray.cc | 20 |
4 files changed, 46 insertions, 12 deletions
diff --git a/include/soyuz/library.hh b/include/soyuz/library.hh index 8680cb3..f96bbf2 100644 --- a/include/soyuz/library.hh +++ b/include/soyuz/library.hh @@ -13,18 +13,13 @@ #include <string> #include <Windows.h> +#include <vector> #define NT_SUCCESS(status) (status >= 0) #define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) namespace soyuz { -// enum log_level { -// low, // blue -// medium, // orange -// high, // red -// }; - const std::string numbers_as_string[] = { "zero", "one", @@ -33,6 +28,23 @@ const std::string numbers_as_string[] = { "four", }; +enum log_level { + trace, + debug, + info, + warn, + error, +}; + +struct log_t { + log_level level; + std::string value; + + log_t(log_level level, std::string value) : level(level), value(std::move(value)) {} + + auto to_coloref() -> COLORREF; +}; + static BOOL CALLBACK enum_windows_proc(HWND, LPARAM); auto find_lunar() -> DWORD; auto delete_handle(DWORD) -> int; diff --git a/include/soyuz/tray.hh b/include/soyuz/tray.hh index 54c998e..a6bdb89 100644 --- a/include/soyuz/tray.hh +++ b/include/soyuz/tray.hh @@ -15,7 +15,7 @@ #include <vector> #include <Windows.h> -#define LOG(message) logs.emplace_back(message); +#include <soyuz/library.hh> LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM); auto minimize() -> void; @@ -25,6 +25,7 @@ auto InitNotifyIconData() -> void; namespace soyuz { auto log(const std::string &) -> void; +auto log(log_level, const std::string &) -> void; } diff --git a/soyuz/library.cc b/soyuz/library.cc index dcb2e36..a3f1874 100644 --- a/soyuz/library.cc +++ b/soyuz/library.cc @@ -178,4 +178,15 @@ auto current_date_time() -> std::string { return buffer; } +auto log_t::to_coloref() -> COLORREF { + switch (this->level) { + case trace: { return 0x00FF0000; } // blue + case debug: { return 0x0000FF00; } // green + case info: { return 0x00000000; } // black + case warn: { return 0x000080FF; } // orange + case error: { return 0x000000FF; } // red + default: { return 0x00000000; } // black + } +} + } diff --git a/soyuz/tray.cc b/soyuz/tray.cc index c405f30..85ea3f8 100644 --- a/soyuz/tray.cc +++ b/soyuz/tray.cc @@ -22,7 +22,7 @@ HMENU menu; NOTIFYICONDATA data; TCHAR tip[64] = TEXT(WINDOW_TRAY_NAME); char class_name[] = WINDOW_TRAY_NAME; -std::vector<std::string> logs; +std::vector<soyuz::log_t> logs; LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { if (message == WM_TASKBAR && !IsWindowVisible(window)) { @@ -42,9 +42,10 @@ LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM GetClientRect(hwnd, &rect); int height = 5; - for (const auto &i : logs) { - TextOut(hdc, 5, height, i.c_str(), (int)strlen(i.c_str())); - height += 20; + for (soyuz::log_t &i : logs) { + SetTextColor(hdc, i.to_coloref()); + TextOut(hdc, 5, height, i.value.c_str(), (int)strlen(i.value.c_str())); + height += 20; } EndPaint(window, &ps); @@ -141,7 +142,16 @@ auto log(const std::string &message) -> void { std::string to_log = fmt::format("[{}] {}", current_date_time(), message); - LOG(to_log.c_str()) write_log_file(to_log); + logs.emplace_back(log_t(log_level::info, to_log)); write_log_file(to_log); + RedrawWindow(window, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW); +} + +auto log(log_level level, const std::string &message) -> void { + if (logs.size() == 16) { logs.erase(logs.begin()); } + + std::string to_log = fmt::format("[{}] {}", current_date_time(), message); + + logs.emplace_back(log_t(level, to_log)); write_log_file(to_log); RedrawWindow(window, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW); } |