-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements for logging with formatting (#522)
* Improvements for logging with formatting * Adding comments and tests * Apply clang-format * Ci fixes --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
89c0bf1
commit 0f767ef
Showing
16 changed files
with
211 additions
and
5 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
52 changes: 52 additions & 0 deletions
52
modules/common/chowdsp_data_structures/Allocators/chowdsp_STLArenaAllocator.h
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,52 @@ | ||
#pragma once | ||
|
||
namespace chowdsp | ||
{ | ||
/** A (mostly) STL-conforming allocator backed by a memory arena of your choosing. */ | ||
template <class T, typename ArenaType> | ||
struct STLArenaAllocator | ||
{ | ||
using value_type = T; | ||
|
||
ArenaType& arena; | ||
|
||
explicit STLArenaAllocator (ArenaType& a) noexcept : arena (a) | ||
{ | ||
} | ||
|
||
template <class U> | ||
explicit STLArenaAllocator (const STLArenaAllocator<U, ArenaType>& other) noexcept | ||
: arena (other.arena) | ||
{ | ||
} | ||
|
||
template <typename U> | ||
struct rebind | ||
{ | ||
using other = STLArenaAllocator<U, ArenaType>; | ||
}; | ||
|
||
T* allocate (std::size_t n) | ||
{ | ||
return static_cast<T*> (arena.allocate_bytes (n, alignof (T))); | ||
} | ||
|
||
void deallocate (T*, std::size_t) const | ||
{ | ||
// no-op... | ||
// memory will be re-claimed when the arena is cleared. | ||
} | ||
}; | ||
|
||
template <class T, class U, typename Arena> | ||
constexpr bool operator== (const STLArenaAllocator<T, Arena>& x, const STLArenaAllocator<U, Arena>& y) noexcept | ||
{ | ||
return &x.arena == &y.arena; | ||
} | ||
|
||
template <class T, class U, typename Arena> | ||
constexpr bool operator!= (const STLArenaAllocator<T, Arena>& x, const STLArenaAllocator<U, Arena>& y) noexcept | ||
{ | ||
return ! (x == y); | ||
} | ||
} // namespace chowdsp |
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
17 changes: 17 additions & 0 deletions
17
modules/common/chowdsp_logging/Loggers/chowdsp_BaseLogger.cpp
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,17 @@ | ||
#include "chowdsp_BaseLogger.h" | ||
|
||
namespace chowdsp | ||
{ | ||
BaseLogger* BaseLogger::global_logger = nullptr; | ||
|
||
void set_global_logger (BaseLogger* logger) | ||
{ | ||
BaseLogger::global_logger = logger; | ||
juce::Logger::setCurrentLogger (logger); | ||
} | ||
|
||
BaseLogger* get_global_logger() | ||
{ | ||
return BaseLogger::global_logger; | ||
} | ||
} // namespace chowdsp |
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
65 changes: 65 additions & 0 deletions
65
modules/common/chowdsp_logging/Loggers/chowdsp_FormatHelpers.h
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,65 @@ | ||
#pragma once | ||
|
||
/** Custom formatter for juce::String */ | ||
template <> | ||
struct fmt::formatter<juce::String> : formatter<std::string> | ||
{ | ||
static auto format (const juce::String& str, format_context& ctx) -> decltype (ctx.out()) | ||
{ | ||
return fmt::format_to (ctx.out(), "{}", str.toStdString()); | ||
} | ||
}; | ||
|
||
/** Custom formatter for std::span */ | ||
template <typename T> | ||
struct fmt::formatter<nonstd::span<T>> : formatter<std::string> | ||
{ | ||
static auto format (nonstd::span<const T> span, format_context& ctx) -> decltype (ctx.out()) | ||
{ | ||
return fmt::format_to (ctx.out(), "{{{}}}", fmt::join (span, ",")); | ||
} | ||
}; | ||
|
||
namespace chowdsp | ||
{ | ||
/** Implementation of fmt::vformat using a memory arena. */ | ||
template <typename ArenaType> | ||
std::string_view vformat (ArenaType& arena, fmt::string_view format_str, fmt::format_args args) | ||
{ | ||
using FormatAllocator = STLArenaAllocator<char, ArenaType>; | ||
FormatAllocator alloc { arena }; | ||
fmt::basic_memory_buffer<char, 1, FormatAllocator> buffer { alloc }; | ||
fmt::vformat_to (std::back_inserter (buffer), format_str, args); | ||
return { buffer.data(), buffer.size() }; | ||
} | ||
|
||
/** Implementation of fmt::format using a memory arena. */ | ||
template <typename ArenaType, typename... Args> | ||
std::string_view format (ArenaType& arena, fmt::string_view format_str, const Args&... args) | ||
{ | ||
return vformat (arena, format_str, fmt::make_format_args (args...)); | ||
} | ||
|
||
/** Implementation of fmt::format using the global logger's memory arena. */ | ||
template <typename... Args> | ||
std::string_view format (fmt::string_view format_str, const Args&... args) | ||
{ | ||
auto* global_logger = get_global_logger(); | ||
if (global_logger == nullptr) | ||
{ | ||
// make sure you've set up the global logger before calling this! | ||
jassertfalse; | ||
return {}; | ||
} | ||
|
||
// If this is being called from multiple threads, we might need to synchronize the arena here? | ||
return format (global_logger->arena, format_str, std::forward<const Args&> (args)...); | ||
} | ||
|
||
/** A formatted wrapped for juce::Logger::writeToLog(). */ | ||
template <typename... Args> | ||
void log (fmt::string_view format_str, const Args&... args) | ||
{ | ||
juce::Logger::writeToLog (toString (format (format_str, std::forward<const Args&> (args)...))); | ||
} | ||
} // namespace chowdsp |
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
27 changes: 27 additions & 0 deletions
27
tests/common_tests/chowdsp_data_structures_test/STLArenaAllocatorTest.cpp
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,27 @@ | ||
#include <CatchUtils.h> | ||
#include <chowdsp_data_structures/chowdsp_data_structures.h> | ||
|
||
TEST_CASE ("STL Arena Allocator Test", "[common][data-structures]") | ||
{ | ||
using Arena = chowdsp::ArenaAllocator<>; | ||
Arena arena { 128 }; | ||
|
||
using Alloc = chowdsp::STLArenaAllocator<int, Arena>; | ||
Alloc alloc { arena }; | ||
|
||
using custom_vector = std::vector<int, Alloc>; | ||
custom_vector vec { { 1, 2, 3, 4 }, alloc }; | ||
REQUIRE (vec.size() == 4); | ||
REQUIRE (vec.front() == 1); | ||
REQUIRE (vec.back() == 4); | ||
|
||
vec.push_back (5); | ||
REQUIRE (vec.size() == 5); | ||
REQUIRE (vec.back() == 5); | ||
|
||
vec.erase (vec.begin()); | ||
REQUIRE (vec.size() == 4); | ||
vec.insert (vec.begin(), 0); | ||
REQUIRE (vec.size() == 5); | ||
REQUIRE (vec.front() == 0); | ||
} |
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
21 changes: 21 additions & 0 deletions
21
tests/common_tests/chowdsp_logging_test/CustomFormattingTest.cpp
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,21 @@ | ||
#include <chowdsp_logging/chowdsp_logging.h> | ||
#include <CatchUtils.h> | ||
|
||
TEST_CASE ("Custom Formatting Test", "[common][logs]") | ||
{ | ||
chowdsp::ArenaAllocator<> arena { 2048 }; | ||
|
||
SECTION ("juce::String") | ||
{ | ||
juce::String str { "This is a JUCE string!" }; | ||
const auto format_result = chowdsp::format (arena, "{}", str); | ||
REQUIRE (str == chowdsp::toString (format_result)); | ||
} | ||
|
||
SECTION ("std::span") | ||
{ | ||
std::vector vec { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f }; | ||
const auto format_result = chowdsp::format (arena, "{}", nonstd::span { vec }); | ||
REQUIRE (format_result == "{0,1,2,3,4}"); | ||
} | ||
} |
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