-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jngl::trace and jngl::warn functions for simple logging
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |