Line data Source code
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: no.
5 :
6 : /*
7 : * messenger.cpp
8 : *
9 : * Created on: Nov 3, 2015
10 : * Author: Leonidas Georgopoulos
11 : */
12 :
13 : #include "logging/Logging.hpp"
14 :
15 : #include "dbe/messenger.hpp"
16 : #include "dbe/messenger_proxy.hpp"
17 :
18 : #include <cassert>
19 : #include <map>
20 :
21 : namespace dbe {
22 : namespace interface {
23 : namespace messenger {
24 :
25 : //------------------------------------------------------------------------------------------
26 : msglevels::t_str const msglevels::debug = "DEBUG";
27 : msglevels::t_str const msglevels::info = "INFO";
28 : msglevels::t_str const msglevels::note = "NOTE";
29 : msglevels::t_str const msglevels::warn = "WARN";
30 : msglevels::t_str const msglevels::error = "ERROR";
31 : msglevels::t_str const msglevels::fail = "FAIL";
32 :
33 : msglevels::t_levels const msglevels::all_levels = { debug, info, note, warn, error, fail };
34 : // provides codes from message level type ( level -> code )
35 : static std::map<qt::t_str const, messages const> codes = {
36 : { msglevels::info, messages::DEBUG }, { msglevels::info, messages::INFO }, { msglevels::note, messages::NOTE },
37 : { msglevels::warn, messages::WARN }, { msglevels::error, messages::ERROR }, { msglevels::fail, messages::FAIL }
38 : };
39 :
40 : // provides level types from code ( code->level)
41 : static std::map<messages const, qt::t_str const> levels = {
42 : { messages::DEBUG, msglevels::debug }, { messages::INFO, msglevels::info }, { messages::NOTE, msglevels::note },
43 : { messages::WARN, msglevels::warn }, { messages::ERROR, msglevels::error }, { messages::FAIL, msglevels::fail }
44 : };
45 :
46 : // provides titles from level types
47 : static std::map<qt::t_str const, qt::t_str const> titles = {
48 : { msglevels::debug, "Debug info" }, { msglevels::info, "Information" }, { msglevels::note, "Notice" },
49 : { msglevels::warn, "Warning" }, { msglevels::error, "Error" }, { msglevels::fail, "Failure" }
50 : };
51 :
52 : std::mutex qt::m_block;
53 : std::atomic<bool> qt::m_batch_mode(false);
54 : qt::t_batches qt::m_batches;
55 : //------------------------------------------------------------------------------------------
56 :
57 : //------------------------------------------------------------------------------------------
58 : qt::post_ret_type
59 0 : qt::post(t_str const& m, t_str const& l)
60 : {
61 0 : std::lock_guard<std::mutex> lock(m_block);
62 :
63 0 : if (m_batch_mode) {
64 0 : merge_post(m, l);
65 : } else {
66 0 : direct_post(m, l);
67 : }
68 0 : }
69 : //------------------------------------------------------------------------------------------
70 :
71 : //------------------------------------------------------------------------------------------
72 : qt::post_ret_type
73 0 : qt::direct_post(t_str const& m, t_str const& l)
74 : {
75 0 : if (warn == l) {
76 0 : TLOG_DEBUG(0) << static_cast<int>(messages::WARN) << m;
77 0 : messenger_proxy::ref().warn(titles[warn], m);
78 0 : } else if (error == l) {
79 0 : TLOG_DEBUG(0) << static_cast<int>(messages::ERROR) << m;
80 0 : messenger_proxy::ref().error(titles[error], m);
81 0 : } else if (info == l) {
82 0 : TLOG_DEBUG(0) << static_cast<int>(messages::INFO) << m;
83 0 : messenger_proxy::ref().info(titles[info], m);
84 0 : } else if (fail == l) {
85 0 : TLOG_DEBUG(0) << static_cast<int>(messages::FAIL) << m;
86 0 : messenger_proxy::ref().fail(titles[fail], m);
87 0 : } else if (note == l) {
88 0 : TLOG_DEBUG(0) << static_cast<int>(messages::NOTE) << m;
89 0 : messenger_proxy::ref().fail(titles[note], m);
90 0 : } else if (debug == l) {
91 0 : TLOG_DEBUG(0) << static_cast<int>(messages::DEBUG) << m;
92 0 : messenger_proxy::ref().fail(titles[debug], m);
93 : }
94 0 : }
95 : //------------------------------------------------------------------------------------------
96 :
97 : //------------------------------------------------------------------------------------------
98 : qt::post_ret_type
99 0 : qt::merge_post(t_str const& m, t_str const& l)
100 : {
101 0 : auto codeit = codes.find(l);
102 0 : assert(codeit != codes.end());
103 0 : m_batches[static_cast<int>(codeit->second)].insert(m);
104 0 : }
105 : //------------------------------------------------------------------------------------------
106 :
107 : //------------------------------------------------------------------------------------------
108 : void
109 0 : qt::purge()
110 : {
111 0 : for (auto clevel = m_batches.begin(); clevel != m_batches.end(); ++clevel) {
112 0 : if (not clevel->empty()) {
113 0 : t_str merged("Summary of messages during batch mode");
114 :
115 0 : for (auto it = clevel->begin(); it != clevel->end();) {
116 0 : auto cnt = clevel->count(*it);
117 :
118 0 : t_str cmessage = *it;
119 0 : merged += t_str("\n\n") + cmessage + t_str("\n\n #Occurances=") + std::to_string(cnt);
120 :
121 0 : std::advance(it, cnt);
122 0 : }
123 :
124 0 : direct_post(merged, levels[static_cast<messages>(clevel - m_batches.begin())]);
125 0 : clevel->clear();
126 0 : }
127 : }
128 0 : }
129 : //------------------------------------------------------------------------------------------
130 :
131 : //------------------------------------------------------------------------------------------
132 : std::unique_ptr<batch_guard>
133 0 : qt::batchmode()
134 : {
135 0 : return std::unique_ptr<batch_guard>(new batch_guard());
136 : }
137 : //------------------------------------------------------------------------------------------
138 :
139 : //------------------------------------------------------------------------------------------
140 0 : batch_guard::~batch_guard()
141 : {
142 0 : qt::m_batch_mode = false;
143 0 : qt::purge();
144 0 : }
145 : //------------------------------------------------------------------------------------------
146 :
147 : //------------------------------------------------------------------------------------------
148 0 : batch_guard::batch_guard()
149 : {
150 0 : qt::m_batch_mode = true;
151 0 : }
152 : //------------------------------------------------------------------------------------------
153 :
154 : } // namespace messenger
155 : } /* namespace interface */
156 : } /* namespace dbe */
|