DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
lutils::program::msghandler< UI, M, T, B > Class Template Reference

#include <msghandler.hpp>

Public Types

typedef UI::t_str t_str
typedef std::vector< t_strt_levels

Public Member Functions

void set (t_str const &logfile)
void setlevel (t_str const &level, t_levels levels)
void message (t_str const &messagein)
void message (t_str const &msglevel, t_str const &messagein)

Static Public Member Functions

static msghandlerref ()

Private Types

typedef B t_bool
typedef T t_thread
typedef M t_mut
typedef std::lock_guard< t_mutt_lock
typedef std::shared_ptr< t_strt_str_ptr
typedef std::ofstream t_file
typedef std::unique_ptr< t_filet_file_ptr
template<typename E>
using t_container = std::queue<E>
typedef t_container< std::pair< t_str, t_str > > t_messages

Private Member Functions

void on ()
void handle ()
template<typename TR = UI>
TR::default_post_ret_type post (t_str const &m, t_str const &l)
template<typename TR = UI>
TR::post_ret_type post (t_str const &m, t_str const &l)
void file (t_str const &)
bool levelcheck (t_str const &level) const
void reopen (t_str const &)
 ~msghandler ()
 msghandler ()
 msghandler (msghandler const &)=delete
msghandleroperator= (msghandler const &)=delete

Private Attributes

t_messages m_messages
t_levels m_levels
t_str m_level
std::condition_variable m_condition
std::mutex m_cond_lock
t_str_ptr m_logfile
t_file_ptr m_file
t_threadm_handler
t_bool m_running
t_bool m_have_messages
t_mut m_loock

Detailed Description

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
class lutils::program::msghandler< UI, M, T, B >

Class providing message handling capabilities to multiple sources

Definition at line 42 of file msghandler.hpp.

Member Typedef Documentation

◆ t_bool

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef B lutils::program::msghandler< UI, M, T, B >::t_bool
private

Definition at line 89 of file msghandler.hpp.

◆ t_container

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
template<typename E>
using lutils::program::msghandler< UI, M, T, B >::t_container = std::queue<E>
private

Definition at line 99 of file msghandler.hpp.

◆ t_file

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef std::ofstream lutils::program::msghandler< UI, M, T, B >::t_file
private

Definition at line 96 of file msghandler.hpp.

◆ t_file_ptr

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef std::unique_ptr<t_file> lutils::program::msghandler< UI, M, T, B >::t_file_ptr
private

Definition at line 97 of file msghandler.hpp.

◆ t_levels

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef std::vector<t_str> lutils::program::msghandler< UI, M, T, B >::t_levels

Definition at line 46 of file msghandler.hpp.

◆ t_lock

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef std::lock_guard<t_mut> lutils::program::msghandler< UI, M, T, B >::t_lock
private

Definition at line 93 of file msghandler.hpp.

◆ t_messages

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef t_container<std::pair<t_str, t_str> > lutils::program::msghandler< UI, M, T, B >::t_messages
private

Definition at line 100 of file msghandler.hpp.

◆ t_mut

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef M lutils::program::msghandler< UI, M, T, B >::t_mut
private

Definition at line 92 of file msghandler.hpp.

◆ t_str

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef UI::t_str lutils::program::msghandler< UI, M, T, B >::t_str

Definition at line 45 of file msghandler.hpp.

◆ t_str_ptr

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef std::shared_ptr<t_str> lutils::program::msghandler< UI, M, T, B >::t_str_ptr
private

Definition at line 94 of file msghandler.hpp.

◆ t_thread

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
typedef T lutils::program::msghandler< UI, M, T, B >::t_thread
private

Definition at line 91 of file msghandler.hpp.

Constructor & Destructor Documentation

◆ ~msghandler()

template<typename UI, typename M, typename T, typename B>
lutils::program::msghandler< UI, M, T, B >::~msghandler ( )
inlineprivate

Destruction waits to write all messages to file

Definition at line 34 of file msghandler.hxx.

35{
36 m_running = false;
38 if (m_handler != nullptr)
39 {
40 m_handler->join();
41 }
42 delete m_handler;
43}
std::condition_variable m_condition
#define CONDITION_STD_SET(cond, lock, var, val)
Definition macro.hpp:78

◆ msghandler() [1/2]

template<typename UI, typename M, typename T, typename B>
lutils::program::msghandler< UI, M, T, B >::msghandler ( )
inlineprivate

Constructs the object

