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_str > t_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 msghandler & ref ()

Private Types

typedef B t_bool
typedef T t_thread
typedef M t_mut
typedef std::lock_guard< t_mut > t_lock
typedef std::shared_ptr< t_str > t_str_ptr
typedef std::ofstream t_file
typedef std::unique_ptr< t_file > t_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
msghandler & operator= (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_thread * m_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 40 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 87 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 98 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 94 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 95 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 44 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 91 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 99 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 90 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 43 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 92 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 89 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 32 of file msghandler.hxx.

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

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

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

212{
213 *(m_file) << std::endl << m << std::endl;
214}

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

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

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

188{
189 if (m_levels.empty()) {
190 return true;
191 }
192
195
196 if (reqlevel == m_levels.end() || reqlevel < minlevel) {
197 return false;
198 }
199 return true;
200}

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

136{
138}
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 142 of file msghandler.hxx.

143{
144 {
147 }
149}

◆ 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) {
90 if (m_handler == nullptr) {
91 m_running = true;
93 }
94 }
95}

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

174{
175 TR::post(m, l);
176}

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

160{
161 std::cerr << "[" << l << "]:" << m << std::endl;
162}

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

248{
249 static msghandler<UI> This;
250 return This;
251}

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

224{
226
228
229 if (m_file != nullptr) {
230 while (m_file->is_open()) {
231 m_file->close();
232 }
233 }
235
236 while (!m_file->is_open()) {
237 m_file->open(logfile);
238 }
239}
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 76 of file msghandler.hxx.

77{
79}
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 63 of file msghandler.hxx.

64{
66 m_level = level;
68}

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 106 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 105 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 109 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 111 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 114 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 103 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 102 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 108 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 116 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 101 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 113 of file msghandler.hpp.


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