From bfe113937db95ac465cefc28e47b60b4d24e2539 Mon Sep 17 00:00:00 2001 From: lzwdgc Date: Fri, 5 Apr 2024 00:41:42 +0300 Subject: [PATCH] [mod] Also log to file. --- src/aim1_mod_maker/aim1_mod_maker.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/aim1_mod_maker/aim1_mod_maker.h b/src/aim1_mod_maker/aim1_mod_maker.h index 31c2a2f..d0121cf 100644 --- a/src/aim1_mod_maker/aim1_mod_maker.h +++ b/src/aim1_mod_maker/aim1_mod_maker.h @@ -52,11 +52,26 @@ auto operator""_bin(const char *ptr, uint64_t len) { return ret; } +auto &log_file(const path *p = {}) { + static std::ofstream ofile = [&](){ + if (!p) { + throw std::runtime_error{"pass log filename first!"}; + } + return std::ofstream{*p}; + }(); + return ofile; +} +void log_internal(const std::string &s) { + std::cout << s << "\n"; + log_file() << s << std::endl; +} void log(auto &&format, auto &&arg, auto &&...args) { - std::println("{}", std::vformat(format, std::make_format_args(arg, args...))); + auto s = std::format("{}", std::vformat(format, std::make_format_args(arg, args...))); + log_internal(s); } void log(auto &&str) { - std::println("{}", str); + auto s = std::format("{}", str); + log_internal(s); } struct aim_exe_v1_06_constants { @@ -585,6 +600,9 @@ private: } #endif fs::create_directories(get_mod_dir()); + // can write to mod dir + auto logfn = get_mod_dir() / "log.txt"; + log_file(&logfn); auto src_fn = get_mod_dir() / get_full_mod_name() += ".cpp"; fs::copy_file(loc.file_name(), src_fn, fs::copy_options::overwrite_existing); code_files_to_distribute.insert(src_fn);