Definition at line 49 of file msghandler.hxx.

50 : m_handler(nullptr),
51 m_running(false),
52 m_have_messages(false)
53{
54 this->on();
55}

◆ msghandler() [2/2]

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
lutils::program::msghandler< UI, M, T, B >::msghandler ( msghandler< UI, M, T, B > const & )
privatedelete

Member Function Documentation

◆ file()

template<typename UI, typename M, typename T, typename B>
void lutils::program::msghandler< UI, M, T, B >::file ( t_str const & m)
inlineprivate

Send message to file

Parameters
thefilename to write to

Default write to file function

This should not be specialized

Parameters
m

Definition at line 222 of file msghandler.hxx.

◆ handle()

template<typename UI, typename M, typename T, typename B>
void lutils::program::msghandler< UI, M, T, B >::handle ( )
inlineprivate

Handles incoming messages

Checks the queue and redirects messages either to a defined output or to a file if a file has been set

Definition at line 104 of file msghandler.hxx.

105{
106 typename t_messages::value_type m;
107
108 while (m_running == true)
109 {
111 {
112 m_running = false;
113 } );
114 {
116 while (!m_messages.empty())
117 {
118 m = m_messages.front();
119 m_messages.pop();
120 if (levelcheck(m.first))
121 {
122 if (m_logfile == nullptr)
123 {
124 post(m.second, m.first);
125 }
126 else
127 {
128 file(m.second);
129 }
130 }
131 }
132
133 m_have_messages = false;
134
135 }
136 }
137}
bool levelcheck(t_str const &level) const
TR::default_post_ret_type post(t_str const &m, t_str const &l)
std::lock_guard< t_mut > t_lock
void file(t_str const &)
#define CONDITION_BOOL_WAIT_TRYFUN(cond, lock, var, tryfun)
Definition macro.hpp:110

◆ levelcheck()

template<typename UI, typename M, typename T, typename B>
bool lutils::program::msghandler< UI, M, T, B >::levelcheck ( t_str const & level) const
inlineprivate

Checks the level of a message and return true if the level of the message is appropriate to be displayed

Parameters
levelof a message
Returns
true or false

Definition at line 195 of file msghandler.hxx.

196{
197 if (m_levels.empty())
198 {
199 return true;
200 }
201
203 m_level);
205 level);
206
207 if (reqlevel == m_levels.end() || reqlevel < minlevel)
208 {
209 return false;
210 }
211 return true;
212}

◆ message() [1/2]

template<typename UI, typename M, typename T, typename B>
void lutils::program::msghandler< UI, M, T, B >::message ( t_str const & in)
inline

Add a message to the queue to be served

Parameters
astring as a message

Adds the message in the internal container which will be picked up later

Parameters
messageinis the message to be displayed

Definition at line 145 of file msghandler.hxx.

146{
148}
void message(t_str const &messagein)

◆ message() [2/2]

template<typename UI, typename M, typename T, typename B>
void lutils::program::msghandler< UI, M, T, B >::message ( t_str const & msglevel,
t_str const & messagein )
inline

Add a message to the queue to be served

Parameters
messageinis the message content to be displayed
msglevelis a tag representing the level of the message

Definition at line 151 of file msghandler.hxx.

152{
153 {
156 }
158}

◆ on()

template<typename UI, typename M, typename T, typename B>
void lutils::program::msghandler< UI, M, T, B >::on ( )
inlineprivate

Sets the object in ready to process state

Properly starts the message handler

Definition at line 86 of file msghandler.hxx.

87{
88 if (m_handler == nullptr)
89 {
91 if (m_handler == nullptr)
92 {
93 m_running = true;
95 }
96 }
97}

◆ operator=()

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
msghandler & lutils::program::msghandler< UI, M, T, B >::operator= ( msghandler< UI, M, T, B > const & )
privatedelete

◆ post() [1/2]

template<typename UI, typename M, typename T, typename B>
template<typename TR>
TR::post_ret_type lutils::program::msghandler< UI, M, T, B >::post ( t_str const & m,
t_str const & l )
inlineprivate

Post action for users classes that define post_ret_type. These have to also define the post function

Parameters
mthe message to display

Definition at line 181 of file msghandler.hxx.

183{
184 TR::post(m,l);
185}

◆ post() [2/2]

template<typename UI, typename M, typename T, typename B>
template<typename TR>
TR::default_post_ret_type lutils::program::msghandler< UI, M, T, B >::post ( t_str const & m,
t_str const & l )
inlineprivate

