Skip to content

Commit

Permalink
progress2...
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Artsishevsky <polter.rnd@gmail.com>
  • Loading branch information
polter-rnd committed Jan 6, 2025
1 parent 3657efb commit b30f280
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 223 deletions.
6 changes: 5 additions & 1 deletion .iwyu-mappings
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
{ symbol: [ "std::reference_wrapper", "private", "<functional>", "public" ] },
{ symbol: [ "std::copy", "private", "<algorithm>", "public" ] },
# Bug on fmtlib: format_error::~format_error is not from "format.h"
{ include: [ "\"format.h\"", "private", "<slimlog/format.h>", "public" ] },
{ include: [ "\"slimlog/format.h\"", "private", "<slimlog/format.h>", "public" ] },
{ include: [ "\"slimlog/level.h\"", "private", "<slimlog/level.h>", "public" ] },
{ include: [ "\"slimlog/record.h\"", "private", "<slimlog/record.h>", "public" ] },
{ include: [ "\"slimlog/location.h\"", "private", "<slimlog/location.h>", "public" ] },
{ include: [ "\"slimlog/policy.h\"", "private", "<slimlog/policy.h>", "public" ] },
]
11 changes: 1 addition & 10 deletions include/slimlog/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@

// IWYU pragma: private, include <slimlog/format.h>

#ifndef SLIMLOG_HEADER_ONLY
#include <slimlog/format.h>
#endif
#include <slimlog/format.h> // IWYU pragma: associated

#ifdef SLIMLOG_FMTLIB
#if __has_include(<fmt/base.h>)
#include <fmt/base.h>
#else
#include <fmt/core.h>
#endif

#include <iterator>
#include <type_traits>
#else
#include <array>
#endif

#include <chrono>
#include <string_view>
#include <utility>

