Skip to content

Commit

Permalink
Tweak runtime logging API
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 18, 2024
1 parent 02407e1 commit c4ac2a7
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

#include <new> // nothrow

#include "tactile/runtime/runtime.hpp"
#include "tactile/runtime/logging.hpp"

namespace tactile {

void GodotTscnFormatPlugin::load(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Loading Godot TSCN format plugin");
log(LogLevel::kTrace, "Loading Godot TSCN format plugin");
}

void GodotTscnFormatPlugin::unload(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Unloading Godot TSCN format plugin");
log(LogLevel::kTrace, "Unloading Godot TSCN format plugin");
}

auto tactile_make_plugin() -> IPlugin*
Expand Down
9 changes: 5 additions & 4 deletions source/null_renderer/lib/src/null_renderer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

#include <new> // nothrow

#include "tactile/runtime/runtime.hpp"
#include "tactile/base/runtime.hpp"
#include "tactile/runtime/logging.hpp"

namespace tactile {

void NullRendererPlugin::load(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Loading null renderer plugin");
log(LogLevel::kTrace, "Loading null renderer plugin");

runtime.init_window(0);
auto* window = runtime.get_window();

if (!window) {
Runtime::log(LogLevel::kError, "Could not initialize window");
log(LogLevel::kError, "Could not initialize window");
return;
}

Expand All @@ -26,7 +27,7 @@ void NullRendererPlugin::load(IRuntime& runtime)

void NullRendererPlugin::unload(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Unloading null renderer plugin");
log(LogLevel::kTrace, "Unloading null renderer plugin");

runtime.set_renderer(nullptr);
mRenderer.reset();
Expand Down
10 changes: 5 additions & 5 deletions source/null_renderer/lib/src/null_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

#include "tactile/runtime/runtime.hpp"
#include "tactile/runtime/logging.hpp"

namespace tactile {

Expand All @@ -26,10 +26,10 @@ auto NullTexture::load(Path path) -> Result<NullTexture>
stbi_load(path_string.c_str(), &size.width, &size.height, nullptr, STBI_default);

if (!pixels) {
Runtime::log(LogLevel::kError,
"Could not load texture '{}': {}",
path_string,
stbi_failure_reason());
log(LogLevel::kError,
"Could not load texture '{}': {}",
path_string,
stbi_failure_reason());
return unexpected(std::make_error_code(std::errc::io_error));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "tactile/opengl_renderer/opengl_error.hpp"
#include "tactile/opengl_renderer/opengl_imgui.hpp"
#include "tactile/opengl_renderer/opengl_texture.hpp"
#include "tactile/runtime/runtime.hpp"
#include "tactile/runtime/logging.hpp"

namespace tactile {

Expand All @@ -43,8 +43,7 @@ struct OpenGLRenderer::Data final // NOLINT(*-member-init)
TextureID next_texture_id;
};

auto OpenGLRenderer::make(IWindow* window,
ImGuiContext* context) -> Result<OpenGLRenderer>
auto OpenGLRenderer::make(IWindow* window, ImGuiContext* context) -> Result<OpenGLRenderer>
{
if (SDL_WasInit(SDL_INIT_VIDEO) != SDL_INIT_VIDEO) {
return unexpected(make_error(OpenGLError::kNotReady));
Expand All @@ -65,18 +64,16 @@ auto OpenGLRenderer::make(IWindow* window,
}

// NOLINTBEGIN(*-no-malloc)
ImGui::SetAllocatorFunctions(
[](const usize size, void*) { return std::malloc(size); },
[](void* ptr, void*) { std::free(ptr); });
ImGui::SetAllocatorFunctions([](const usize size, void*) { return std::malloc(size); },
[](void* ptr, void*) { std::free(ptr); });
// NOLINTEND(*-no-malloc)
ImGui::SetCurrentContext(context);

if (!gladLoadGLLoader(&SDL_GL_GetProcAddress)) {
return unexpected(make_error(OpenGLError::kLoaderError));
}

if (auto imgui_backend =
GLImGuiBackendWrapper::init(window->get_handle(), context)) {
if (auto imgui_backend = GLImGuiBackendWrapper::init(window->get_handle(), context)) {
renderer.mData->imgui_backend.emplace(std::move(*imgui_backend));
}
else {
Expand All @@ -101,8 +98,7 @@ OpenGLRenderer::OpenGLRenderer()

OpenGLRenderer::OpenGLRenderer(OpenGLRenderer&& other) noexcept = default;

auto OpenGLRenderer::operator=(OpenGLRenderer&& other) noexcept
-> OpenGLRenderer& = default;
auto OpenGLRenderer::operator=(OpenGLRenderer&& other) noexcept -> OpenGLRenderer& = default;

OpenGLRenderer::~OpenGLRenderer() noexcept = default;

Expand All @@ -113,10 +109,7 @@ auto OpenGLRenderer::begin_frame() -> bool
ImGui::NewFrame();

const auto& io = ImGui::GetIO();
glViewport(0,
0,
static_cast<int>(io.DisplaySize.x),
static_cast<int>(io.DisplaySize.y));
glViewport(0, 0, static_cast<int>(io.DisplaySize.x), static_cast<int>(io.DisplaySize.y));

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Expand Down Expand Up @@ -162,8 +155,7 @@ void OpenGLRenderer::unload_texture(const TextureID id)

auto OpenGLRenderer::find_texture(const TextureID id) const -> const ITexture*
{
if (const auto iter = mData->textures.find(id);
iter != mData->textures.end()) {
if (const auto iter = mData->textures.find(id); iter != mData->textures.end()) {
return &iter->second;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

#include <SDL2/SDL.h>

#include "tactile/runtime/runtime.hpp"
#include "tactile/base/runtime.hpp"
#include "tactile/runtime/logging.hpp"

namespace tactile {
namespace opengl_renderer_plugin {

void set_hints()
{
if constexpr (kOnMacos) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS,
SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
}

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
Expand All @@ -40,7 +40,7 @@ void OpenGLRendererPlugin::load(IRuntime& runtime)
auto* window = runtime.get_window();

if (!window) {
Runtime::log(LogLevel::kError, "Could not initialize OpenGL window");
log(LogLevel::kError, "Could not initialize OpenGL window");
return;
}

Expand Down
2 changes: 2 additions & 0 deletions source/runtime/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_library(tactile::runtime ALIAS tactile-runtime)
target_sources(tactile-runtime
PRIVATE
"src/tactile/runtime/launcher.cpp"
"src/tactile/runtime/logging.cpp"
"src/tactile/runtime/plugin_instance.cpp"
"src/tactile/runtime/protobuf_context.cpp"
"src/tactile/runtime/runtime.cpp"
Expand All @@ -15,6 +16,7 @@ target_sources(tactile-runtime
PUBLIC FILE_SET "HEADERS" BASE_DIRS "inc" FILES
"inc/tactile/runtime/api.hpp"
"inc/tactile/runtime/launcher.hpp"
"inc/tactile/runtime/logging.hpp"
"inc/tactile/runtime/plugin.hpp"
"inc/tactile/runtime/plugin_instance.hpp"
"inc/tactile/runtime/protobuf_context.hpp"
Expand Down
34 changes: 34 additions & 0 deletions source/runtime/lib/inc/tactile/runtime/logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#pragma once

#include <format> // make_format_args, format_args

#include "tactile/base/container/string.hpp"
#include "tactile/base/log/log_level.hpp"
#include "tactile/base/prelude.hpp"
#include "tactile/runtime/api.hpp"

namespace tactile {
namespace internal {

TACTILE_RUNTIME_API void log(LogLevel level, StringView fmt, std::format_args args);

} // namespace internal

/**
* Logs a message using the internal logger.
*
* \tparam Args The format argument types.
*
* \param level The severity of the message.
* \param fmt The format string.
* \param args The format arguments.
*/
template <typename... Args>
void log(const LogLevel level, const StringView fmt, const Args&... args)
{
internal::log(level, fmt, std::make_format_args(args...));
}

} // namespace tactile
23 changes: 1 addition & 22 deletions source/runtime/lib/inc/tactile/runtime/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

#pragma once

#include <format> // format_args, make_format_args

#include "tactile/base/container/smart_ptr.hpp"
#include "tactile/base/container/string.hpp"
#include "tactile/base/int.hpp"
#include "tactile/base/io/compress/compression_format.hpp"
#include "tactile/base/io/save/save_format_id.hpp"
#include "tactile/base/log/log_level.hpp"
#include "tactile/base/prelude.hpp"
#include "tactile/base/runtime.hpp"
#include "tactile/runtime/api.hpp"
Expand All @@ -19,7 +15,7 @@ struct ImGuiContext;
namespace tactile {

/**
* Provides the primary API used by dynamic Tactile modules.
* Implements the runtime interface.
*/
class TACTILE_RUNTIME_API Runtime final : public IRuntime
{
Expand All @@ -46,21 +42,6 @@ class TACTILE_RUNTIME_API Runtime final : public IRuntime

~Runtime() noexcept override;

/**
* Logs a message using the internal logger.
*
* \tparam Args The format argument types.
*
* \param level The severity of the message.
* \param fmt The format string.
* \param args The format arguments.
*/
template <typename... Args>
static void log(const LogLevel level, const StringView fmt, const Args&... args)
{
_log(level, fmt, std::make_format_args(args...));
}

void init_window(uint32 flags) override;

void set_renderer(IRenderer* renderer) override;
Expand All @@ -87,8 +68,6 @@ class TACTILE_RUNTIME_API Runtime final : public IRuntime
private:
struct Data;
Unique<Data> mData;

static void _log(LogLevel level, StringView fmt, std::format_args args);
};

} // namespace tactile
17 changes: 17 additions & 0 deletions source/runtime/lib/src/tactile/runtime/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#include "tactile/runtime/logging.hpp"

#include "tactile/base/util/buffer.hpp"
#include "tactile/core/log/logger.hpp"

namespace tactile::internal {

void log(const LogLevel level, const StringView fmt, const std::format_args args)
{
Buffer<char, 256> buffer; // NOLINT uninitialized
vformat_to_buffer(buffer, fmt, args);
TACTILE_LOG(level, "{}", buffer.view());
}

} // namespace tactile::internal
8 changes: 0 additions & 8 deletions source/runtime/lib/src/tactile/runtime/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "tactile/base/container/hash_map.hpp"
#include "tactile/base/container/maybe.hpp"
#include "tactile/base/util/buffer.hpp"
#include "tactile/core/debug/terminate.hpp"
#include "tactile/core/log/logger.hpp"
#include "tactile/core/log/set_log_scope.hpp"
Expand Down Expand Up @@ -183,11 +182,4 @@ auto Runtime::get_imgui_context() -> ImGuiContext*
return mData->imgui_context.get();
}

void Runtime::_log(const LogLevel level, const StringView fmt, std::format_args args)
{
Buffer<char, 256> buffer; // NOLINT uninitialized
vformat_to_buffer(buffer, fmt, args);
TACTILE_LOG(level, "{}", buffer.view());
}

} // namespace tactile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

#include <new> // nothrow

#include "tactile/runtime/runtime.hpp"
#include "tactile/base/runtime.hpp"
#include "tactile/runtime/logging.hpp"

namespace tactile {

void TiledTmjFormatPlugin::load(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Loading Tiled TMJ format plugin");
log(LogLevel::kTrace, "Loading Tiled TMJ format plugin");
runtime.set_save_format(SaveFormatId::kTiledTmj, &mFormat);
}

void TiledTmjFormatPlugin::unload(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Unloading Tiled TMJ format plugin");
log(LogLevel::kTrace, "Unloading Tiled TMJ format plugin");
runtime.set_save_format(SaveFormatId::kTiledTmj, nullptr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

#include <new> // nothrow

#include "tactile/runtime/runtime.hpp"
#include "tactile/runtime/logging.hpp"

namespace tactile {

void TiledTmxFormatPlugin::load(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Loading Tiled TMX format plugin");
log(LogLevel::kTrace, "Loading Tiled TMX format plugin");
}

void TiledTmxFormatPlugin::unload(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Unloading Tiled TMX format plugin");
log(LogLevel::kTrace, "Unloading Tiled TMX format plugin");
}

auto tactile_make_plugin() -> IPlugin*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

#include <new> // nothrow

#include "tactile/runtime/runtime.hpp"
#include "tactile/runtime/logging.hpp"

namespace tactile {

void VulkanRendererPlugin::load(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Loading Vulkan renderer plugin");
log(LogLevel::kTrace, "Loading Vulkan renderer plugin");
}

void VulkanRendererPlugin::unload(IRuntime& runtime)
{
Runtime::log(LogLevel::kTrace, "Unloading Vulkan renderer plugin");
log(LogLevel::kTrace, "Unloading Vulkan renderer plugin");
}

auto tactile_make_plugin() -> IPlugin*
Expand Down
Loading

0 comments on commit c4ac2a7

Please sign in to comment.