Sends message to standard output or the provided UI::post method

Parameters
mis the message to send

Default post action for users classes that define default_post_ret_type

Parameters
mthe message to display

Definition at line 167 of file msghandler.hxx.

169{
170 std::cerr << "[" << l << "]:"<< m << std::endl;
171}

◆ ref()

template<typename UI, typename M, typename T, typename B>
msghandler< UI, M, T, B > & lutils::program::msghandler< UI, M, T, B >::ref ( )
inlinestatic

Creates or retrieves the msghandler

Returns
a reference to a singleton msghandler

Creates the message handler

Returns
a reference to the created singleton

Definition at line 259 of file msghandler.hxx.

260{
261 static msghandler<UI> This;
262 return This;
263}

◆ reopen()

template<typename UI, typename M, typename T, typename B>
void lutils::program::msghandler< UI, M, T, B >::reopen ( t_str const & logfile)
inlineprivate

Opens a designated file

Parameters
thefilename to open

Closes the current logfile and opens a new one

Parameters
logfile

Definition at line 233 of file msghandler.hxx.

234{
236
238
239 if (m_file != nullptr)
240 {
241 while (m_file->is_open())
242 {
243 m_file->close();
244 }
245 }
247
248 while (!m_file->is_open())
249 {
250 m_file->open(logfile);
251 }
252}
std::shared_ptr< t_str > t_str_ptr
std::unique_ptr< t_file > t_file_ptr

◆ set()

template<typename UI, typename M, typename T, typename B>
void lutils::program::msghandler< UI, M, T, B >::set ( t_str const & logfile)
inline

Sets the logfile. All remaining messages will be appended to that file

Parameters
logfile
Returns
a reference to a singleton msghandler

Properly sets the logfile

Parameters
logfileto write to

Definition at line 77 of file msghandler.hxx.

78{
80}
void reopen(t_str const &)

◆ setlevel()

template<typename UI, typename M, typename T, typename B>
void lutils::program::msghandler< UI, M, T, B >::setlevel ( t_str const & level,
t_levels levels )
inline

Sets the minimum level of messages that the message handler will consider for handling. The rest will be discarded

Parameters
levelis a string that defines the minimum level
levelsis a vector of strings in order of imporance

Sets the minimum level of messages that the message handler will consider for handling. The rest will be discarded

Parameters
levelis a string that defines the minimum level
levelsis a vector of strings in order of importance (least important -> most )

Definition at line 65 of file msghandler.hxx.

66{
68 m_level = level;
70}

Member Data Documentation

◆ m_cond_lock

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
std::mutex lutils::program::msghandler< UI, M, T, B >::m_cond_lock
private

Definition at line 107 of file msghandler.hpp.

◆ m_condition

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
std::condition_variable lutils::program::msghandler< UI, M, T, B >::m_condition
private

Definition at line 106 of file msghandler.hpp.

◆ m_file

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
t_file_ptr lutils::program::msghandler< UI, M, T, B >::m_file
private

Definition at line 111 of file msghandler.hpp.

◆ m_handler

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
t_thread* lutils::program::msghandler< UI, M, T, B >::m_handler
private

Definition at line 113 of file msghandler.hpp.

◆ m_have_messages

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
t_bool lutils::program::msghandler< UI, M, T, B >::m_have_messages
private

Definition at line 116 of file msghandler.hpp.

◆ m_level

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
t_str lutils::program::msghandler< UI, M, T, B >::m_level
private

Definition at line 104 of file msghandler.hpp.

◆ m_levels

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
t_levels lutils::program::msghandler< UI, M, T, B >::m_levels
private

Definition at line 103 of file msghandler.hpp.

◆ m_logfile

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
t_str_ptr lutils::program::msghandler< UI, M, T, B >::m_logfile
private

Definition at line 110 of file msghandler.hpp.

◆ m_loock

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
t_mut lutils::program::msghandler< UI, M, T, B >::m_loock
private

Definition at line 118 of file msghandler.hpp.

◆ m_messages

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
t_messages lutils::program::msghandler< UI, M, T, B >::m_messages
private

Definition at line 102 of file msghandler.hpp.

◆ m_running

template<typename UI, typename M = std::mutex, typename T = std::thread, typename B = bool>
t_bool lutils::program::msghandler< UI, M, T, B >::m_running
private

Definition at line 115 of file msghandler.hpp.


The documentation for this class was generated from the following files: