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 37 of file msghandler.hpp.

Member Typedef Documentation

◆ t_bool

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

Definition at line 84 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 94 of file msghandler.hpp.

◆ t_file

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

Definition at line 91 of file msghandler.hpp.

◆ t_file_ptr

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

Definition at line 92 of file msghandler.hpp.

◆ t_levels

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

Definition at line 41 of file msghandler.hpp.

◆ t_lock

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

Definition at line 88 of file msghandler.hpp.

◆ t_messages

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

Definition at line 95 of file msghandler.hpp.

◆ t_mut

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

Definition at line 87 of file msghandler.hpp.

◆ t_str

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

Definition at line 40 of file msghandler.hpp.

◆ t_str_ptr

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

Definition at line 89 of file msghandler.hpp.

◆ t_thread

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

Definition at line 86 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 29 of file msghandler.hxx.

30{
31 m_running = false;
33 if (m_handler != nullptr)
34 {
35 m_handler->join();
36 }
37 delete m_handler;
38}
std::condition_variable m_condition
#define CONDITION_STD_SET(cond, lock, var, val)
Definition macro.hpp:73

◆ 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 44 of file msghandler.hxx.

45 : m_handler(nullptr),
46 m_running(false),
47 m_have_messages(false)
48{
49 this->on();
50}

◆ 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 217 of file msghandler.hxx.

218{
219 *(m_file) << std::endl << m << std::endl;
220}

◆ 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 99 of file msghandler.hxx.

100{
101 typename t_messages::value_type m;
102
103 while (m_running == true)
104 {
106 {
107 m_running = false;
108 } );
109 {
111 while (!m_messages.empty())
112 {
113 m = m_messages.front();
114 m_messages.pop();
115 if (levelcheck(m.first))
116 {
117 if (m_logfile == nullptr)
118 {
119 post(m.second, m.first);
120 }
121 else
122 {
123 file(m.second);
124 }
125 }
126 }
127
128 m_have_messages = false;
129
130 }
131 }
132}
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:105

◆ 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 190 of file msghandler.hxx.

191{
192 if (m_levels.empty())
193 {
194 return true;
195 }
196
197 typename t_levels::const_iterator minlevel = std::find(m_levels.begin(), m_levels.end(),
198 m_level);
199 typename t_levels::const_iterator reqlevel = std::find(m_levels.begin(), m_levels.end(),
200 level);
201
202 if (reqlevel == m_levels.end() || reqlevel < minlevel)
203 {
204 return false;
205 }
206 return true;
207}

◆ 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 140 of file msghandler.hxx.

141{
143}
void message(t_str const &messagein)
#define LUTILS_MESSAGE_LEVEL_ZERO

◆ 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 146 of file msghandler.hxx.

147{
148 {
150 m_messages.push(std::make_pair(level, in));
151 }
153}

◆ 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 81 of file msghandler.hxx.

82{
83 if (m_handler == nullptr)
84 {
86 if (m_handler == nullptr)
87 {
88 m_running = true;
90 }
91 }
92}

◆ 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::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 162 of file msghandler.hxx.

164{
165 std::cerr << "[" << l << "]:"<< m << std::endl;
166}

◆ post() [2/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 176 of file msghandler.hxx.

178{
179 TR::post(m,l);
180}

◆ 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 254 of file msghandler.hxx.

255{
256 static msghandler<UI> This;
257 return This;
258}

◆ 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 228 of file msghandler.hxx.

229{
231
232 m_logfile = t_str_ptr(new t_str(logfile));
233
234 if (m_file != nullptr)
235 {
236 while (m_file->is_open())
237 {
238 m_file->close();
239 }
240 }
241 m_file = t_file_ptr(new std::ofstream);
242
243 while (!m_file->is_open())
244 {
245 m_file->open(logfile);
246 }
247}
std::unique_ptr< t_file > t_file_ptr
std::shared_ptr< t_str > t_str_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 72 of file msghandler.hxx.

73{
74 reopen(logfile);
75}
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 60 of file msghandler.hxx.

61{
63 m_level = level;
64 m_levels = levels;
65}

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 102 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 101 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 106 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 108 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 111 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 99 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 98 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 105 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 113 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 97 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 110 of file msghandler.hpp.


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