From 76383ba652a208b504e00302c289322184d695a0 Mon Sep 17 00:00:00 2001 From: Lukas Cone Date: Tue, 24 Sep 2024 18:33:01 +0200 Subject: [PATCH] new print api --- cmake/targetex.cmake | 4 +++- include/spike/master_printer.hpp | 35 ++++++++++++++-------------- src/app/context.cpp | 40 ++++++++++++++++---------------- src/cli/CMakeLists.txt | 4 +++- src/master_printer.cpp | 24 +++++++------------ 5 files changed, 52 insertions(+), 55 deletions(-) diff --git a/cmake/targetex.cmake b/cmake/targetex.cmake index c663bd0..b9505b8 100644 --- a/cmake/targetex.cmake +++ b/cmake/targetex.cmake @@ -215,7 +215,9 @@ function(build_target) TARGETS ${_arg_NAME} LIBRARY DESTINATION $,modules,lib> RUNTIME DESTINATION bin/modules) - install(FILES $ CONFIGURATIONS "RelWithDebInfo" DESTINATION bin) + if(WIN32 OR MINGW) + install(FILES $ CONFIGURATIONS "RelWithDebInfo" DESTINATION bin) + endif() endif() if(${_is_python_module}) diff --git a/include/spike/master_printer.hpp b/include/spike/master_printer.hpp index cf4d181..6a12564 100644 --- a/include/spike/master_printer.hpp +++ b/include/spike/master_printer.hpp @@ -19,27 +19,32 @@ #include "util/settings.hpp" #include #include +#include #define printerror(...) \ { \ - es::print::Get(es::print::MPType::ERR) << __VA_ARGS__ << std::endl; \ - es::print::FlushAll(); \ + es::print::PrintFn(es::print::MPType::ERR, [&](std::ostream &str) { \ + str << __VA_ARGS__ << std::endl; \ + }); \ } #define printwarning(...) \ { \ - es::print::Get(es::print::MPType::WRN) << __VA_ARGS__ << std::endl; \ - es::print::FlushAll(); \ + es::print::PrintFn(es::print::MPType::WRN, [&](std::ostream &str) { \ + str << __VA_ARGS__ << std::endl; \ + }); \ } #define printline(...) \ { \ - es::print::Get(es::print::MPType::MSG) << __VA_ARGS__ << std::endl; \ - es::print::FlushAll(); \ + es::print::PrintFn(es::print::MPType::MSG, [&](std::ostream &str) { \ + str << __VA_ARGS__ << std::endl; \ + }); \ } #define printinfo(...) \ { \ - es::print::Get(es::print::MPType::INF) << __VA_ARGS__ << std::endl; \ - es::print::FlushAll(); \ + es::print::PrintFn(es::print::MPType::INF, [&](std::ostream &str) { \ + str << __VA_ARGS__ << std::endl; \ + }); \ } namespace es::print { @@ -52,22 +57,18 @@ struct Queuer { }; using queue_func = void (*)(const Queuer &); +using stream_func = std::function; -// Calling this will lock other threads that will try to access stream until -// FlushAll is called! -std::ostream PC_EXTERN &Get(MPType type = MPType::PREV); +void PC_EXTERN PrintFn(MPType type, stream_func fn); void PC_EXTERN AddPrinterFunction(print_func func, bool useColor = true); void PC_EXTERN AddQueuer(queue_func func); -// Unlocks other threads access to Get -void PC_EXTERN FlushAll(); void PC_EXTERN PrintThreadID(bool yn); template void Print(es::print::MPType type, C... args) { - auto &printStream = es::print::Get(type); // todo?, add detectors and to_string converters - ((printStream << std::forward(args)), ...) << '\n'; - - es::print::FlushAll(); + PrintFn(type, [&](std::ostream &str) { + ((str << std::forward(args)), ...) << '\n'; + }); } } // namespace es::print diff --git a/src/app/context.cpp b/src/app/context.cpp index 3d6d454..464149e 100644 --- a/src/app/context.cpp +++ b/src/app/context.cpp @@ -415,31 +415,31 @@ int APPContext::ApplySetting(std::string_view key, std::string_view value) { void APPContext::PrintCLIHelp() const { printline("Options:" << std::endl); + es::print::PrintFn(es::print::MPType::PREV, [&](std::ostream &str) { + auto printStuff = [&](auto rtti) { + for (size_t i = 0; i < rtti->nTypes; i++) { + if (rtti->typeAliases && rtti->typeAliases[i]) { + str << "-" << rtti->typeAliases[i] << ", "; + } - auto printStuff = [](auto rtti) { - for (size_t i = 0; i < rtti->nTypes; i++) { - if (rtti->typeAliases && rtti->typeAliases[i]) { - es::print::Get() << "-" << rtti->typeAliases[i] << ", "; + str << "--" << rtti->typeNames[i]; + str << " = " << rtti->typeDescs[i].part1 << std::endl; } + }; - es::print::Get() << "--" << rtti->typeNames[i]; - es::print::Get() << " = " << rtti->typeDescs[i].part1 << std::endl; - } - }; - - printStuff(::RTTI(MainSettings())); - printStuff(::RTTI(TexelSettings())); + printStuff(::RTTI(MainSettings())); + printStuff(::RTTI(TexelSettings())); - if (ProcessFile) { - printStuff(::RTTI(ExtractSettings())); - } else if (NewArchive) { - printStuff(::RTTI(CompressSettings())); - } + if (ProcessFile) { + printStuff(::RTTI(ExtractSettings())); + } else if (NewArchive) { + printStuff(::RTTI(CompressSettings())); + } - if (info->settings) { - printStuff(RTTI()); - } - printline(""); + if (info->settings) { + printStuff(RTTI()); + } + }); } void DumpTypeMD(std::ostream &out, const ReflectorFriend &info, diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index b08be63..d364b7b 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -33,7 +33,9 @@ if(NOT BUILD_SHARED_LIBS) endif() install(TARGETS spike_cli RUNTIME DESTINATION $,$>>,.,bin>) -install(FILES $ CONFIGURATIONS "RelWithDebInfo" DESTINATION bin) +if(WIN32 OR MINGW) + install(FILES $ CONFIGURATIONS "RelWithDebInfo" DESTINATION bin) +endif() function(add_spike_subdir name_) add_subdirectory(${name_} ${SpikeCLI_BINARY_DIR}/${name_}) diff --git a/src/master_printer.cpp b/src/master_printer.cpp index fd97fe8..105310a 100644 --- a/src/master_printer.cpp +++ b/src/master_printer.cpp @@ -34,9 +34,6 @@ static struct MasterPrinter { std::vector functions; std::vector queues; - std::stringstream buffer; - std::mutex mutex; - std::thread::id lockedThread; bool printThreadID = false; MPType cType = MPType::MSG; } MASTER_PRINTER; @@ -54,26 +51,23 @@ void AddPrinterFunction(print_func func, bool useColor) { void AddQueuer(queue_func func) { MASTER_PRINTER.queues.push_back(func); } -std::ostream &Get(MPType type) { - if (auto threadID = std::this_thread::get_id(); - !(MASTER_PRINTER.lockedThread == threadID)) { - MASTER_PRINTER.mutex.lock(); - MASTER_PRINTER.lockedThread = threadID; - } - +void PrintFn(MPType type, stream_func fn) { if (type != MPType::PREV) { MASTER_PRINTER.cType = type; } - return MASTER_PRINTER.buffer; -} -void FlushAll() { + std::stringstream buffer; + fn(buffer); + Queuer que; - que.payload = std::move(MASTER_PRINTER.buffer).str(); + que.payload = std::move(buffer).str(); std::thread::id threadID = std::this_thread::get_id(); que.threadId = reinterpret_cast(threadID); que.type = MASTER_PRINTER.cType; + static std::mutex mutex; + std::lock_guard lg(mutex); + for (auto &[func, useColor] : MASTER_PRINTER.functions) { if (useColor) { if (que.type == MPType::WRN) { @@ -111,8 +105,6 @@ void FlushAll() { } MASTER_PRINTER.cType = MPType::MSG; - MASTER_PRINTER.mutex.unlock(); - MASTER_PRINTER.lockedThread = {}; } void PrintThreadID(bool yn) { MASTER_PRINTER.printThreadID = yn; }