libzypp: zypp/ng/log/format.h Source File

libzypp 17.38.7

format.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------/
9*
10* This file contains private API, this might break at any time between releases.
11* You have been warned!
12*
13*/
14#ifndef ZYPP_NG_LOG_FORMAT_H_INCLUDED
15#define ZYPP_NG_LOG_FORMAT_H_INCLUDED
16
17#include <ostream>
18#include <sstream>
19#include <type_traits>
20
21#include <zypp-core/ng/base/zyppglobal.h>
22#include <zypp-core/ng/meta/type_traits.h>
23
24
25namespace zyppng{
26
27 ZYPP_FWD_DECL_TYPE_WITH_REFS(Context);
28
29 namespace log {
30
66 template <typename T, typename Enable = void>
67 struct formatter;
68
69 namespace detail {
70
72 template <typename T>
73 struct view_proxy {
74 const T& obj;
75
76 operator std::string() const {
77 std::ostringstream ss;
78 ss << *this;
79 return ss.str();
80 }
81
82 friend std::ostream& operator<<(std::ostream& os, const view_proxy& v) {
83 return formatter<T>::stream(os, v.obj);
84 }
85 };
86
88 template <typename T>
89 struct ctx_bound_view_proxy {
90 const ContextWeakRef ctx;
91 const T& obj;
92
93 operator std::string() const {
94 std::ostringstream ss;
95 ss << *this;
96 return ss.str();
97 }
98
99 friend std::ostream& operator<<(std::ostream& os, const ctx_bound_view_proxy& v) {
100 auto ctxLock = v.ctx.lock();
101 if ( ctxLock )
102 return formatter<T>::stream(os, ctxLock, v.obj);
103 else {
104 return os << "<<Error: Context Required for streaming " << typeid(T).name() << ">>";
105 }
106 }
107 };
108
110 template <typename T, typename = void>
111 struct has_view_proxy : std::false_type {};
112
113 template <typename T>
114 struct has_view_proxy<T, std::void_t<decltype(formatter<T>::stream(std::declval<std::ostream&>(), std::declval<const T&>()))>>
115 : std::true_type {};
116 }
117
119 template <typename T>
120 auto view(const ContextRef& ctx, const T& obj) {
121 return detail::ctx_bound_view_proxy<T>{ ctx, obj };
122 }
123
125 template <typename T >
126 auto view(const T& obj) {
127 return detail::view_proxy<T>{ obj };
128 }
129 }
130
136 template <typename T>
137 auto operator<<(std::ostream& os, const T& obj)
138 -> std::enable_if_t<log::detail::has_view_proxy<T>::value, std::ostream&>
139 {
140 return os << log::view<T>(obj);
141 }
142}
143
144#endif
zyppng::Context
Definition context.h:27
std
Definition ansi.h:855
std::enable_if_t
typename enable_if< B, T >::type enable_if_t
Definition TypeTraits.h:45
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
zyppng::log::detail
Definition format.h:69
zyppng::log
Definition format.h:29
zyppng::log::view
auto view(const ContextRef &ctx, const T &obj)
Unified format call: Context-Aware.
Definition format.h:120
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::log::detail::ctx_bound_view_proxy
Temporary object for streaming, we prefer direct streaming so we have string allocations only when ne...
Definition format.h:89
zyppng::log::detail::ctx_bound_view_proxy::operator<<
friend std::ostream & operator<<(std::ostream &os, const ctx_bound_view_proxy &v)
Definition format.h:99
zyppng::log::detail::ctx_bound_view_proxy::ctx
const ContextWeakRef ctx
Definition format.h:90
zyppng::log::detail::ctx_bound_view_proxy::obj
const T & obj
Definition format.h:91
zyppng::log::detail::has_view_proxy
Detection trait for StaticFormatter implementation.
Definition format.h:111
zyppng::log::detail::view_proxy
Temporary object for streaming, we prefer direct streaming so we have string allocations only when ne...
Definition format.h:73
zyppng::log::detail::view_proxy::obj
const T & obj
Definition format.h:74
zyppng::log::detail::view_proxy::operator<<
friend std::ostream & operator<<(std::ostream &os, const view_proxy &v)
Definition format.h:82
zyppng::log::formatter
Primary trait for object formatting.
Definition format.h:67
type_traits.h
zyppglobal.h
ZYPP_FWD_DECL_TYPE_WITH_REFS
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:119