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 {
26namespace program {
27
31template<typename UI, typename M, typename T, typename B>
33{
34 m_running = false;
36 if (m_handler != nullptr) {
37 m_handler->join();
38 }
39 delete m_handler;
40}
41
45template<typename UI, typename M, typename T, typename B>
47 : m_handler(nullptr)
48 , m_running(false)
49 , m_have_messages(false)
50{
51 this->on();
52}
53
61template<typename UI, typename M, typename T, typename B>
62inline void
64{
65 t_lock l(m_loock);
66 m_level = level;
67 m_levels = levels;
68}
69
74template<typename UI, typename M, typename T, typename B>
75inline void
77{
78 reopen(logfile);
79}
80
84template<typename UI, typename M, typename T, typename B>
85inline void
87{
88 if (m_handler == nullptr) {
89 t_lock l(m_loock);
90 if (m_handler == nullptr) {
91 m_running = true;
93 }
94 }
95}
96
101template<typename UI, typename M, typename T, typename B>
102inline void
104{
105 typename t_messages::value_type m;
106
107 while (m_running == true) {
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
133template<typename UI, typename M, typename T, typename B>
134inline void
139
140template<typename UI, typename M, typename T, typename B>
141inline void
143{
144 {
145 t_lock l(m_loock);
146 m_messages.push(std::make_pair(level, in));
147 }
149}
150
156template<typename UI, typename M, typename T, typename B>
157template<typename TR>
158inline typename TR::default_post_ret_type
160{
161 std::cerr << "[" << l << "]:" << m << std::endl;
162}
163
170template<typename UI, typename M, typename T, typename B>
171template<typename TR>
172inline typename TR::post_ret_type
174{
175 TR::post(m, l);
176}
177
185template<typename UI, typename M, typename T, typename B>
186inline bool
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
209template<typename UI, typename M, typename T, typename B>
210inline void
212{
213 *(m_file) << std::endl << m << std::endl;
214}
215
221template<typename UI, typename M, typename T, typename B>
222inline void
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
245template<typename UI, typename M, typename T, typename B>
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_ */
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:106
#define CONDITION_STD_SET(cond, lock, var, val)
Definition macro.hpp:77
#define LUTILS_MESSAGE_LEVEL_ZERO