Skip to content

Commit 2be4773

Browse files
committed
Add log function with custom level name support
1 parent bf98633 commit 2be4773

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/jngl/log.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ void info(const std::string& line) {
4747
internal::log(shortenDisplayName(), "\x1b[32minfo\x1b[0m", line);
4848
}
4949

50+
void log(std::string_view levelName, const std::string& line) {
51+
internal::log(shortenDisplayName(), std::format("\x1b[35m{}\x1b[0m", std::string(levelName)), line);
52+
}
53+
5054
void warn(const std::string& line) {
5155
internal::log(shortenDisplayName(), "\x1b[1;33mwarn\x1b[0m", line);
5256
}

src/jngl/log.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ template <class... Args> void info(std::format_string<Args...> format, Args&&...
4747
template <class... Args> void info(Args&&...) {}
4848
#endif
4949

50+
/// Same level as "info", but with custom level name. Useful for your own subsystems, for example
51+
/// your scripting engine.
52+
void log(std::string_view levelName, const std::string&);
53+
54+
#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000)
55+
template <class... Args> void log(std::string_view levelName, std::format_string<Args...> format, Args&&... args) {
56+
return log(levelName, std::format(std::move(format), std::forward<Args>(args)...));
57+
}
58+
#else
59+
template <class... Args> void log(Args&&...) {}
60+
#endif
61+
5062
void warn(const std::string&);
5163

5264
#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000)

0 commit comments

Comments
 (0)