Skip to content

Commit

Permalink
use fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
phisko committed Mar 3, 2023
1 parent c268492 commit 694ac96
Show file tree
Hide file tree
Showing 87 changed files with 455 additions and 381 deletions.
36 changes: 18 additions & 18 deletions kengine/adjustable/imgui/systems/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// putils
#include "putils/forward_to.hpp"
#include "putils/ini_file.hpp"
#include "putils/parse.hpp"
#include "putils/split.hpp"
#include "putils/static_assert.hpp"
#include "putils/to_string.hpp"
#include "putils/vector.hpp"

// kengine core
Expand Down Expand Up @@ -132,7 +132,7 @@ namespace kengine::adjustable::imgui {

ImGui::Separator();
if (ImGui::InputText("Name", name_search, sizeof(name_search))) {
kengine_logf(r, verbose, log_category, "Name search changed to '%s'", name_search);
kengine_logf(r, verbose, log_category, "Name search changed to '{}'", name_search);
search_out_of_date = true;
}
ImGui::Separator();
Expand Down Expand Up @@ -291,7 +291,7 @@ namespace kengine::adjustable::imgui {
KENGINE_PROFILING_SCOPE;

auto & comp = r.get<adjustable>(e);
kengine_logf(r, verbose, log_category, "Adjustable destroyed in [%u] (section %s)", e, comp.section.c_str());
kengine_logf(r, verbose, log_category, "Adjustable destroyed in {} (section {})", e, comp.section);
remove_adjustable_from_section(comp, root_section);
}

Expand All @@ -318,23 +318,23 @@ namespace kengine::adjustable::imgui {

void init_adjustable(adjustable & comp) noexcept {
KENGINE_PROFILING_SCOPE;
kengine_logf(r, verbose, log_category, "Initializing section %s", comp.section.c_str());
kengine_logf(r, verbose, log_category, "Initializing section {}", comp.section);

const auto it = loaded_file.sections.find(comp.section.c_str());
if (it == loaded_file.sections.end()) {
kengine_logf(r, warning, log_category, "Section '%s' not found in INI file", comp.section.c_str());
kengine_logf(r, warning, log_category, "Section '{}' not found in INI file", comp.section);
return;
}

const auto & section = it->second;
for (auto & value : comp.values) {
const auto it = section.values.find(value.name.c_str());
if (it == section.values.end()) {
kengine_logf(r, warning, log_category, "Value for '%s' not found in INI file", value.name.c_str());
kengine_logf(r, warning, log_category, "Value for '{}' not found in INI file", value.name);
continue;
}

kengine_logf(r, verbose, log_category, "Initializing %s to %s", value.name.c_str(), it->second.c_str());
kengine_logf(r, verbose, log_category, "Initializing {} to {}", value.name, it->second);
set_value(value, it->second.c_str());
}
}
Expand Down Expand Up @@ -395,39 +395,39 @@ namespace kengine::adjustable::imgui {

std::ofstream f(KENGINE_ADJUSTABLE_SAVE_FILE, std::ofstream::trunc);
if (!f) {
kengine_assert_failed(r, "Failed to open '", KENGINE_ADJUSTABLE_SAVE_FILE, "' with write permissions");
kengine_assert_failed(r, "Failed to open '" KENGINE_ADJUSTABLE_SAVE_FILE "' with write permissions");
return;
}

putils::ini_file ini;

for (const auto & [e, comp] : r.view<adjustable>().each()) {
kengine_logf(r, verbose, log_category, "Adding section %s to INI file", e, comp.section.c_str());
kengine_logf(r, verbose, log_category, "Adding section {} to INI file", e, comp.section);
auto & section = ini.sections[comp.section.c_str()];

for (const auto & value : comp.values) {
kengine_logf(r, verbose, log_category, "Adding value %s to section %s of INI file", e, value.name.c_str());
kengine_logf(r, verbose, log_category, "Adding value {} to section {} of INI file", e, value.name);
auto & ini_value = section.values[value.name.c_str()];

switch (value.type) {
case adjustable::value_type::Int: {
ini_value = putils::to_string(value.int_storage.value);
kengine_logf(r, verbose, log_category, "Adding int value %s", e, ini_value.c_str());
ini_value = fmt::format("{}", value.int_storage.value);
kengine_logf(r, verbose, log_category, "Adding int value {}", e, ini_value);
break;
}
case adjustable::value_type::Float: {
ini_value = putils::to_string(value.float_storage.value);
kengine_logf(r, verbose, log_category, "Adding float value %s", e, ini_value.c_str());
ini_value = fmt::format("{}", value.float_storage.value);
kengine_logf(r, verbose, log_category, "Adding float value {}", e, ini_value);
break;
}
case adjustable::value_type::Bool: {
ini_value = putils::to_string(value.bool_storage.value);
kengine_logf(r, verbose, log_category, "Adding bool value %s", e, ini_value.c_str());
ini_value = fmt::format("{}", value.bool_storage.value);
kengine_logf(r, verbose, log_category, "Adding bool value {}", e, ini_value);
break;
}
case adjustable::value_type::Color: {
ini_value = putils::to_string(putils::to_rgba(value.color_storage.value));
kengine_logf(r, verbose, log_category, "Adding color value %s", e, ini_value.c_str());
ini_value = fmt::format("{}", putils::to_rgba(value.color_storage.value));
kengine_logf(r, verbose, log_category, "Adding color value {}", e, ini_value);
break;
}
default: {
Expand Down
9 changes: 6 additions & 3 deletions kengine/async/helpers/process_results.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// stl
#include <chrono>

// fmt
#include <fmt/chrono.h>

// kengine core/log
#include "kengine/core/log/helpers/kengine_log.hpp"

Expand All @@ -29,16 +32,16 @@ namespace kengine::async {
const auto now = std::chrono::system_clock::now();

for (const auto & [e, async_result, async_task] : r.view<result<T>, task>().each()) {
const auto time_running = std::chrono::duration<float>(now - async_task.start).count();
const auto time_running = now - async_task.start;

using namespace std::chrono_literals;
const auto status = async_result.future.wait_for(0s);
if (status == std::future_status::timeout) {
kengine_logf(r, verbose, log_category, "Async task '%s' still running (started %fs ago)", async_task.name.c_str(), time_running);
kengine_logf(r, verbose, log_category, "Async task '{}' still running (started {:%S}s ago)", async_task.name, time_running);
continue;
}

kengine_logf(r, log, log_category, "Async task '%s' completed after %fs", async_task.name.c_str(), time_running);
kengine_logf(r, log, log_category, "Async task '{}' completed after {:%S}s", async_task.name, time_running);

auto data = async_result.future.get();

Expand Down
2 changes: 1 addition & 1 deletion kengine/async/helpers/start_task.inl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace kengine::async {
void start_task(entt::registry & r, entt::entity e, const task::string & task_name, std::future<T> && future) noexcept {
KENGINE_PROFILING_SCOPE;

kengine_logf(r, log, "async", "Async task '%s' starting", task_name.c_str());
kengine_logf(r, log, "async", "Async task '{}' starting", task_name);
r.emplace<task>(e, task_name);
r.emplace<result<T>>(e, std::move(future));
}
Expand Down
8 changes: 4 additions & 4 deletions kengine/async/helpers/tests/process_results.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TEST(async, process_results_deferred_return_value) {
const auto e = r.create();
kengine::async::start_task(
r, e,
kengine::async::task::string("%s %d", "hello", 0),
kengine::async::task::string("{} {}", "hello", 0),
std::async(std::launch::deferred, [] {
return 42;
})
Expand All @@ -36,7 +36,7 @@ TEST(async, process_results_deferred_side_effect) {
int result = 0;
kengine::async::start_task(
r, e,
kengine::async::task::string("%s %d", "hello", 0),
kengine::async::task::string("{} {}", "hello", 0),
std::async(std::launch::deferred, [&] {
result = 42;
return 0;
Expand All @@ -57,7 +57,7 @@ TEST(async, process_results_async_return_value) {
const auto e = r.create();
kengine::async::start_task(
r, e,
kengine::async::task::string("%s %d", "hello", 0),
kengine::async::task::string("{} {}", "hello", 0),
std::async(std::launch::async, [] {
return 42;
})
Expand All @@ -79,7 +79,7 @@ TEST(async, process_results_async_side_effect) {
int result = 0;
kengine::async::start_task(
r, e,
kengine::async::task::string("%s %d", "hello", 0),
kengine::async::task::string("{} {}", "hello", 0),
std::async(std::launch::async, [&] {
result = 42;
return 0;
Expand Down
2 changes: 1 addition & 1 deletion kengine/async/helpers/tests/start_task.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ TEST(async, start_task_formatted_name) {
const auto e = r.create();
kengine::async::start_task(
r, e,
kengine::async::task::string("%s %d", "hello", 0),
kengine::async::task::string("{} {}", "hello", 0),
std::async(std::launch::deferred, [] {})
);
EXPECT_EQ(r.get<kengine::async::task>(e).name, "hello 0");
Expand Down
2 changes: 1 addition & 1 deletion kengine/async/imgui/systems/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace kengine::async::imgui {

const auto now = std::chrono::system_clock::now();
for (const auto & [e, task] : r.view<async::task>().each()) {
kengine_logf(r, very_verbose, log_category, "Found async task %s", task.name.c_str());
kengine_logf(r, very_verbose, log_category, "Found async task {}", task.name);

ImGui::TableNextRow();

Expand Down
8 changes: 2 additions & 6 deletions kengine/command_line/helpers/create_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

// stl
#include <span>
#include <sstream>

// entt
#include <entt/entity/registry.hpp>
Expand All @@ -22,12 +21,9 @@ namespace kengine::command_line {

const auto e = r.create();
auto & comp = r.emplace<arguments>(e);
std::stringstream s;
for (const char * argument : std::span(argv, argc)) {
for (const char * argument : std::span(argv, argc))
comp.args.emplace_back(argument);
s << ' ' << argument;
}

kengine_logf(r, log, log_category, "Creating command-line:%s", s.str().c_str());
kengine_logf(r, log, log_category, "Creating command-line: {}", comp.args);
}
}
2 changes: 1 addition & 1 deletion kengine/command_line/helpers/parse.inl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace kengine::command_line {
kengine_log(r, verbose, log_category, "Parsing command-line");

for (const auto & [e, args] : r.view<arguments>().each()) {
kengine_logf(r, very_verbose, log_category, "Parsing command-line from [%u]", e);
kengine_logf(r, very_verbose, log_category, "Parsing command-line from {}", e);
return putils::parse_arguments<T>(args.args);
}

Expand Down
2 changes: 1 addition & 1 deletion kengine/core/assert/functions/on_assert_failed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "kengine/base_function.hpp"

namespace kengine::core::assert {
using on_assert_failed_signature = bool (const char * file, int line, const std::string & expr);
using on_assert_failed_signature = bool (const char * file, int line, const char * expr);
//! putils reflect all
//! parents: [refltype::base]
struct on_assert_failed : base_function<on_assert_failed_signature> {};
Expand Down
2 changes: 1 addition & 1 deletion kengine/core/assert/functions/on_assert_failed.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Prototype

```cpp
bool (const char * file, int line, const std::string & expr);
bool (const char * file, int line, const char * expr);
```

### Parameters
Expand Down
4 changes: 2 additions & 2 deletions kengine/core/assert/helpers/assert_failed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
namespace kengine::core::assert {
static constexpr auto log_category = "core_assert";

bool assert_failed(const entt::registry & r, const char * file, int line, const std::string & expr) noexcept {
bool assert_failed(const entt::registry & r, const char * file, int line, const char * expr) noexcept {
KENGINE_PROFILING_SCOPE;
kengine_logf(r, error, log_category, "%s:%d %s\nCallstack:\n%s", file, line, expr.c_str(), putils::get_call_stack(1).c_str());
kengine_logf(r, error, log_category, "{}:{} {}\nCallstack:\n{}", file, line, expr, putils::get_call_stack(1));

const auto view = r.view<on_assert_failed>();
if (view.empty())
Expand Down
2 changes: 1 addition & 1 deletion kengine/core/assert/helpers/assert_failed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#include <entt/entity/registry.hpp>

namespace kengine::core::assert {
KENGINE_CORE_ASSERT_EXPORT bool assert_failed(const entt::registry & r, const char * file, int line, const std::string & expr) noexcept;
KENGINE_CORE_ASSERT_EXPORT bool assert_failed(const entt::registry & r, const char * file, int line, const char * expr) noexcept;
}
2 changes: 1 addition & 1 deletion kengine/core/assert/helpers/assert_failed.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# [assert_failed](assert_failed.hpp)

```cpp
bool assert_failed(const entt::registry & r, const char * file, int line, const std::string & expr) noexcept;
bool assert_failed(const entt::registry & r, const char * file, int line, const char * expr) noexcept;
```
Logs an error and notifies [assert handlers](../functions/on_assert_failed.md).
4 changes: 2 additions & 2 deletions kengine/core/assert/helpers/kengine_assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
#include <entt/entity/fwd.hpp>

// putils
#include "putils/concatenate.hpp"
#include "putils/string.hpp"

// kengine core/assert
#include "kengine/core/assert/helpers/assert_failed.hpp"
#include "kengine/core/assert/helpers/is_debugger_present.hpp"

#define kengine_assert_failed(r, ...) \
do { \
const bool should_break = kengine::core::assert::assert_failed(r, __FILE__, __LINE__, putils::concatenate(__VA_ARGS__)); \
const bool should_break = kengine::core::assert::assert_failed(r, __FILE__, __LINE__, putils::string<1024>(__VA_ARGS__).c_str()); \
if (should_break && kengine::core::assert::is_debugger_present()) \
kengine_debug_break; \
} while (false)
Expand Down
45 changes: 45 additions & 0 deletions kengine/core/helpers/entt_formatter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

// fmt
#include <fmt/core.h>

// entt
#include <entt/entity/entity.hpp>
#include <entt/entity/handle.hpp>
#include <entt/entity/registry.hpp>

template<>
struct fmt::formatter<entt::entity> {
constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

template <typename FormatContext>
auto format(entt::entity e, FormatContext& ctx) const -> decltype(ctx.out()) {
return fmt::format_to(ctx.out(), "[{}]", std::underlying_type_t<entt::entity>(e));
}
};

template<>
struct fmt::formatter<entt::handle> {
constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

template <typename FormatContext>
auto format(entt::handle e, FormatContext& ctx) const -> decltype(ctx.out()) {
return fmt::format_to(ctx.out(), "{}", e.entity());
}
};

template<>
struct fmt::formatter<entt::const_handle> {
constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

template <typename FormatContext>
auto format(entt::const_handle e, FormatContext& ctx) const -> decltype(ctx.out()) {
return fmt::format_to(ctx.out(), "{}", e.entity());
}
};
3 changes: 3 additions & 0 deletions kengine/core/helpers/entt_formatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# [entt_formatter](entt_formatter.hpp)

`fmt::formatter` specialization for `entt::entity`, `entt::handle` and `entt::const_handle`.
2 changes: 1 addition & 1 deletion kengine/core/helpers/new_entity_processor.inl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace kengine {

const auto view = r.view<Comps...>(entt::exclude<ProcessedTag>);
for (const auto e : view) {
kengine_logf(r, very_verbose, "new_entity_processor", "Processing [%u]", e);
kengine_logf(r, very_verbose, "new_entity_processor", "Processing {}", e);

r.emplace<ProcessedTag>(e);
std::apply(
Expand Down
2 changes: 1 addition & 1 deletion kengine/core/log/file/systems/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace kengine::core::log::file {

file.open(file_name);
if (!file) {
kengine_assert_failed(r, "log_file system couldn't open output file '%s'", file_name);
kengine_assert_failed(r, "log_file system couldn't open output file '{}'", file_name);
return;
}

Expand Down
4 changes: 3 additions & 1 deletion kengine/core/log/helpers/kengine_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

// putils
#include "putils/string.hpp"
#include "putils/reflection_helpers/reflectible_formatter.hpp"

// kengine core/log
// kengine
#include "kengine/core/helpers/entt_formatter.hpp"
#include "kengine/core/log/helpers/log.hpp"

#ifndef KENGINE_LOG_MAX_SEVERITY
Expand Down
Loading

0 comments on commit 694ac96

Please sign in to comment.