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
namespace
program
{
27
31
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
32
inline
msghandler<UI, M, T, B>::~msghandler
()
33
{
34
m_running
=
false
;
35
CONDITION_STD_SET
(
m_condition
,
m_cond_lock
,
m_have_messages
,
true
);
36
if
(
m_handler
!=
nullptr
) {
37
m_handler
->join();
38
}
39
delete
m_handler
;
40
}
41
45
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
46
inline
msghandler<UI, M, T, B>::msghandler
()
47
:
m_handler
(nullptr)
48
,
m_running
(false)
49
,
m_have_messages
(false)
50
{
51
this->
on
();
52
}
53
61
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
62
inline
void
63
msghandler<UI, M, T, B>::setlevel
(
t_str
const
& level,
t_levels
levels)
64
{
65
t_lock
l(
m_loock
);
66
m_level
= level;
67
m_levels
= levels;
68
}
69
74
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
75
inline
void
76
msghandler<UI, M, T, B>::set
(
t_str
const
& logfile)
77
{
78
reopen
(logfile);
79
}
80
84
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
85
inline
void
86
msghandler<UI, M, T, B>::on
()
87
{
88
if
(
m_handler
==
nullptr
) {
89
t_lock
l(
m_loock
);
90
if
(
m_handler
==
nullptr
) {
91
m_running
=
true
;
92
m_handler
=
new
t_thread
(&
msghandler<UI>::handle
,
this
);
93
}
94
}
95
}
96
101
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
102
inline
void
103
msghandler<UI, M, T, B>::handle
()
104
{
105
typename
t_messages::value_type m;
106
107
while
(
m_running
==
true
) {
108
CONDITION_BOOL_WAIT_TRYFUN
(
m_condition
,
m_cond_lock
,
m_have_messages
, [&
m_running
]() {
m_running
=
false
; });
109
{
110
t_lock
l(
m_loock
);
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
}
127
133
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
134
inline
void
135
msghandler<UI, M, T, B>::message
(
t_str
const
& in)
136
{
137
message
(
LUTILS_MESSAGE_LEVEL_ZERO
, in);
138
}
139
140
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
141
inline
void
142
msghandler<UI, M, T, B>::message
(
t_str
const
& level,
t_str
const
& in)
143
{
144
{
145
t_lock
l(
m_loock
);
146
m_messages
.push(std::make_pair(level, in));
147
}
148
CONDITION_STD_SET
(
m_condition
,
m_cond_lock
,
m_have_messages
,
true
);
149
}
150
156
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
157
template
<
typename
TR>
158
inline
typename
TR::default_post_ret_type
159
msghandler<UI, M, T, B>::post
(
t_str
const
& m,
t_str
const
& l)
160
{
161
std::cerr <<
"["
<< l <<
"]:"
<< m << std::endl;
162
}
163
170
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
171
template
<
typename
TR>
172
inline
typename
TR::post_ret_type
173
msghandler<UI, M, T, B>::post
(
t_str
const
& m,
t_str
const
& l)
174
{
175
TR::post(m, l);
176
}
177
185
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
186
inline
bool
187
msghandler<UI, M, T, B>::levelcheck
(
t_str
const
& level)
const
188
{
189
if
(
m_levels
.empty()) {
190
return
true
;
191
}
192
193
typename
t_levels::const_iterator minlevel = std::find(
m_levels
.begin(),
m_levels
.end(),
m_level
);
194
typename
t_levels::const_iterator reqlevel = std::find(
m_levels
.begin(),
m_levels
.end(), level);
195
196
if
(reqlevel ==
m_levels
.end() || reqlevel < minlevel) {
197
return
false
;
198
}
199
return
true
;
200
}
201
209
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
210
inline
void
211
msghandler<UI, M, T, B>::file
(
t_str
const
& m)
212
{
213
*(
m_file
) << std::endl << m << std::endl;
214
}
215
221
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
222
inline
void
223
msghandler<UI, M, T, B>::reopen
(
t_str
const
& logfile)
224
{
225
t_lock
l(
m_loock
);
226
227
m_logfile
=
t_str_ptr
(
new
t_str
(logfile));
228
229
if
(
m_file
!=
nullptr
) {
230
while
(
m_file
->is_open()) {
231
m_file
->close();
232
}
233
}
234
m_file
=
t_file_ptr
(
new
std::ofstream);
235
236
while
(!
m_file
->is_open()) {
237
m_file
->open(logfile);
238
}
239
}
240
245
template
<
typename
UI,
typename
M,
typename
T,
typename
B>
246
inline
msghandler<UI, M, T, B>
&
247
msghandler<UI, M, T, B>::ref
()
248
{
249
static
msghandler<UI>
This;
250
return
This;
251
}
252
253
}
/* namespace program */
254
}
/* namespace lutils */
255
256
// Facility macro to inform at compile time msg handler that this class provides a post function
257
#define HAS_POST typedef void post_ret_type;
258
#define DEFAULT_POST typedef void default_post_ret_type;
259
260
#endif
/* LUTILS_MSGHANDLER_HPP_ */
lutils::program::msghandler
Definition
msghandler.hpp:41
lutils::program::msghandler::levelcheck
bool levelcheck(t_str const &level) const
Definition
msghandler.hxx:187
lutils::program::msghandler::m_loock
t_mut m_loock
Definition
msghandler.hpp:116
lutils::program::msghandler::message
void message(t_str const &messagein)
Definition
msghandler.hxx:135
lutils::program::msghandler::handle
void handle()
Definition
msghandler.hxx:103
lutils::program::msghandler::~msghandler
~msghandler()
Definition
msghandler.hxx:32
lutils::program::msghandler::post
TR::default_post_ret_type post(t_str const &m, t_str const &l)
Definition
msghandler.hxx:159
lutils::program::msghandler::m_logfile
t_str_ptr m_logfile
Definition
msghandler.hpp:108
lutils::program::msghandler::msghandler
msghandler()
Definition
msghandler.hxx:46
lutils::program::msghandler::m_cond_lock
std::mutex m_cond_lock
Definition
msghandler.hpp:106
lutils::program::msghandler::ref
static msghandler & ref()
Definition
msghandler.hxx:247
lutils::program::msghandler::m_have_messages
t_bool m_have_messages
Definition
msghandler.hpp:114
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:91
lutils::program::msghandler::t_str
UI::t_str t_str
Definition
msghandler.hpp:43
lutils::program::msghandler::m_condition
std::condition_variable m_condition
Definition
msghandler.hpp:105
lutils::program::msghandler::t_str_ptr
std::shared_ptr< t_str > t_str_ptr
Definition
msghandler.hpp:92
lutils::program::msghandler::reopen
void reopen(t_str const &)
Definition
msghandler.hxx:223
lutils::program::msghandler::t_thread
T t_thread
Definition
msghandler.hpp:89
lutils::program::msghandler::m_handler
t_thread * m_handler
Definition
msghandler.hpp:111
lutils::program::msghandler::file
void file(t_str const &)
Definition
msghandler.hxx:211
lutils::program::msghandler::m_running
t_bool m_running
Definition
msghandler.hpp:113
lutils::program::msghandler::m_level
t_str m_level
Definition
msghandler.hpp:103
lutils::program::msghandler::m_file
t_file_ptr m_file
Definition
msghandler.hpp:109
lutils::program::msghandler::m_messages
t_messages m_messages
Definition
msghandler.hpp:101
lutils::program::msghandler::set
void set(t_str const &logfile)
Definition
msghandler.hxx:76
lutils::program::msghandler::t_file_ptr
std::unique_ptr< t_file > t_file_ptr
Definition
msghandler.hpp:95
lutils::program::msghandler::m_levels
t_levels m_levels
Definition
msghandler.hpp:102
lutils::program::msghandler::t_levels
std::vector< t_str > t_levels
Definition
msghandler.hpp:44
lutils::program::msghandler::setlevel
void setlevel(t_str const &level, t_levels levels)
Definition
msghandler.hxx:63
CONDITION_BOOL_WAIT_TRYFUN
#define CONDITION_BOOL_WAIT_TRYFUN(cond, lock, var, tryfun)
Definition
macro.hpp:106
CONDITION_STD_SET
#define CONDITION_STD_SET(cond, lock, var, val)
Definition
macro.hpp:77
LUTILS_MESSAGE_LEVEL_ZERO
#define LUTILS_MESSAGE_LEVEL_ZERO
Definition
msghandler.hxx:23
lutils::program
Definition
msghandler.hxx:26
lutils
Definition
msghandler.hxx:25
Generated on
for DUNE-DAQ by
1.18.0