-
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.
- Loading branch information
Showing
12 changed files
with
234 additions
and
148 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
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
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
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,85 @@ | ||
// Copyright 2024 Jan Niklas Hasse <jhasse@gmail.com> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
#include "log.hpp" | ||
|
||
#include "jngl/message.hpp" | ||
|
||
#include <cctype> | ||
#include <sstream> | ||
|
||
namespace jngl::internal { | ||
|
||
void trace(const std::string& line) { | ||
#ifdef JNGL_TRACE | ||
log("JNGL", "\x1b[36mtrace\x1b[0m", line); | ||
#endif | ||
} | ||
|
||
void debug(const std::string& line) { | ||
#ifndef NDEBUG | ||
log("JNGL", "\x1b[34mdebug\x1b[0m", line); | ||
#endif | ||
} | ||
|
||
void info(const std::string& line) { | ||
log("JNGL", "\x1b[32minfo\x1b[0m", line); | ||
} | ||
|
||
void warn(const std::string& line) { | ||
log("JNGL", "\x1b[33mwarn\x1b[0m", line); | ||
} | ||
|
||
void error(const std::string& line) { | ||
log("JNGL", "\x1b[31merror\x1b[0m", line); | ||
} | ||
|
||
namespace { | ||
std::string stripAnsiEscapeCodes(const std::string& in) { | ||
std::string stripped; | ||
for (size_t i = 0; i < in.size(); ++i) { | ||
if (in[i] != '\33') { | ||
stripped.push_back(in[i]); | ||
continue; | ||
} | ||
|
||
// Only strip CSIs for now. | ||
if (i + 1 >= in.size()) { | ||
break; | ||
} | ||
if (in[i + 1] != '[') { | ||
continue; | ||
} // Not a CSI. | ||
i += 2; | ||
|
||
// Skip everything up to and including the next [a-zA-Z]. | ||
while (i < in.size() && !std::isalpha(in[i])) { | ||
++i; | ||
} | ||
} | ||
return stripped; | ||
} | ||
} // namespace | ||
|
||
void log(const std::string& appName, const std::string& level, const std::string& message) { | ||
std::ostringstream tmp; | ||
if (!appName.empty()) { | ||
tmp << '[' << appName << ']'; | ||
} | ||
tmp << '[' << level << "] "; | ||
bool first = true; | ||
std::stringstream lines(message); | ||
std::string line; | ||
while (std::getline(lines, line)) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
const size_t indentation = | ||
stripAnsiEscapeCodes(appName).size() + 5 + stripAnsiEscapeCodes(level).size(); | ||
tmp << std::string(indentation, ' '); | ||
} | ||
tmp << line << '\n'; | ||
} | ||
printMessage(tmp.str()); | ||
} | ||
|
||
} // namespace jngl::internal |
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,69 @@ | ||
// Copyright 2024 Jan Niklas Hasse <jhasse@gmail.com> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
#pragma once | ||
|
||
#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000) | ||
#include <format> | ||
#endif | ||
#include <string> | ||
|
||
namespace jngl::internal { | ||
|
||
void trace(const std::string&); | ||
|
||
#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000) | ||
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&&...) { | ||
} | ||
#endif | ||
|
||
void debug(const std::string&); | ||
|
||
#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000) | ||
template <class... Args> void debug(std::format_string<Args...> format, Args&&... args) { | ||
return debug(std::format(std::move(format), std::forward<Args>(args)...)); | ||
} | ||
#else | ||
template <class... Args> void debug(Args&&...) { | ||
} | ||
#endif | ||
|
||
void info(const std::string&); | ||
|
||
#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000) | ||
template <class... Args> void info(std::format_string<Args...> format, Args&&... args) { | ||
return info(std::format(std::move(format), std::forward<Args>(args)...)); | ||
} | ||
#else | ||
template <class... Args> void info(Args&&...) { | ||
} | ||
#endif | ||
|
||
void warn(const std::string&); | ||
|
||
#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000) | ||
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&&...) { | ||
} | ||
#endif | ||
|
||
void error(const std::string&); | ||
|
||
#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000) | ||
template <class... Args> void error(std::format_string<Args...> format, Args&&... args) { | ||
return error(std::format(std::move(format), std::forward<Args>(args)...)); | ||
} | ||
#else | ||
template <class... Args> void error(Args&&...) { | ||
} | ||
#endif | ||
|
||
void log(const std::string& appName, const std::string& level, const std::string& message); | ||
|
||
} // namespace jngl::internal |
Oops, something went wrong.