bux API Reference 1.9.0
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
ParaLog.cpp
Go to the documentation of this file.
1#include "ParaLog.h"
2
3namespace bux {
4
5//
6// Class Implementation
7//
9{
10 std::lock_guard _{*m_lock};
11 return create_proxy(i < m_nodePart->m_filteredNodes.size()?
12 m_nodePart->m_filteredNodes.at(i).second:
13 m_nodePart->m_elseNode);
14}
15
16auto C_ParaLog::C_NodeArrayProxy::create_proxy(C_NodePtr &holder) const -> C_NodeProxy
17{
18 if (!holder)
19 holder = std::make_unique<C_Node>();
20
21 return {*m_lock, *holder};
22}
23
25{
26 std::lock_guard _{*m_lock};
27 return create_proxy(m_nodePart->m_elseNode);
28}
29
30void C_ParaLog::C_LockedNode::create_from(const C_Node &node, const std::function<std::ostream*(I_ReenterableLog&)> &to_log)
31{
32 for (auto &i: node.m_loggers)
33 {
34 if (const auto ret = to_log(*i))
35 m_loggers.emplace_back(i.get(), ret);
36 }
37 for (auto &i: node.m_partitions)
38 {
39 auto &dstPart = m_partitions.emplace_back();
40 for (auto &j: i.m_filteredNodes)
41 {
42 auto &dstPair = dstPart.m_filteredNodes.emplace_back(std::piecewise_construct, std::forward_as_tuple(j.first), std::forward_as_tuple());
43 if (j.second)
44 dstPair.second.create_from(*j.second, to_log);
45 }
46
47 if (i.m_elseNode)
48 dstPart.m_elseNode.create_from(*i.m_elseNode, to_log);
49 if (dstPart.m_elseNode.empty())
50 while (dstPart.m_filteredNodes.back().second.empty())
51 dstPart.m_filteredNodes.pop_back();
52 if (dstPart.empty())
53 m_partitions.pop_back();
54 }
55}
56
57void C_ParaLog::C_LockedNode::log(std::string_view s, bool flush) const
58{
59 for (auto &i: m_loggers)
60 {
61 *i.second <<s;
62 i.first->unuseLog(flush);
63 }
64 for (auto &i: m_partitions)
65 {
66 bool logged{};
67 for (auto &j: i.m_filteredNodes)
68 {
69 if (j.first(s))
70 {
71 j.second.log(s, flush);
72 logged = true;
73 break;
74 }
75 }
76 if (!logged)
77 i.m_elseNode.log(s, flush);
78 }
79}
80
81std::ostream *C_ParaLog::lockLog(const std::function<std::ostream*(I_ReenterableLog&)> &to_log)
82{
83 m_lock.lock();
84 auto &dst = m_lockedStack.emplace_back();
85 dst.first.create_from(m_root, to_log);
86 if (!dst.first.empty())
87 return &dst.second;
88
89 m_lockedStack.pop_back();
90 m_lock.unlock();
91 return nullptr;
92}
93
94std::ostream *C_ParaLog::lockLog()
95{
96 return lockLog([](auto &child){ return child.useLog(); });
97}
98
100{
101 return lockLog([ll](auto &child){ return child.useLog(ll); });
102}
103
104void C_ParaLog::unlockLog(bool flush)
105{
106 const auto &back = m_lockedStack.back();
107 back.first.log(back.second.str(), flush);
108 m_lockedStack.pop_back();
109 m_lock.unlock();
110}
111
112} // namespace bux
C_NodeProxy operator[](size_t i) const
Definition ParaLog.cpp:8
C_NodeProxy matchedNone() const
Definition ParaLog.cpp:24
void unlockLog(bool flush) override
If the previous call to lockLog() returned null, the behavior is undefined.
Definition ParaLog.cpp:104
std::ostream * lockLog() override
Return non-null pointer if possible.
Definition ParaLog.cpp:94
THE common namespace of bux library.
Definition AtomiX.cpp:3
E_LogLevel
Definition LogLevel.h:9
Thread-unsafe implementation is preferred for performance.
Definition SyncLog.h:35