Line data Source code
1 : /**
2 : * @file exception_example.cxx logger interface definition
3 : *
4 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : // See https://atlas-tdaq-monitoring.web.cern.ch/OH/refman/ERSHowTo.html
10 :
11 : const char* usage = R"foo(
12 : Usage: %s <num>
13 : where <num> is a number: 0 - 6.
14 : )foo"
15 : "\n";
16 : #define USAGE usage, basename(*argv)
17 :
18 : #include <ers/SampleIssues.hpp>
19 : #include <libgen.h> // basename
20 : #include <logging/Logging.hpp>
21 : #include <string>
22 :
23 : class XX
24 : {
25 : public:
26 0 : ~XX() { TLOG_DEBUG(1) << "dtor object after raise/throw"; }
27 : };
28 :
29 : void
30 0 : foo(int except_int)
31 : {
32 0 : XX xx;
33 0 : switch (except_int) {
34 0 : case 0: {
35 0 : TLOG_DEBUG(1) << "raising ers::PermissionDenied " << "somefilename";
36 0 : ers::PermissionDenied permdenied(ERS_HERE, "somefilename", 0644);
37 0 : permdenied.raise();
38 0 : } break;
39 0 : case 1: {
40 0 : TLOG_DEBUG(1) << "raising ers::FileDoesNotExist " << "somenonfile";
41 0 : ers::FileDoesNotExist doesnotexist(ERS_HERE, "somenonfilename");
42 0 : doesnotexist.raise();
43 0 : } break;
44 0 : case 2: {
45 0 : TLOG_DEBUG(1) << "raising ers::CantOpenFile " << "somelockedfile";
46 0 : ers::CantOpenFile(ERS_HERE, "somelockedfile").raise();
47 : } break;
48 0 : case 3:
49 0 : TLOG_DEBUG(1) << "throwing std::exception";
50 0 : throw std::exception();
51 0 : break;
52 0 : case 4:
53 0 : TLOG_DEBUG(1) << "throwing integer 4";
54 0 : throw 4;
55 0 : break;
56 0 : case 5: {
57 0 : TLOG_DEBUG(1) << "write to address 0 - cause segv";
58 0 : int* ptr = nullptr;
59 0 : *ptr = 0;
60 0 : } break;
61 0 : default:
62 0 : TLOG_DEBUG(1) << "not throwing anything";
63 : }
64 0 : }
65 :
66 : int
67 0 : main(int argc, char* argv[])
68 : {
69 0 : if (argc != 2) {
70 0 : printf(USAGE);
71 0 : exit(1);
72 : }
73 :
74 0 : dunedaq::logging::Logging::setup("test", "exception_example");
75 :
76 0 : TLOG_DEBUG(1) << "trying foo";
77 0 : try {
78 0 : foo(strtoul(argv[1], nullptr, 0));
79 0 : } catch (ers::PermissionDenied& ex) {
80 0 : ers::CantOpenFile issue(ERS_HERE, ex.get_file_name(), ex);
81 0 : ers::warning(issue);
82 0 : } catch (ers::FileDoesNotExist& ex) {
83 0 : ers::CantOpenFile issue(ERS_HERE, ex.get_file_name(), ex);
84 0 : ers::warning(issue);
85 0 : } catch (ers::Issue& ex) {
86 0 : TLOG_DEBUG(0) << "Unknown issue caught: " << ex;
87 0 : ers::error(ex);
88 0 : } catch (std::exception& ex) {
89 0 : ers::CantOpenFile issue(ERS_HERE, "unknown", ex);
90 0 : ers::warning(issue);
91 0 : }
92 : #if 0
93 : catch (...) {
94 : // ers::fatal( ers::Message(ERS_HERE,"unhandle exceptions would not make it to the the TRACE memory buffer") );
95 : // ErrorHandler::abort(...) does StandardStreamOutput::println(std::cerr, issue, 13); ::abort();
96 : }
97 : #endif
98 :
99 0 : return (0);
100 : } // main
|