Skip to content

Commit 7ad04d9

Browse files
feat: spdlog also log to file
1 parent bec5360 commit 7ad04d9

4 files changed

Lines changed: 50 additions & 16 deletions

File tree

src/inject/inject.cc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <vector>
66

77
#include <spdlog/spdlog.h>
8+
#include <spdlog/sinks/basic_file_sink.h>
9+
#include <spdlog/sinks/stdout_color_sinks.h>
10+
#include <spdlog/sinks/msvc_sink.h>
811

912
#include "breeze_ui/animator.h"
1013
#include "breeze_ui/ui.h"
@@ -23,11 +26,28 @@ static unsigned char g_icon_png[] = {
2326
#include <shellapi.h>
2427
#include <taskschd.h>
2528

26-
2729
#include "Shlobj.h"
2830

2931
namespace fs = std::filesystem;
3032

33+
void init_inject_logger() {
34+
try {
35+
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
36+
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(
37+
(data_directory() / "inject.log").string(), true);
38+
auto msvc_sink = std::make_shared<spdlog::sinks::msvc_sink_mt>();
39+
40+
std::vector<spdlog::sink_ptr> sinks{console_sink, file_sink, msvc_sink};
41+
auto logger = std::make_shared<spdlog::logger>("inject", sinks.begin(), sinks.end());
42+
spdlog::set_default_logger(logger);
43+
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
44+
spdlog::set_level(spdlog::level::debug);
45+
spdlog::flush_on(spdlog::level::info);
46+
} catch (const spdlog::spdlog_ex &ex) {
47+
printf("Log initialization failed: %s\n", ex.what());
48+
}
49+
}
50+
3151
std::wstring GetModuleDirectory() {
3252
wchar_t path[MAX_PATH];
3353
GetModuleFileNameW(NULL, path, MAX_PATH);
@@ -1197,6 +1217,8 @@ void UpdateDllPath() {
11971217

11981218
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
11991219
LPSTR lpCmdLine, int nShowCmd) {
1220+
init_inject_logger();
1221+
12001222
int argc = 0;
12011223
auto argv = CommandLineToArgvW(GetCommandLineW(), &argc);
12021224

src/shell/entry.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
#include "qjs_sanitizer.h"
5252

53+
#include "logger.h"
54+
5355
namespace mb_shell {
5456
window_proc_hook entry::main_window_loop_hook{};
5557
void main() {
@@ -60,6 +62,7 @@ void main() {
6062
freopen("CONIN$", "r", stdin);
6163
ShowWindow(GetConsoleWindow(), SW_HIDE);
6264

65+
init_logger();
6366
install_error_handlers();
6467
config::run_config_loader();
6568

src/shell/logger.cc

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@
22
#include "config.h"
33
#include "utils.h"
44

5-
#include <fstream>
6-
#include <iostream>
7-
#include <string>
8-
9-
#include <windows.h>
5+
#include <spdlog/spdlog.h>
6+
#include <spdlog/sinks/basic_file_sink.h>
7+
#include <spdlog/sinks/stdout_color_sinks.h>
8+
#include <spdlog/sinks/msvc_sink.h>
109

1110
namespace mb_shell {
12-
void append_debug_string(const std::string &str) {
13-
static std::mutex mutex;
14-
static std::ofstream file(config::data_directory() / "debug.log",
15-
std::ios::app);
11+
void init_logger() {
12+
try {
13+
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
14+
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(
15+
(config::data_directory() / "debug.log").string(), true);
16+
auto msvc_sink = std::make_shared<spdlog::sinks::msvc_sink_mt>();
1617

17-
std::lock_guard<std::mutex> lock(mutex);
18+
std::vector<spdlog::sink_ptr> sinks{console_sink, file_sink, msvc_sink};
1819

19-
file << str;
20-
file.flush();
20+
auto logger = std::make_shared<spdlog::logger>("multi_sink", sinks.begin(), sinks.end());
21+
spdlog::set_default_logger(logger);
22+
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
23+
spdlog::set_level(spdlog::level::debug);
24+
spdlog::flush_on(spdlog::level::info);
2125

22-
printf("%s", str.c_str());
23-
OutputDebugStringA(str.c_str());
26+
} catch (const spdlog::spdlog_ex &ex) {
27+
printf("Log initialization failed: %s\n", ex.what());
28+
}
2429
}
2530
} // namespace mb_shell

src/shell/logger.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#pragma once
22
#include <spdlog/spdlog.h>
3-
#include <fmt/format.h>
3+
#include <fmt/format.h>
4+
5+
namespace mb_shell {
6+
void init_logger();
7+
}

0 commit comments

Comments
 (0)