DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
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
25namespace lutils
26{
27namespace program
28{
29
33template<typename UI, typename M, typename T, typename B>
35{
36 m_running = false;
38 if (m_handler != nullptr)
39 {
40 m_handler->join();
41 }
42 delete m_handler;
43}
44
48template<typename UI, typename M, typename T, typename B>
50 : m_handler(nullptr),
51 m_running(false),
52 m_have_messages(false)
53{
54 this->on();
55}
56
64template<typename UI, typename M, typename T, typename B>
65inline 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
76template<typename UI, typename M, typename T, typename B>
77inline void msghandler<UI, M, T, B>::set(t_str const & logfile)
78{
79 reopen(logfile);
80}
81
85template<typename UI, typename M, typename T, typename B>
87{
88 if (m_handler == nullptr)
89 {
90 t_lock l(m_loock);
91 if (m_handler == nullptr)
92 {
93 m_running = true;
95 }
96 }
97}
98
103template<typename UI, typename M, typename T, typename B>
105{
106 typename t_messages::value_type m;
107
108 while (m_running == true)
109 {
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
144template<typename UI, typename M, typename T, typename B>
149
150template<typename UI, typename M, typename T, typename B>
151inline 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 }
158}
159
165template<typename UI, typename M, typename T, typename B>
166template<typename TR >
167inline 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
179template<typename UI, typename M, typename T, typename B>
180template<typename TR >
181inline 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
194template<typename UI, typename M, typename T, typename B>
195inline 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
221template<typename UI, typename M, typename T, typename B>
223{
224 *(m_file) << std::endl << m << std::endl;
225}
226
232template<typename UI, typename M, typename T, typename B>
233inline 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
258template<typename UI, typename M, typename T, typename B>
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_ */
bool levelcheck(t_str const &level) const
void message(t_str const &messagein)
TR::default_post_ret_type post(t_str const &m, t_str const &l)
static msghandler & ref()
std::lock_guard< t_mut > t_lock
std::condition_variable m_condition
std::shared_ptr< t_str > t_str_ptr
void reopen(t_str const &)
void file(t_str const &)
void set(t_str const &logfile)
std::unique_ptr< t_file > t_file_ptr
std::vector< t_str > t_levels
void setlevel(t_str const &level, t_levels levels)
#define CONDITION_BOOL_WAIT_TRYFUN(cond, lock, var, tryfun)
Definition macro.hpp:110
#define CONDITION_STD_SET(cond, lock, var, val)
Definition macro.hpp:78
#define LUTILS_MESSAGE_LEVEL_ZERO