GNU Radio Manual and C++ API Reference  3.10.1.0
The Free & Open Software Radio Ecosystem
thread_body_wrapper.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_THREAD_BODY_WRAPPER_H
12 #define INCLUDED_THREAD_BODY_WRAPPER_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/logger.h>
16 #include <gnuradio/thread/thread.h>
17 #include <exception>
18 
19 namespace gr {
20 namespace thread {
21 
23 
24 template <class F>
26 {
27 private:
28  F d_f;
29  std::string d_name;
30  bool d_catch_exceptions;
31  gr::logger_ptr d_logger;
32  gr::logger_ptr d_debug_logger;
33 
34 public:
35  explicit thread_body_wrapper(F f,
36  const std::string& name = "",
37  bool catch_exceptions = true)
38  : d_f(f), d_name(name), d_catch_exceptions(catch_exceptions)
39  {
40  gr::configure_default_loggers(d_logger, d_debug_logger, "thread_body_wrapper");
41  }
42 
43  void operator()()
44  {
45  mask_signals();
46 
47  if (d_catch_exceptions) {
48  try {
49  d_f();
50  } catch (boost::thread_interrupted const&) {
51  } catch (std::exception const& e) {
52  std::ostringstream msg;
53  msg << "ERROR thread[" << d_name << "]: " << e.what();
54  GR_LOG_ERROR(d_logger, msg.str());
55  } catch (...) {
56  std::ostringstream msg;
57  msg << "ERROR thread[" << d_name << "]: caught unrecognized exception";
58  GR_LOG_ERROR(d_logger, msg.str());
59  }
60 
61  } else {
62  try {
63  d_f();
64  } catch (boost::thread_interrupted const&) {
65  }
66  }
67  }
68 };
69 
70 } /* namespace thread */
71 } /* namespace gr */
72 
73 #endif /* INCLUDED_THREAD_BODY_WRAPPER_H */
Definition: thread_body_wrapper.h:26
thread_body_wrapper(F f, const std::string &name="", bool catch_exceptions=true)
Definition: thread_body_wrapper.h:35
void operator()()
Definition: thread_body_wrapper.h:43
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
#define GR_LOG_ERROR(log, msg)
Definition: logger.h:250
GR_RUNTIME_API const pmt::pmt_t msg()
boost::thread thread
Definition: thread.h:36
GR_RUNTIME_API void mask_signals()
GNU Radio logging wrapper.
Definition: basic_block.h:29
std::shared_ptr< logger > logger_ptr
Definition: logger.h:207
GR_RUNTIME_API bool configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, const std::string &name)