Skip to content

Commit

Permalink
Rewrite thread policy and sink driver.
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 Oct 10, 2024
1 parent 3da81a9 commit c54914d
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 254 deletions.
24 changes: 7 additions & 17 deletions include/log/level.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

#pragma once

#include "policy.h"

#include <atomic>
#include <cstdint>

namespace PlainCloud::Log {

struct MultiThreadedPolicy;
struct SingleThreadedPolicy;

/**
* @brief Logging level enumeration.
*
Expand Down Expand Up @@ -90,20 +91,9 @@ class LevelDriver<SingleThreadedPolicy> final {
* @brief Multi-threaded log level driver.
*
* Handles log level access with atomic operations.
*
* @tparam Mutex Mutex type for synchronization.
* @tparam ReadLock Read lock type for synchronization.
* @tparam WriteLock Write lock type for synchronization.
* @tparam LoadOrder Memory order for load operations.
* @tparam StoreOrder Memory order for store operations.
*/
template<
typename Mutex,
typename ReadLock,
typename WriteLock,
std::memory_order LoadOrder,
std::memory_order StoreOrder>
class LevelDriver<MultiThreadedPolicy<Mutex, ReadLock, WriteLock, LoadOrder, StoreOrder>> final {
template<>
class LevelDriver<MultiThreadedPolicy> final {
public:
/**
* @brief Constructs a new LevelDriver object.
Expand All @@ -122,7 +112,7 @@ class LevelDriver<MultiThreadedPolicy<Mutex, ReadLock, WriteLock, LoadOrder, Sto
*/
explicit operator Level() const noexcept
{
return m_level.load(LoadOrder);
return m_level.load(std::memory_order_relaxed);
}

/**
Expand All @@ -133,7 +123,7 @@ class LevelDriver<MultiThreadedPolicy<Mutex, ReadLock, WriteLock, LoadOrder, Sto
*/
auto operator=(Level level) noexcept -> auto&
{
m_level.store(level, StoreOrder);
m_level.store(level, std::memory_order_relaxed);
return *this;
}

Expand Down
19 changes: 8 additions & 11 deletions include/log/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static constexpr auto DefaultBufferSize = 1024U;
template<
typename String,
typename Char = Util::Types::UnderlyingCharType<String>,
typename ThreadingPolicy = MultiThreadedPolicy<>,
typename ThreadingPolicy = MultiThreadedPolicy,
std::size_t StaticBufferSize = DefaultBufferSize>
class Logger {
public:
Expand Down Expand Up @@ -87,11 +87,10 @@ class Logger {
* @param level Logging level.
* @param parent Parent logger to inherit sinks from.
*/
explicit Logger(StringViewType category, Level level, const std::shared_ptr<Logger>& parent)
: m_parent(parent)
, m_category(category)
explicit Logger(StringViewType category, Level level, Logger& parent)
: m_category(category)
, m_level(level)
, m_sinks(this, &parent->m_sinks)
, m_sinks(this, &parent.m_sinks)
{
}

Expand All @@ -101,11 +100,10 @@ class Logger {
* @param category Logger category name. Can be used in logger messages.
* @param parent Parent logger to inherit sinks and logging level from.
*/
explicit Logger(StringViewType category, const std::shared_ptr<Logger>& parent)
: m_parent(parent)
, m_category(category)
, m_level(parent->level())
, m_sinks(this, &parent->m_sinks)
explicit Logger(StringViewType category, Logger& parent)
: m_category(category)
, m_level(parent.level())
, m_sinks(this, &parent.m_sinks)
{
}

Expand Down Expand Up @@ -288,7 +286,6 @@ class Logger {
}

private:
std::shared_ptr<Logger> m_parent;
std::basic_string<Char> m_category;
LevelDriver<ThreadingPolicy> m_level;
SinkDriver<Logger, ThreadingPolicy> m_sinks;
Expand Down
39 changes: 23 additions & 16 deletions include/log/policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#pragma once

#include <atomic>
#include <mutex>
#include <shared_mutex>

Expand All @@ -16,25 +15,33 @@ namespace PlainCloud::Log {
*
* This policy handles data manipulation without using any locks or atomic operations.
*/
struct SingleThreadedPolicy final { };
struct SingleThreadedPolicy final {
/** @brief Dummy mutex. */
struct Mutex {
/** @brief Lock method that does nothing. */
static void lock()
{
}

/** @brief Unlock method that does nothing. */
static void unlock()
{
}
};

using ReadLock = std::lock_guard<Mutex>; ///< Dummy read lock.
using WriteLock = std::lock_guard<Mutex>; ///< Dummy write lock.
};

/**
* @brief Policy for multi-threaded data manipulation.
*
* This policy ensures thread-safe data manipulation, potentially involving locking mechanisms.
*
* @tparam Mutex Type of mutex used for locking.
* @tparam ReadLock Type of lock used for read-only access.
* @tparam WriteLock Type of lock used for write access.
* @tparam LoadOrder Memory order used for loading values.
* @tparam StoreOrder Memory order used for storing new values.
* This policy ensures thread-safe data manipulation involving locking mechanisms.
*/
template<
typename Mutex = std::shared_mutex,
typename ReadLock = std::shared_lock<Mutex>,
typename WriteLock = std::unique_lock<Mutex>,
std::memory_order LoadOrder = std::memory_order_relaxed,
std::memory_order StoreOrder = std::memory_order_relaxed>
struct MultiThreadedPolicy final { };
struct MultiThreadedPolicy final {
using Mutex = std::shared_mutex; ///< Type of mutex used for locking.
using ReadLock = std::shared_lock<Mutex>; ///< Type of lock used for read-only access.
using WriteLock = std::unique_lock<Mutex>; ///< Type of lock used for write access.
};

} // namespace PlainCloud::Log
Loading

0 comments on commit c54914d

Please sign in to comment.