Skip to content

Commit

Permalink
new print api
Browse files Browse the repository at this point in the history
  • Loading branch information
PredatorCZ committed Sep 24, 2024
1 parent 6e4c702 commit 76383ba
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 55 deletions.
4 changes: 3 additions & 1 deletion cmake/targetex.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ function(build_target)
TARGETS ${_arg_NAME}
LIBRARY DESTINATION $<IF:$<BOOL:${UNIX}>,modules,lib>
RUNTIME DESTINATION bin/modules)
install(FILES $<TARGET_PDB_FILE:${_arg_NAME}> CONFIGURATIONS "RelWithDebInfo" DESTINATION bin)
if(WIN32 OR MINGW)
install(FILES $<TARGET_PDB_FILE:${_arg_NAME}> CONFIGURATIONS "RelWithDebInfo" DESTINATION bin)
endif()
endif()

if(${_is_python_module})
Expand Down
35 changes: 18 additions & 17 deletions include/spike/master_printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,32 @@
#include "util/settings.hpp"
#include <ostream>
#include <string>
#include <functional>

#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 {
Expand All @@ -52,22 +57,18 @@ struct Queuer {
};

using queue_func = void (*)(const Queuer &);
using stream_func = std::function<void(std::ostream &)>;

// 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 <class... C> void Print(es::print::MPType type, C... args) {
auto &printStream = es::print::Get(type);
// todo?, add detectors and to_string converters
((printStream << std::forward<C>(args)), ...) << '\n';

es::print::FlushAll();
PrintFn(type, [&](std::ostream &str) {
((str << std::forward<C>(args)), ...) << '\n';
});
}

} // namespace es::print
Expand Down
40 changes: 20 additions & 20 deletions src/app/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ if(NOT BUILD_SHARED_LIBS)
endif()

install(TARGETS spike_cli RUNTIME DESTINATION $<IF:$<AND:$<BOOL:${UNIX}>,$<NOT:$<BOOL:${SPIKE_NODOT}>>>,.,bin>)
install(FILES $<TARGET_PDB_FILE:spike_cli> CONFIGURATIONS "RelWithDebInfo" DESTINATION bin)
if(WIN32 OR MINGW)
install(FILES $<TARGET_PDB_FILE:spike_cli> CONFIGURATIONS "RelWithDebInfo" DESTINATION bin)
endif()

function(add_spike_subdir name_)
add_subdirectory(${name_} ${SpikeCLI_BINARY_DIR}/${name_})
Expand Down
24 changes: 8 additions & 16 deletions src/master_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ static struct MasterPrinter {

std::vector<FuncType> functions;
std::vector<queue_func> queues;
std::stringstream buffer;
std::mutex mutex;
std::thread::id lockedThread;
bool printThreadID = false;
MPType cType = MPType::MSG;
} MASTER_PRINTER;
Expand All @@ -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<uint32 &>(threadID);
que.type = MASTER_PRINTER.cType;

static std::mutex mutex;
std::lock_guard<std::mutex> lg(mutex);

for (auto &[func, useColor] : MASTER_PRINTER.functions) {
if (useColor) {
if (que.type == MPType::WRN) {
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit 76383ba

Please sign in to comment.