Skip to content

Commit

Permalink
Add internal logging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Oct 3, 2024
1 parent 86878a2 commit 964ca2a
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 148 deletions.
10 changes: 4 additions & 6 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

#include "jngl/AppParameters.hpp"
#include "jngl/ShaderProgram.hpp"
#include "jngl/debug.hpp"
#include "jngl/screen.hpp"
#include "jngl/window.hpp"
#include "jngl/work.hpp"
#include "log.hpp"
#include "windowptr.hpp"

#include <cmath>
Expand Down Expand Up @@ -69,8 +69,8 @@ void App::callAtExitFunctions() {
f();
}
if (!callAtExit.empty()) {
debugLn("WARNING: The destructor of a Singleton caused the creation of another Singleton. "
"Use handleIfAlive inside of destructors of Singletons.");
internal::warn("The destructor of a Singleton caused the creation of another Singleton. "
"Use handleIfAlive inside of destructors of Singletons.");
}
callAtExit.clear();
}
Expand All @@ -90,9 +90,7 @@ void App::mainLoop() {
if (impl->steamAppId) {
initSteamAchievements();
}
debug("Starting main loop for '");
debug(impl->displayName);
debugLn('\'');
internal::debug("Starting main loop for '{}'.", impl->displayName);
pWindow->mainLoop();
}

Expand Down
9 changes: 4 additions & 5 deletions src/freetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "helper.hpp"
#include "jngl/ScaleablePixels.hpp"
#include "jngl/debug.hpp"
#include "jngl/matrix.hpp"
#include "jngl/screen.hpp"
#include "log.hpp"
#include "main.hpp"

