DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
dbe
include
dbe
detail
msghandler.hxx
Go to the documentation of this file.
1
// DUNE DAQ modification notice:
2
// This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3
// Fork baseline commit: dbe-02-12-17 (2022-05-12).
4
// Renamed since fork: yes (from dbe/msghandler.hpp to include/dbe/detail/msghandler.hxx).
5
6
/************************************************************
7
* msghandler.hpp
8
*
9
* Created on: Dec 3, 2014
10
* Author: Leonidas Georgopoulos
11
*
12
* Copyright (C) 2014 Leonidas Georgopoulos
13
* Distributed under the Boost Software License, Version 1.0.
14
* (See accompanying file LICENSE_1_0.txt or
15
* copy at http://www.boost.org/LICENSE_1_0.txt)
16
*
17
************************************************************/
18
#ifndef LUTILS_MSGHANDLER_HPP_
19
#define LUTILS_MSGHANDLER_HPP_
20
21
#include <algorithm>
22
23
#define LUTILS_MESSAGE_LEVEL_ZERO "lutils_nolevel"
24
25
namespace
lutils
26
{
27
namespace
program
28
{
29
33
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
34
inline
msghandler<UI, M, T, B>::~msghandler
()
35
{
36
m_running
=
false
;
37
CONDITION_STD_SET
(
m_condition
,
m_cond_lock
,
m_have_messages
,
true
);
38
if
(
m_handler
!=
nullptr
)
39
{
40
m_handler
->join();
41
}
42
delete
m_handler
;
43
}
44
48
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
49
inline
msghandler<UI, M, T, B>::msghandler
()
50
:
m_handler
(nullptr),
51
m_running
(false),
52
m_have_messages
(false)
53
{
54
this->
on
();
55
}
56
64
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
65
inline
void
msghandler<UI, M, T, B>::setlevel
(
t_str
const
& level,
t_levels
levels)
66
{
67
t_lock
l(
m_loock
);
68
m_level
= level;
69
m_levels
= levels;
70
}
71
76
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
77
inline
void
msghandler<UI, M, T, B>::set
(
t_str
const
& logfile)
78
{
79
reopen
(logfile);
80
}
81
85
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
86
inline
void
msghandler<UI, M, T, B>::on
()
87
{
88
if
(
m_handler
==
nullptr
)
89
{
90
t_lock
l(
m_loock
);
91
if
(
m_handler
==
nullptr
)
92
{
93
m_running
=
true
;
94
m_handler
=
new
t_thread
(&
msghandler<UI>::handle
,
this
);
95
}
96
}
97
}
98
103
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
104
inline
void
msghandler<UI, M, T, B>::handle
()
105
{
106
typename
t_messages::value_type m;
107
108
while
(
m_running
==
true
)
109
{
110
CONDITION_BOOL_WAIT_TRYFUN
(
m_condition
,
m_cond_lock
,
m_have_messages
, [&
m_running
]()
111
{
112
m_running
=
false
;
113
} );
114
{
115
t_lock
l(
m_loock
);
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
}
138
144
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
145
inline
void
msghandler<UI, M, T, B>::message
(
t_str
const
& in)
146
{
147
message
(
LUTILS_MESSAGE_LEVEL_ZERO
, in);
148
}
149
150
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
151
inline
void
msghandler<UI, M, T, B>::message
(
t_str
const
& level,
t_str
const
& in)
152
{
153
{
154
t_lock
l(
m_loock
);
155
m_messages
.push(std::make_pair(level, in));
156
}
157
CONDITION_STD_SET
(
m_condition
,
m_cond_lock
,
m_have_messages
,
true
);
158
}
159
165
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
166
template
<
typename
TR >
167
inline
typename
TR::default_post_ret_type
msghandler<UI, M, T, B>::post
(
168
t_str
const
& m,
t_str
const
& l)
169
{
170
std::cerr <<
"["
<< l <<
"]:"
<< m << std::endl;
171
}
172
179
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
180
template
<
typename
TR >
181
inline
typename
TR::post_ret_type
msghandler<UI, M, T, B>::post
(
182
t_str
const
& m,
t_str
const
& l)
183
{
184
TR::post(m,l);
185
}
186
194
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
195
inline
bool
msghandler<UI, M, T, B>::levelcheck
(
t_str
const
& level)
const
196
{
197
if
(
m_levels
.empty())
198
{
199
return
true
;
200
}
201
202
typename
t_levels::const_iterator minlevel = std::find(
m_levels
.begin(),
m_levels
.end(),
203
m_level
);
204
typename
t_levels::const_iterator reqlevel = std::find(
m_levels
.begin(),
m_levels
.end(),
205
level);
206
207
if
(reqlevel ==
m_levels
.end() || reqlevel < minlevel)
208
{
209
return
false
;
210
}
211
return
true
;
212
}
213
221
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
222
inline
void
msghandler<UI, M, T, B>::file
(
t_str
const
& m)
223
{
224
*(
m_file
) << std::endl << m << std::endl;
225
}
226
232
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
233
inline
void
msghandler<UI, M, T, B>::reopen
(
t_str
const
& logfile)
234
{
235
t_lock
l(
m_loock
);
236
237
m_logfile
=
t_str_ptr
(
new
t_str
(logfile));
238
239
if
(
m_file
!=
nullptr
)
240
{
241
while
(
m_file
->is_open())
242
{
243
m_file
->close();
244
}
245
}
246
m_file
=
t_file_ptr
(
new
std::ofstream);
247
248
while
(!
m_file
->is_open())
249
{
250
m_file
->open(logfile);
251
}
252
}
253
258
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
259
inline
msghandler<UI, M, T, B>
&
msghandler<UI, M, T, B>::ref
()
260
{
261
static
msghandler<UI>
This;
262
return
This;
263
}
264
265
}
/* namespace program */
266
}
/* namespace lutils */
267
268
269
// Facility macro to inform at compile time msg handler that this class provides a post function
270
#define HAS_POST typedef void post_ret_type;
271
#define DEFAULT_POST typedef void default_post_ret_type;
272
273
#endif
/* LUTILS_MSGHANDLER_HPP_ */
lutils::program::msghandler::levelcheck
bool levelcheck(t_str const &level) const
Definition
msghandler.hxx:195
lutils::program::msghandler::m_loock
t_mut m_loock
Definition
msghandler.hpp:118
lutils::program::msghandler::message
void message(t_str const &messagein)
Definition
msghandler.hxx:145
lutils::program::msghandler::handle
void handle()
Definition
msghandler.hxx:104
lutils::program::msghandler::~msghandler
~msghandler()
Definition
msghandler.hxx:34
lutils::program::msghandler::post
TR::default_post_ret_type post(t_str const &m, t_str const &l)
Definition
msghandler.hxx:167
lutils::program::msghandler::m_logfile
t_str_ptr m_logfile
Definition
msghandler.hpp:110
lutils::program::msghandler::msghandler
msghandler()
Definition
msghandler.hxx:49
lutils::program::msghandler::m_cond_lock
std::mutex m_cond_lock
Definition
msghandler.hpp:107
lutils::program::msghandler::ref
static msghandler & ref()
Definition
msghandler.hxx:259
lutils::program::msghandler::m_have_messages
t_bool m_have_messages
Definition
msghandler.hpp:116
lutils::program::msghandler::on
void on()
Definition
msghandler.hxx:86
lutils::program::msghandler::t_lock
std::lock_guard< t_mut > t_lock
Definition
msghandler.hpp:93
lutils::program::msghandler::t_str
UI::t_str t_str
Definition
msghandler.hpp:45
lutils::program::msghandler::m_condition
std::condition_variable m_condition
Definition
msghandler.hpp:106
lutils::program::msghandler::t_str_ptr
std::shared_ptr< t_str > t_str_ptr
Definition
msghandler.hpp:94
lutils::program::msghandler::reopen
void reopen(t_str const &)
Definition
msghandler.hxx:233
lutils::program::msghandler::t_thread
T t_thread
Definition
msghandler.hpp:91
lutils::program::msghandler::m_handler
t_thread * m_handler
Definition
msghandler.hpp:113
lutils::program::msghandler::file
void file(t_str const &)
Definition
msghandler.hxx:222
lutils::program::msghandler::m_running
t_bool m_running
Definition
msghandler.hpp:115
lutils::program::msghandler::m_level
t_str m_level
Definition
msghandler.hpp:104
lutils::program::msghandler::m_file
t_file_ptr m_file
Definition
msghandler.hpp:111
lutils::program::msghandler::m_messages
t_messages m_messages
Definition
msghandler.hpp:102
lutils::program::msghandler::set
void set(t_str const &logfile)
Definition
msghandler.hxx:77
lutils::program::msghandler::t_file_ptr
std::unique_ptr< t_file > t_file_ptr
Definition
msghandler.hpp:97
lutils::program::msghandler::m_levels
t_levels m_levels
Definition
msghandler.hpp:103
lutils::program::msghandler::t_levels
std::vector< t_str > t_levels
Definition
msghandler.hpp:46
lutils::program::msghandler::setlevel
void setlevel(t_str const &level, t_levels levels)
Definition
msghandler.hxx:65
CONDITION_BOOL_WAIT_TRYFUN
#define CONDITION_BOOL_WAIT_TRYFUN(cond, lock, var, tryfun)
Definition
macro.hpp:110
CONDITION_STD_SET
#define CONDITION_STD_SET(cond, lock, var, val)
Definition
macro.hpp:78
LUTILS_MESSAGE_LEVEL_ZERO
#define LUTILS_MESSAGE_LEVEL_ZERO
Definition
msghandler.hxx:23
lutils::program
Definition
msghandler.hxx:28
lutils
Definition
msghandler.hxx:26
Generated on
for DUNE-DAQ by
1.17.0