mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-12 13:59:46 -04:00
refactor(logger): simplify and factor out logging_class_factory
This commit is contained in:
parent
8c4e840dcf
commit
10c943f9c7
94
include/dwarfs/detail/logging_class_factory.h
Normal file
94
include/dwarfs/detail/logging_class_factory.h
Normal file
@ -0,0 +1,94 @@
|
||||
/* vim:set ts=2 sw=2 sts=2 et: */
|
||||
/**
|
||||
* \author Marcus Holland-Moritz (github@mhxnet.de)
|
||||
* \copyright Copyright (c) Marcus Holland-Moritz
|
||||
*
|
||||
* This file is part of dwarfs.
|
||||
*
|
||||
* dwarfs is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* dwarfs is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
namespace dwarfs {
|
||||
|
||||
class logger;
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <class T>
|
||||
struct unique_ptr_policy {
|
||||
using return_type = std::unique_ptr<T>;
|
||||
|
||||
template <class U, class... Args>
|
||||
static return_type create(Args&&... args) {
|
||||
return std::make_unique<U>(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct shared_ptr_policy {
|
||||
using return_type = std::shared_ptr<T>;
|
||||
|
||||
template <class U, class... Args>
|
||||
static return_type create(Args&&... args) {
|
||||
return std::make_shared<U>(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
class logging_class_factory {
|
||||
public:
|
||||
template <template <class> class T, class CreatePolicy,
|
||||
class LoggerPolicyList, class... Args>
|
||||
static typename CreatePolicy::return_type
|
||||
create(logger& lgr, Args&&... args) {
|
||||
return create_unwrap<T, CreatePolicy>(
|
||||
lgr, std::type_identity<LoggerPolicyList>{},
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
private:
|
||||
template <template <class> class T, class CreatePolicy,
|
||||
class... LoggerPolicies, class... Args>
|
||||
static typename CreatePolicy::return_type
|
||||
create_unwrap(logger& lgr, std::type_identity<std::tuple<LoggerPolicies...>>,
|
||||
Args&&... args) {
|
||||
return create_impl<T, CreatePolicy, LoggerPolicies...>(
|
||||
lgr, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <template <class> class T, class CreatePolicy, class LoggerPolicy,
|
||||
class... Rest, class... Args>
|
||||
static typename CreatePolicy::return_type
|
||||
create_impl(logger& lgr, Args&&... args) {
|
||||
if (is_policy_name(lgr, LoggerPolicy::name())) {
|
||||
return CreatePolicy::template create<T<LoggerPolicy>>(
|
||||
lgr, std::forward<Args>(args)...);
|
||||
} else if constexpr (sizeof...(Rest) > 0) {
|
||||
return create_impl<T, CreatePolicy, Rest...>(lgr,
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
on_policy_not_found(lgr);
|
||||
}
|
||||
|
||||
static bool is_policy_name(logger const& lgr, std::string_view name);
|
||||
[[noreturn]] static void on_policy_not_found(logger const& lgr);
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace dwarfs
|
@ -39,7 +39,7 @@
|
||||
|
||||
#include <boost/chrono/thread_clock.hpp>
|
||||
|
||||
#include <dwarfs/error.h>
|
||||
#include <dwarfs/detail/logging_class_factory.h>
|
||||
#include <dwarfs/util.h>
|
||||
|
||||
namespace dwarfs {
|
||||
@ -65,26 +65,22 @@ class logger {
|
||||
virtual void write(level_type level, const std::string& output,
|
||||
char const* file, int line) = 0;
|
||||
|
||||
const std::string& policy_name() const { return policy_name_; }
|
||||
|
||||
template <class Policy>
|
||||
void set_policy() // TODO: construction time arg?
|
||||
{
|
||||
policy_name_ = Policy::name();
|
||||
}
|
||||
|
||||
void set_policy_name(const std::string& name) // TODO: construction time arg?
|
||||
{
|
||||
policy_name_ = name;
|
||||
}
|
||||
std::string_view policy_name() const { return policy_name_; }
|
||||
|
||||
static level_type parse_level(std::string_view level);
|
||||
static std::string_view level_name(level_type level);
|
||||
|
||||
static std::string all_level_names();
|
||||
|
||||
protected:
|
||||
template <class Policy>
|
||||
void set_policy() // TODO: construction time arg?
|
||||
{
|
||||
policy_name_ = Policy::name();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string policy_name_; // TODO: const?
|
||||
std::string_view policy_name_;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, logger::level_type const& optval);
|
||||
@ -395,82 +391,30 @@ class log_proxy {
|
||||
|
||||
class prod_logger_policy : public MinimumLogLevelPolicy<logger::VERBOSE> {
|
||||
public:
|
||||
static std::string name() { return "prod"; }
|
||||
static std::string_view name() { return "prod"; }
|
||||
};
|
||||
|
||||
class debug_logger_policy : public MinimumLogLevelPolicy<logger::TRACE> {
|
||||
public:
|
||||
static std::string name() { return "debug"; }
|
||||
static std::string_view name() { return "debug"; }
|
||||
};
|
||||
|
||||
using logger_policies = std::tuple<debug_logger_policy, prod_logger_policy>;
|
||||
|
||||
template <class T>
|
||||
struct unique_ptr_policy {
|
||||
using return_type = std::unique_ptr<T>;
|
||||
|
||||
template <class U, class... Args>
|
||||
static return_type create(Args&&... args) {
|
||||
return std::make_unique<U>(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct shared_ptr_policy {
|
||||
using return_type = std::shared_ptr<T>;
|
||||
|
||||
template <class U, class... Args>
|
||||
static return_type create(Args&&... args) {
|
||||
return std::make_shared<U>(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <template <class> class T, class CreatePolicy, class LoggerPolicyList,
|
||||
size_t N>
|
||||
struct logging_class_factory {
|
||||
template <class... Args>
|
||||
static typename CreatePolicy::return_type
|
||||
create(logger& lgr, Args&&... args) {
|
||||
if (std::tuple_element<N - 1, LoggerPolicyList>::type::name() ==
|
||||
lgr.policy_name()) {
|
||||
using obj_type =
|
||||
T<typename std::tuple_element<N - 1, LoggerPolicyList>::type>;
|
||||
return CreatePolicy::template create<obj_type>(
|
||||
lgr, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
return logging_class_factory<T, CreatePolicy, LoggerPolicyList,
|
||||
N - 1>::create(lgr,
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <template <class> class T, class CreatePolicy, class LoggerPolicyList>
|
||||
struct logging_class_factory<T, CreatePolicy, LoggerPolicyList, 0> {
|
||||
template <class... Args>
|
||||
static typename CreatePolicy::return_type create(logger& lgr, Args&&...) {
|
||||
DWARFS_THROW(runtime_error, "no such logger policy: " + lgr.policy_name());
|
||||
}
|
||||
};
|
||||
|
||||
template <class Base, template <class> class T, class LoggerPolicyList,
|
||||
class... Args>
|
||||
std::unique_ptr<Base> make_unique_logging_object(logger& lgr, Args&&... args) {
|
||||
return logging_class_factory<
|
||||
T, unique_ptr_policy<Base>, LoggerPolicyList,
|
||||
std::tuple_size<LoggerPolicyList>::value>::create(lgr,
|
||||
std::forward<Args>(
|
||||
args)...);
|
||||
return detail::logging_class_factory::create<
|
||||
T, detail::unique_ptr_policy<Base>, LoggerPolicyList>(
|
||||
lgr, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <class Base, template <class> class T, class LoggerPolicyList,
|
||||
class... Args>
|
||||
std::shared_ptr<Base> make_shared_logging_object(logger& lgr, Args&&... args) {
|
||||
return logging_class_factory<
|
||||
T, shared_ptr_policy<Base>, LoggerPolicyList,
|
||||
std::tuple_size<LoggerPolicyList>::value>::create(lgr,
|
||||
std::forward<Args>(
|
||||
args)...);
|
||||
return detail::logging_class_factory::create<
|
||||
T, detail::shared_ptr_policy<Base>, LoggerPolicyList>(
|
||||
lgr, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
std::string get_logger_context(char const* path, int line);
|
||||
|
@ -38,6 +38,7 @@
|
||||
#include <fmt/chrono.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <dwarfs/error.h>
|
||||
#include <dwarfs/logger.h>
|
||||
#include <dwarfs/string.h>
|
||||
#include <dwarfs/terminal_ansi.h>
|
||||
@ -291,6 +292,20 @@ void stream_logger::set_threshold(level_type threshold) {
|
||||
}
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
bool logging_class_factory::is_policy_name(logger const& lgr,
|
||||
std::string_view name) {
|
||||
return lgr.policy_name() == name;
|
||||
}
|
||||
|
||||
void logging_class_factory::on_policy_not_found(logger const& lgr) {
|
||||
DWARFS_THROW(runtime_error,
|
||||
fmt::format("no such logger policy: {}", lgr.policy_name()));
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
std::string get_logger_context(char const* path, int line) {
|
||||
return fmt::format("[{0}:{1}] ", basename(path), line);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user