namespace SlimLog {

#ifndef SLIMLOG_FMTLIB
Expand Down
63 changes: 48 additions & 15 deletions include/slimlog/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
namespace SlimLog {

/**
* @brief Default buffer size for log messages.
* @brief Default buffer size for raw log messages.
*/
static constexpr auto DefaultBufferSize = 256U;

/**
* @brief Default threading policy for logger sinks.
*/
using DefaultThreadingPolicy = MultiThreadedPolicy;

/**
* @brief Logger front-end class.
*
Expand All @@ -34,13 +39,15 @@ static constexpr auto DefaultBufferSize = 256U;
* @tparam String Type used for logging messages (e.g., `std::string`).
* @tparam Char Underlying character type for the string.
* @tparam ThreadingPolicy Threading policy for sink operations.
* @tparam StaticBufferSize Size of the internal pre-allocated buffer.
* @tparam BufferSize Size of the internal pre-allocated buffer.
* @tparam Allocator Allocator type for the internal buffer.
*/
template<
typename String,
typename Char = Util::Types::UnderlyingCharType<String>,
typename ThreadingPolicy = MultiThreadedPolicy,
std::size_t StaticBufferSize = DefaultBufferSize>
typename ThreadingPolicy = DefaultThreadingPolicy,
std::size_t BufferSize = DefaultBufferSize,
typename Allocator = std::allocator<Char>>
class Logger {
public:
/** @brief String type for log messages. */
Expand All @@ -49,8 +56,10 @@ class Logger {
using CharType = Char;
/** @brief String view type for log categories. */
using StringViewType = std::basic_string_view<CharType>;
/** @brief Size of the internal buffer. */
static constexpr auto BufferSize = StaticBufferSize;
/** @brief Base sink type for the logger. */
using SinkType = Sink<String, Char>;
/** @brief Buffer type used for log message formatting. */
using FormatBufferType = FormatBuffer<Char, BufferSize, Allocator>;

Logger(Logger const&) = delete;
Logger(Logger&&) = delete;
Expand Down Expand Up @@ -114,23 +123,47 @@ class Logger {
* @param sink Shared pointer to the sink.
* @return true if the sink was added, false if it already exists.
*/
auto add_sink(const std::shared_ptr<Sink<Logger>>& sink) -> bool
auto add_sink(const std::shared_ptr<SinkType>& sink) -> bool
{
return m_sinks.add_sink(sink);
}

/**
* @brief Creates and adds a new formattable sink to this logger.
*
* @tparam T Sink type template (e.g., OStreamSink).
* @tparam SinkBufferSize Size of the internal buffer for the sink.
* @tparam SinkAllocator Allocator type for the sink buffer.
* @tparam Args Argument types for the sink constructor.
* @param args Arguments forwarded to the sink constructor.
* @return Shared pointer to the created sink.
*/
template<
template<typename, typename, std::size_t, typename>
typename T,
std::size_t SinkBufferSize = DefaultBufferSize,
typename SinkAllocator = Allocator,
typename... Args>
requires(IsFormattableSink<T<String, Char, SinkBufferSize, SinkAllocator>>)
auto add_sink(Args&&... args) -> std::shared_ptr<SinkType>
{
return m_sinks.template add_sink<T<String, Char, SinkBufferSize, SinkAllocator>>(
std::forward<Args>(args)...);
}

/**
* @brief Creates and adds a new sink to this logger.
*
* @tparam T Sink type template (e.g., OStreamSink).
* @tparam Args Constructor argument types for the sink.
* @tparam Args Argument types for the sink constructor.
* @param args Arguments forwarded to the sink constructor.
* @return Shared pointer to the created sink.
*/
template<template<typename> class T, typename... Args>
auto add_sink(Args&&... args) -> std::shared_ptr<Sink<Logger>>
template<template<typename, typename> class T, typename... Args>
requires(!IsFormattableSink<T<String, Char>>)
auto add_sink(Args&&... args) -> std::shared_ptr<SinkType>
{
return m_sinks.template add_sink<T<Logger>>(std::forward<Args>(args)...);
return m_sinks.template add_sink<T<String, Char>>(std::forward<Args>(args)...);
}

/**
Expand All @@ -140,7 +173,7 @@ class Logger {
* @return \b true if the sink was actually removed.
* @return \b false if the sink does not exist in this logger.
*/
auto remove_sink(const std::shared_ptr<Sink<Logger>>& sink) -> bool
auto remove_sink(const std::shared_ptr<SinkType>& sink) -> bool
{
return m_sinks.remove_sink(sink);
}
Expand All @@ -153,7 +186,7 @@ class Logger {
* @return \b true if the sink exists and is enabled.
* @return \b false if the sink does not exist in this logger.
*/
auto set_sink_enabled(const std::shared_ptr<Sink<Logger>>& sink, bool enabled) -> bool
auto set_sink_enabled(const std::shared_ptr<SinkType>& sink, bool enabled) -> bool
{
return m_sinks.set_sink_enabled(sink, enabled);
}
Expand All @@ -165,7 +198,7 @@ class Logger {
* @return \b true if the sink is enabled.
* @return \b false if the sink is disabled.
*/
[[nodiscard]] auto sink_enabled(const std::shared_ptr<Sink<Logger>>& sink) const -> bool
[[nodiscard]] auto sink_enabled(const std::shared_ptr<SinkType>& sink) const -> bool
{
return m_sinks.sink_enabled(sink);
}
Expand Down Expand Up @@ -238,7 +271,7 @@ class Logger {
void
message(Level level, Format<CharType, std::type_identity_t<Args>...> fmt, Args&&... args) const
{
auto callback = [&fmt = fmt.fmt()](auto& buffer, Args&&... args) {
auto callback = [&fmt = fmt.fmt()](FormatBufferType& buffer, Args&&... args) {
buffer.format(fmt, std::forward<Args>(args)...);
};

Expand Down
21 changes: 2 additions & 19 deletions include/slimlog/pattern-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,15 @@

// IWYU pragma: private, include <slimlog/pattern.h>

#ifndef SLIMLOG_HEADER_ONLY
#include <slimlog/pattern.h>
#endif

#include <slimlog/format.h>
#include <slimlog/level.h>
#include <slimlog/record.h>
#include <slimlog/pattern.h> // IWYU pragma: associated
#include <slimlog/util/types.h>
#include <slimlog/util/unicode.h>

#include <algorithm>
#include <array>
#include <chrono>
#include <climits>
#include <concepts>
#include <cstddef>
#include <functional>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>

namespace SlimLog {

Expand Down Expand Up @@ -144,7 +127,7 @@ auto Pattern<Char>::format(auto& out, Record<Char, StringType>& record) -> void
case Placeholder::Type::Message:
std::visit(
Util::Types::Overloaded{
[&out, &value = item.value](std::reference_wrapper<StringType> arg) {
[&out, &value = item.value](std::reference_wrapper<const StringType> arg) {
if constexpr (Detail::HasConvertString<Char, StringType>) {
format_string(
out,
Expand Down
8 changes: 1 addition & 7 deletions include/slimlog/record-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@

// IWYU pragma: private, include <slimlog/record.h>

#ifndef SLIMLOG_HEADER_ONLY
#include <slimlog/record.h>
#endif

#include <slimlog/record.h> // IWYU pragma: associated
#include <slimlog/util/unicode.h>

#include <atomic>
#include <cstddef>
#include <string_view>
#include <utility>

namespace SlimLog {
Expand Down
10 changes: 7 additions & 3 deletions include/slimlog/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,17 @@ struct RecordLocation {
*/
template<typename Char, typename String>
struct Record {
/** @brief String reference type. */
using StringRefType = std::reference_wrapper<const String>;
/** @brief String view type. */
using StringViewType = RecordStringView<Char>;

Level level = {}; ///< Log level.
RecordLocation location = {}; ///< Source code location.
RecordStringView<Char> category = {}; ///< Log category.
StringViewType category = {}; ///< Log category.
std::size_t thread_id = {}; ///< Thread ID.
RecordTime time = {}; ///< Record time.
std::variant<std::reference_wrapper<String>, RecordStringView<Char>> message
= RecordStringView<Char>{}; ///< Log message.
std::variant<StringRefType, StringViewType> message = StringViewType{}; ///< Log message.
};

} // namespace SlimLog
Expand Down
44 changes: 16 additions & 28 deletions include/slimlog/sink-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,34 @@

#ifndef SLIMLOG_HEADER_ONLY
#include <slimlog/logger.h>
#include <slimlog/sink.h>
#endif

#include <slimlog/level.h>
#include <slimlog/location.h>
#include <slimlog/pattern.h>
#include <slimlog/policy.h>
#include <slimlog/record.h>
#include <slimlog/sink.h> // IWYU pragma: associated
#include <slimlog/util/os.h>

#include <algorithm> // IWYU pragma: keep
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <string_view>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

namespace SlimLog {

template<typename Logger>
auto Sink<Logger>::set_pattern(StringViewType pattern) -> void
template<typename String, typename Char, std::size_t BufferSize, typename Allocator>
auto FormattableSink<String, Char, BufferSize, Allocator>::set_levels(
std::initializer_list<std::pair<Level, StringViewType>> levels) -> void
{
m_pattern.set_pattern(std::move(pattern));
m_pattern.set_levels(std::move(levels));
}

template<typename Logger>
auto Sink<Logger>::set_levels(std::initializer_list<std::pair<Level, StringViewType>> levels)
template<typename String, typename Char, std::size_t BufferSize, typename Allocator>
auto FormattableSink<String, Char, BufferSize, Allocator>::set_pattern(StringViewType pattern)
-> void
{
m_pattern.set_levels(std::move(levels));
m_pattern.set_pattern(std::move(pattern));
}

template<typename Logger>
auto Sink<Logger>::format(FormatBufferType& result, RecordType& record) -> void
template<typename String, typename Char, std::size_t BufferSize, typename Allocator>
auto FormattableSink<String, Char, BufferSize, Allocator>::format(
FormatBufferType& result, RecordType& record) -> void
{
m_pattern.format(result, record);
}
Expand Down Expand Up @@ -75,8 +65,7 @@ SinkDriver<Logger, ThreadingPolicy>::~SinkDriver()
}

template<typename Logger, typename ThreadingPolicy>
auto SinkDriver<Logger, ThreadingPolicy>::add_sink(const std::shared_ptr<Sink<Logger>>& sink)
-> bool
auto SinkDriver<Logger, ThreadingPolicy>::add_sink(const std::shared_ptr<SinkType>& sink) -> bool
{
const typename ThreadingPolicy::WriteLock lock(m_mutex);
const auto result = m_sinks.insert_or_assign(sink, true).second;
Expand All @@ -85,8 +74,7 @@ auto SinkDriver<Logger, ThreadingPolicy>::add_sink(const std::shared_ptr<Sink<Lo
}

template<typename Logger, typename ThreadingPolicy>
auto SinkDriver<Logger, ThreadingPolicy>::remove_sink(const std::shared_ptr<Sink<Logger>>& sink)
-> bool
auto SinkDriver<Logger, ThreadingPolicy>::remove_sink(const std::shared_ptr<SinkType>& sink) -> bool
{
const typename ThreadingPolicy::WriteLock lock(m_mutex);
if (m_sinks.erase(sink) > 0) {
Expand All @@ -98,7 +86,7 @@ auto SinkDriver<Logger, ThreadingPolicy>::remove_sink(const std::shared_ptr<Sink

template<typename Logger, typename ThreadingPolicy>
auto SinkDriver<Logger, ThreadingPolicy>::set_sink_enabled(
const std::shared_ptr<Sink<Logger>>& sink, bool enabled) -> bool
const std::shared_ptr<SinkType>& sink, bool enabled) -> bool
{
const typename ThreadingPolicy::WriteLock lock(m_mutex);
if (const auto itr = m_sinks.find(sink); itr != m_sinks.end()) {
Expand All @@ -110,8 +98,8 @@ auto SinkDriver<Logger, ThreadingPolicy>::set_sink_enabled(
}

template<typename Logger, typename ThreadingPolicy>
auto SinkDriver<Logger, ThreadingPolicy>::sink_enabled(
const std::shared_ptr<Sink<Logger>>& sink) const -> bool
auto SinkDriver<Logger, ThreadingPolicy>::sink_enabled(const std::shared_ptr<SinkType>& sink) const
-> bool
{
const typename ThreadingPolicy::ReadLock lock(m_mutex);
if (const auto itr = m_sinks.find(sink); itr != m_sinks.end()) {
Expand Down
Loading

0 comments on commit b30f280

Please sign in to comment.