The Sparta Modeling Framework
Loading...
Searching...
No Matches
MessageSource.hpp
1// <MessageSource> -*- C++ -*-
2
3#pragma once
4
5#include <cstdint>
6#include <iostream>
7#include <string>
8#include <sstream>
9#include <utility>
10
12#include "sparta/log/NotificationSource.hpp"
13#include "sparta/log/Message.hpp"
14#include "sparta/log/Tap.hpp"
15#include "sparta/log/Events.hpp"
16#include "sparta/log/categories/CategoryManager.hpp"
17#include "sparta/utils/StringManager.hpp"
18#include "sparta/utils/Utils.hpp"
19#include "sparta/log/MessageInfo.hpp"
20
21namespace sparta
22{
27 namespace log
28 {
34 class MessageSource : public NotificationSource<sparta::log::Message>
35 {
36 public:
37
39 static const constexpr char* GROUP_NAME_MSG_SOURCE = "_sparta_log_msg_source_";
40
41 public:
42
43 MessageSource(const MessageSource&) = delete;
44 MessageSource& operator=(const MessageSource&) = delete;
45 MessageSource& operator=(const MessageSource&&) = delete;
46
47 MessageSource(TreeNode* parent,
48 const std::string& category,
49 const std::string& desc) :
50 MessageSource(parent, StringManager::getStringManager().internString(category), desc)
51 { }
52
53 // MessageSource with category_id interned in StringManager
54 MessageSource(TreeNode* parent,
55 const std::string* category_id,
56 const std::string& desc) :
57 NotificationSource(parent,
60 desc,
61 category_id)
62 { }
63
68 virtual ~MessageSource() {
69 }
70
71 uint64_t getNumEmitted() const {
72 return getNumPosts();
73 }
74
75 const std::string* getCategoryID() const {
76 return getNotificationID();
77 }
78
79 const std::string& getCategoryName() const {
80 return getNotificationName();
81 }
82
83 operator bool() const noexcept {
84 return observed();
85 }
86
99
106
113
114
118
132 {
137 const sparta::log::MessageSource* src_;
138
142 std::ostringstream s_;
143
144 public:
145
147 LogObject() = delete;
148
151 src_(rhp.src_),
152 s_(rhp.s_.str()) // May unfortunately involve a copy
153 { }
154
156 LogObject(const LogObject& rhp) = delete;
157
162 src_(&src)
163 { }
164
168 template <class T>
169 LogObject(const MessageSource& src, const T& init) :
170 src_(&src)
171 {
172 s_ << init;
173 }
174
179 LogObject(const MessageSource& src, std::ostream& (*f)(std::ostream&)) :
180 src_(&src)
181 {
182 f(s_);
183 }
184
192 if(src_){
193 src_->emit_(s_.str());
194 }
195 }
196
200 void cancel() {
201 src_ = nullptr;
202 }
203
210 template <class T>
211 LogObject& operator<<(const T& t) {
212 s_ << t;
213 return *this;
214 }
215
219 LogObject& operator<<(std::ostream& (*f)(std::ostream&)) {
220 f(s_);
221 return *this;
222 }
223 };
224
225 template <class T>
226 LogObject operator<<(const T& t) const {
227 return LogObject(*this, t);
228 }
229
230 LogObject operator<<(std::ostream& (*f)(std::ostream&)) const {
231 return LogObject(*this, f);
232 }
233
234 LogObject emit(const std::string& msg) const {
235 return LogObject(*this, msg);
236 }
237
238 LogObject emit(std::string&& msg) const {
239 return LogObject(*this, msg);
240 }
241
244
245
246 // Override from TreeNode
247 virtual std::string stringize(bool pretty=false) const override {
248 (void) pretty;
249 std::stringstream ss;
250 ss << '<' << getParent()->getLocation() << ":log_msg_src cat:\""
251 << getCategoryName() << "\" observed:" << std::boolalpha << observed()
252 << " msgs:" << getNumEmitted() << '>';
253 return ss.str();
254 }
255
256 private:
257
265 void emit_(const std::string& content) const;
266
268 static seq_num_type seq_num_;
269 };
270
271 } // namespace log
272} // namespace sparta
273
Basic Node framework in sparta device tree composite pattern.
const std::string & getNotificationName() const
Returns the notification name string for slow string comparison or printing.
bool observed() const noexcept
Is this NotificationSourceBase being observed at this node or an ancestor of any distance.
uint64_t getNumPosts() const
Returns the number of notifications posted by this node.
const std::string * getNotificationID() const
Returns notification ID (string pointer from Notification ID interned in sparta::StringManager)
A TreeNode that generates a specific type of notification which propagates up a tree of TreeNodes (us...
Manages string internment for SPARTA. This allows strings to be compared by pointer once interned.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
std::string getLocation() const override final
group_idx_type getGroupIndexMax(const std::string &group)
Gets the largest index of a node in the given group.
virtual TreeNode * getParent()
Gets immediate parent of this node if one exists.
Definition TreeNode.hpp:965
Temporary object for constructing a log message with a ostream-like interface. Emits a message to the...
LogObject(LogObject &&rhp)
Move constructor.
LogObject(const MessageSource &src)
Construct with message source.
LogObject()=delete
Not default-constructable.
LogObject & operator<<(std::ostream &(*f)(std::ostream &))
Handler for stream modifiers (e.g. endl)
LogObject(const MessageSource &src, const T &init)
Construct with an initial value.
LogObject(const MessageSource &src, std::ostream &(*f)(std::ostream &))
Construct with a function operating on ostreams (e.g. std::setw)
LogObject(const LogObject &rhp)=delete
Not Copy-constructable.
LogObject & operator<<(const T &t)
Insertion operator on this LogObject.
void cancel()
Cancel the LogObject by.
Message source object associated with a sparta TreeNode through which messages can be sent.
static MessageSource & getGlobalDebug()
Gets the global warning logger. These are global-level debugging messages from the SPARTA framework.
static MessageSource & getGlobalWarn()
Gets the global warning logger. These messages can be picked up by placing a Tap on the sparta::TreeN...
static const constexpr char * GROUP_NAME_MSG_SOURCE
Group name of logging message sources.
virtual std::string stringize(bool pretty=false) const override
Create a string representation of this node.
static MessageSource & getGlobalParameterTraceSource()
Gets the global parameters/configuration logger. These are global-level configuration messages from t...
int64_t seq_num_type
Sequence number of a message within a thread ID. Signed so that initial state can be -1.
Macros for handling exponential backoff.
T * notNull(T *p)
Ensures that a pointer is not null.
Definition Utils.hpp:224