Skip to content

Commit

Permalink
Add jngl::trace and jngl::warn functions for simple logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Jun 15, 2024
1 parent 71e582f commit 329413e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/jngl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions src/jngl/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 Jan Niklas Hasse <jhasse@gmail.com>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
#include "log.hpp"

#include "../App.hpp"
#include "message.hpp"

#include <sstream>

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
33 changes: 33 additions & 0 deletions src/jngl/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Jan Niklas Hasse <jhasse@gmail.com>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
/// Logging functions
/// @file
#pragma once

#if __has_include(<format>)
#include <format>
#endif

namespace jngl {

void trace(const std::string&);

#if __has_include(<format>)
template <class... Args> void trace(std::format_string<Args...> format, Args&&... args) {
return trace(std::format(std::move(format), std::forward<Args>(args)...));
}
#else
template <class... Args> void trace(Args&&... args) {}
#endif

void warn(const std::string&);

#if __has_include(<format>)
template <class... Args> void warn(std::format_string<Args...> format, Args&&... args) {
return warn(std::format(std::move(format), std::forward<Args>(args)...));
}
#else
template <class... Args> void warn(Args&&... args) {}
#endif

} // namespace jngl

0 comments on commit 329413e

Please sign in to comment.