UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
ilisten.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
5
17#pragma once
18
19#include <map>
20#include <memory>
21#include <sstream>
22#include <string>
23#include <vector>
24#include <atomic>
25
26namespace util::log {
27
28class ListenStream;
29
39
46class IListen {
47 public:
48 virtual ~IListen() = default;
49
56 [[nodiscard]] const std::string &Name() const { return name_; }
57
63 void Name(const std::string &name) { name_ = name; }
64
71 [[nodiscard]] const std::string &Description() const { return description_; }
72
78 void Description(const std::string &description) {
79 description_ = description;
80 }
81
88 [[nodiscard]] const std::string &PreText() const { return pre_text_; }
89
97 void PreText(const std::string &pre_text) { pre_text_ = pre_text; }
98
103 [[nodiscard]] const std::string &HostName() const { return host_name_; }
104
113 void HostName(const std::string &host_name) { host_name_ = host_name; }
114
120 [[nodiscard]] uint16_t Port() const { return port_; }
121
127 void Port(uint16_t port) { port_ = port; }
128
136 void SetLogLevelText(uint64_t level, const std::string &menu_text);
137
143 [[nodiscard]] const std::map<uint64_t, std::string> &LogLevelList() const;
144
151 void ListenString(const std::string &text);
152
154
162 void ListenText(const char *format_text, ...);
163
172 void ListenTextEx(uint64_t ns1970, const std::string &pre_text,
173 const char *format_text, ...);
174
184 virtual void ListenTransmit(uint64_t ns1970, const std::string &pre_text,
185 const std::vector<uint8_t> &buffer, void *hint);
186
196 virtual void ListenReceive(uint64_t ns1970, const std::string &pre_text,
197 const std::vector<uint8_t> &buffer, void *hint);
198
199 virtual void SetActive(
200 bool active);
201
207 [[nodiscard]] virtual bool IsActive() const = 0;
208
209 virtual void SetLogLevel(size_t log_level);
210
216 [[nodiscard]] virtual size_t LogLevel() = 0;
217
223 virtual bool Start();
224
230 virtual bool Stop();
231
238 [[nodiscard]] virtual size_t NofConnections() const;
239
246 uint64_t GetNumberOfMessages() const { return number_of_messages_; }
247
253 void ResetNumberOfMessages() { number_of_messages_ = 0; }
254
255 protected:
256 std::string share_name_;
257 std::string name_;
258 std::string description_;
259 std::string pre_text_;
260 std::string host_name_ = "127.0.0.1";
261 uint16_t port_ = 0;
262 std::map<uint64_t, std::string> log_level_list_;
263
264
265 IListen() = default;
266
275 virtual void AddMessage(uint64_t nano_sec_1970, const std::string &pre_text,
276 const std::string &text) = 0;
277
283 static std::string ParseHex(const std::vector<uint8_t> &buffer);
284
290 void IncrementNumberOfMessages() { ++number_of_messages_; }
291
292 private:
293 std::atomic<uint64_t> number_of_messages_ = 0;
294};
295
300class ListenStream final : public std::ostringstream {
301 public:
302 ListenStream() = delete;
303
304 explicit ListenStream(IListen &listen) : listen_(listen) {}
305
306 ~ListenStream() override {
307 if (listen_.IsActive()) {
308 listen_.ListenString(str());
309 }
310 }
311
312 private:
313 IListen &listen_;
314};
315} // namespace util::log
An interface class that hides the actual implementation of the object.
Definition ilisten.h:46
virtual size_t LogLevel()=0
Returns the current log level.
void ListenTextEx(uint64_t ns1970, const std::string &pre_text, const char *format_text,...)
Generate a user defined text line.
virtual bool Start()
Starts the listen object.
virtual void SetLogLevel(size_t log_level)
Sets the log level.
virtual void ListenReceive(uint64_t ns1970, const std::string &pre_text, const std::vector< uint8_t > &buffer, void *hint)
Generates a hex string text from a byte buffer.
void PreText(const std::string &pre_text)
Sets the default pre-text that starts each line.
Definition ilisten.h:97
void ResetNumberOfMessages()
Definition ilisten.h:253
void Description(const std::string &description)
Sets the description for the listen object.
Definition ilisten.h:78
const std::map< uint64_t, std::string > & LogLevelList() const
Returns all log levels and their menu texts.
static std::string ParseHex(const std::vector< uint8_t > &buffer)
Parses a byte buffer into a hexadecimal string.
void Name(const std::string &name)
Sets the display name.
Definition ilisten.h:63
ListenStream ListenOut()
Internal debug function (unit tests)
virtual bool Stop()
Stops the listen object.
void IncrementNumberOfMessages()
Definition ilisten.h:290
void ListenString(const std::string &text)
Generates a listen text line.
virtual size_t NofConnections() const
Number of client connects.
std::string share_name_
Share memory name.
Definition ilisten.h:256
const std::string & HostName() const
Host name for the object.
Definition ilisten.h:103
const std::string & PreText() const
Text that always starts a text line.
Definition ilisten.h:88
uint16_t Port() const
TCP/IP port.
Definition ilisten.h:120
virtual ~IListen()=default
Default destructor.
const std::string & Name() const
Display name.
Definition ilisten.h:56
IListen()=default
Default constructor.
void SetLogLevelText(uint64_t level, const std::string &menu_text)
Sets the log level menu texts.
std::string host_name_
Host name.
Definition ilisten.h:260
std::map< uint64_t, std::string > log_level_list_
Log level index and.
Definition ilisten.h:262
void HostName(const std::string &host_name)
Sets the host name for the listen object.
Definition ilisten.h:113
std::string description_
Description of the functionality.
Definition ilisten.h:258
virtual void ListenTransmit(uint64_t ns1970, const std::string &pre_text, const std::vector< uint8_t > &buffer, void *hint)
Generates a hex string text from a byte buffer.
const std::string & Description() const
Description of the listen object.
Definition ilisten.h:71
std::string name_
Display name.
Definition ilisten.h:257
std::string pre_text_
Small text between time and text string.
Definition ilisten.h:259
virtual void SetActive(bool active)
Activate or deactivate the listen object.
void ListenText(const char *format_text,...)
Generates a listen text line.
uint16_t port_
IP-port to listen on.
Definition ilisten.h:261
virtual void AddMessage(uint64_t nano_sec_1970, const std::string &pre_text, const std::string &text)=0
Adds a listen message.
uint64_t GetNumberOfMessages() const
Returns number of generated messages.
Definition ilisten.h:246
void Port(uint16_t port)
Sets the TCP/IP port.
Definition ilisten.h:127
virtual bool IsActive() const =0
Indicate if any listen window is open.
Support stream class when log messages to the listen functionality.
Definition ilisten.h:300
~ListenStream() override
Definition ilisten.h:306
ListenStream(IListen &listen)
Constructor.
Definition ilisten.h:304
The log namespace is used for log related classes and functions.
Definition idirectory.h:18
TypeOfListen
Defines the type of listen objects.
Definition ilisten.h:34
@ ListenProxyType
Forward the listen messages in shared memory.
Definition ilisten.h:35
@ ListenServerType
TCP/IP server that forwards the listen messages.
Definition ilisten.h:36
@ ListenConsoleType
Forward the listen messages to a console window.
Definition ilisten.h:37