diff --git a/src/jngl.hpp b/src/jngl.hpp index e9e70642..aec4a8eb 100644 --- a/src/jngl.hpp +++ b/src/jngl.hpp @@ -33,6 +33,7 @@ #include "jngl/font.hpp" #include "jngl/input.hpp" #include "jngl/job.hpp" +#include "jngl/log.hpp" #include "jngl/main.hpp" #include "jngl/matrix.hpp" #include "jngl/message.hpp" diff --git a/src/jngl/log.cpp b/src/jngl/log.cpp new file mode 100644 index 00000000..36c9ff92 --- /dev/null +++ b/src/jngl/log.cpp @@ -0,0 +1,40 @@ +// Copyright 2024 Jan Niklas Hasse +// For conditions of distribution and use, see copyright notice in LICENSE.txt +#include "log.hpp" + +#include "../App.hpp" +#include "message.hpp" + +#include + +namespace jngl { + +void trace(const std::string& line) { + std::ostringstream tmp; + if (auto displayName = App::instance().getDisplayName(); !displayName.empty()) { + tmp << '['; + if (displayName.size() > 4) { + tmp << displayName.substr(0, 3) << "…]"; + } else { + tmp << displayName << ']'; + } + } + tmp << "[\x1b[1;36mtrace\x1b[0m] " << line << '\n'; + printMessage(tmp.str()); +} + +void warn(const std::string& line) { + std::ostringstream tmp; + if (auto displayName = App::instance().getDisplayName(); !displayName.empty()) { + tmp << '['; + if (displayName.size() > 4) { + tmp << displayName.substr(0, 3) << "…]"; + } else { + tmp << displayName << ']'; + } + } + tmp << "[\x1b[1;33mwarn\x1b[0m] " << line << '\n'; + printMessage(tmp.str()); +} + +} // namespace jngl diff --git a/src/jngl/log.hpp b/src/jngl/log.hpp new file mode 100644 index 00000000..c22736d1 --- /dev/null +++ b/src/jngl/log.hpp @@ -0,0 +1,33 @@ +// Copyright 2024 Jan Niklas Hasse +// For conditions of distribution and use, see copyright notice in LICENSE.txt +/// Logging functions +/// @file +#pragma once + +#if __has_include() +#include +#endif + +namespace jngl { + +void trace(const std::string&); + +#if __has_include() +template void trace(std::format_string format, Args&&... args) { + return trace(std::format(std::move(format), std::forward(args)...)); +} +#else +template void trace(Args&&... args) {} +#endif + +void warn(const std::string&); + +#if __has_include() +template void warn(std::format_string format, Args&&... args) { + return warn(std::format(std::move(format), std::forward(args)...)); +} +#else +template void warn(Args&&... args) {} +#endif + +} // namespace jngl