#ifdef ANDROID
Expand All @@ -30,11 +30,11 @@ Character::Character(const char32_t ch, const unsigned int fontHeight, FT_Face f
if (FT_Load_Char(face, ch, flags)) {
const std::string msg =
std::string("FT_Load_Glyph failed. Character: ") + std::to_string(ch);
debugLn(msg);
// Load a question mark instead
if (FT_Load_Glyph(face, FT_Get_Char_Index(face, '?'), flags)) {
throw std::runtime_error(msg);
}
internal::error(msg);
}
FT_Glyph glyph;
if (FT_Get_Glyph(face->glyph, &glyph)) {
Expand Down Expand Up @@ -166,9 +166,9 @@ FontImpl::FontImpl(const std::string& relativeFilename, unsigned int height, flo
auto& fileCache = fileCaches[filename];
bytes = fileCache.lock();
if (bytes) {
debug("Reusing font buffer for "); debug(filename); debug("... ");
internal::debug("Reusing font buffer for {}... ", filename);
} else {
debug("Loading font "); debug(filename); debug("... ");
internal::debug("Loading font {}...", filename);

FILE* const f = fopen(filename.c_str(), "rb");
assert(f);
Expand All @@ -188,7 +188,6 @@ FontImpl::FontImpl(const std::string& relativeFilename, unsigned int height, flo
0) {
throw std::runtime_error("FT_New_Memory_Face failed");
}
debug("OK\n");
// Finally will call FT_Done_Face when the Font class is destroyed:
freeFace = std::make_unique<Finally>([this]() { return FT_Done_Face(face); });

Expand Down
6 changes: 3 additions & 3 deletions src/jngl/Shader.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2018-2023 Jan Niklas Hasse <jhasse@bixense.com>
// Copyright 2018-2024 Jan Niklas Hasse <jhasse@bixense.com>
// For conditions of distribution and use, see copyright notice in LICENSE.txt

#include "Shader.hpp"

#include "../Shader_Impl.hpp"

#if defined (JNGL_UWP) || defined (__EMSCRIPTEN__)
#include "debug.hpp"
#include "../log.hpp"
#endif

#include <sstream>
Expand Down Expand Up @@ -42,7 +42,7 @@ Shader::Shader(const char* source, const Type type, const char* const gles20Sour
if (gles20Source) {
source = gles20Source;
} else {
jngl::debugLn("WARNING: OpenGL ES 3.0 not supported on this platform!");
internal::warn("OpenGL ES 3.0 not supported on this platform!");
}
#endif
glShaderSource(impl->id, 1, &source, nullptr);
Expand Down
2 changes: 1 addition & 1 deletion src/jngl/debug.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2012-2021 Jan Niklas Hasse <jhasse@gmail.com>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
/// Logging functions for debug purposes
/// Logging functions for debug purposes, deprecated in favor of log.hpp
/// @file
#pragma once

Expand Down
64 changes: 22 additions & 42 deletions src/jngl/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,46 @@
#include "log.hpp"

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

#include <sstream>

namespace jngl {

void trace(const std::string& line) {
std::ostringstream tmp;
namespace {
std::string shortenDisplayName() {
if (auto displayName = App::instance().getDisplayName(); !displayName.empty()) {
tmp << '[';
if (displayName.size() > 4) {
tmp << displayName.substr(0, 3) << "…]";
std::ostringstream tmp;
tmp << "\x1b[1m";
const size_t MAX_LENGTH = 12;
if (displayName.size() > MAX_LENGTH) {
tmp << displayName.substr(0, MAX_LENGTH - 1) << "";
} else {
tmp << displayName << ']';
tmp << displayName;
}
tmp << "\x1b[0m";
return tmp.str();
}
tmp << "[\x1b[1;36mtrace\x1b[0m] " << line << '\n';
printMessage(tmp.str());
return {};
}
} // namespace

void trace(const std::string& line) {
#ifndef NDEBUG
internal::log(shortenDisplayName(), "\x1b[1;36mtrace\x1b[0m", line);
#endif
}

void info(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[32minfo\x1b[0m] " << line << '\n';
printMessage(tmp.str());
internal::log(shortenDisplayName(), "\x1b[32minfo\x1b[0m", line);
}

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());
internal::log(shortenDisplayName(), "\x1b[1;33mwarn\x1b[0m", line);
}

void error(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;31merror\x1b[0m] " << line << '\n';
printMessage(tmp.str());
internal::log(shortenDisplayName(), "\x1b[1;31merror\x1b[0m", line);
}

} // namespace jngl
11 changes: 3 additions & 8 deletions src/jngl/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#include "sprite.hpp"

#include "../helper.hpp"
#include "../log.hpp"
#include "../main.hpp"
#include "../spriteimpl.hpp"
#include "../texture.hpp"
#include "../windowptr.hpp"
#include "Alpha.hpp"
#include "debug.hpp"
#include "matrix.hpp"
#include "screen.hpp"

Expand Down Expand Up @@ -83,9 +83,7 @@ Sprite::Sprite(const std::string& filename, LoadType loadType) : texture(getText
}
const bool halfLoad = (loadType == LoadType::HALF);
if (!halfLoad) {
jngl::debug("Creating sprite ");
jngl::debug(filename);
jngl::debug("... ");
internal::debug("Creating sprite {}...", filename);
}
auto fullFilename = pathPrefix + filename;
const char* extensions[] = {
Expand Down Expand Up @@ -145,13 +143,10 @@ Sprite::Sprite(const std::string& filename, LoadType loadType) : texture(getText
throw std::runtime_error(std::string("File not found: " + fullFilename));
}
auto loadTexture = std::make_shared<Finally>(loadFunction(this, filename, pFile, halfLoad));
loader = std::make_shared<Finally>([pFile, loadTexture, halfLoad, this]() mutable {
loader = std::make_shared<Finally>([pFile, loadTexture, this]() mutable {
loadTexture.reset(); // call ~Finally
fclose(pFile);
setCenter(0, 0);
if (!halfLoad) {
jngl::debugLn("OK");
}
});
if (loadType != LoadType::THREADED) {
loader.reset();
Expand Down
85 changes: 85 additions & 0 deletions src/log.cpp
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
69 changes: 69 additions & 0 deletions src/log.hpp
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
Loading

0 comments on commit 964ca2a

Please sign in to comment.