tdaq-develop-2025-02-12
MessageTools.h
1 /*===============================================================================
2  * Monicelli: the FERMILAB MTEST geometry builder and track reconstruction tool
3  *
4  * Copyright (C) 2014
5  *
6  * Authors:
7  *
8  * Dario Menasce (INFN)
9  * Luigi Moroni (INFN)
10  * Jennifer Ngadiuba (INFN)
11  * Stefano Terzo (INFN)
12  * Lorenzo Uplegger (FNAL)
13  * Luigi Vigani (INFN)
14  *
15  * INFN: Piazza della Scienza 3, Edificio U2, Milano, Italy 20126
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  ================================================================================*/
30 
31 #ifndef MESSAGETOOLS_H
32 #define MESSAGETOOLS_H
33 
34 #include <algorithm>
35 #include <iostream>
36 #include <sstream>
37 
38 #include "ANSIColors.h"
39 
40 #define __MODULE__ __PRETTY_FUNCTION__ // Options: __FUNCTION__ | __PRETTY_FUNCTION___
41 #define __OFFSET__ 50 // Options: __FUNCTION__ | __PRETTY_FUNCTION___
42 
43 #define FATAL(MSG, COLOR) \
44  { \
45  std::stringstream msg_; \
46  std::stringstream PFs_; \
47  std::string PF_ = __MODULE__; \
48  int PFSize = PF_.size(); \
49  int maxL = 90; \
50  if(PFSize > __OFFSET__) \
51  { \
52  PFSize = __OFFSET__; \
53  } \
54  for(int i = 0; i < PFSize; ++i) \
55  { \
56  PFs_ << PF_[i]; \
57  } \
58  if(PFSize < __OFFSET__) \
59  { \
60  for(int i = 0; i < __OFFSET__ + 3 - PFSize; ++i) \
61  { \
62  PFs_ << " "; \
63  } \
64  } \
65  if(PFSize < (int)PF_.size()) \
66  { \
67  maxL -= 3; \
68  PFs_ << ACRed << "..."; \
69  } \
70  msg_ << ACCyan << ACBold << __LINE__ << ACPlain << ACYellow << "\t] [" << ACRed \
71  << ACBold << ACReverse << "FATAL:" << ACPlain << ACWhite << ACBold << " " \
72  << PFs_.str() << ACPlain << ACYellow << "]"; \
73  int blankSize = maxL - msg_.str().size() + 1; \
74  if(blankSize < 0) \
75  blankSize = 2; \
76  std::string blanks = ""; \
77  for(int i = 0; i < blankSize; ++i) \
78  { \
79  blanks += " "; \
80  } \
81  std::cout << msg_.str() << blanks << COLOR << ACBold << MSG << ACPlain \
82  << std::endl; \
83  }
84 
85 #define STDLINE(MSG, COLOR) \
86  { \
87  std::stringstream msg_; \
88  std::stringstream PFs_; \
89  std::string PF_ = __MODULE__; \
90  int PFSize = PF_.size(); \
91  int maxL = 90; \
92  if(PFSize > __OFFSET__) \
93  { \
94  PFSize = __OFFSET__; \
95  } \
96  for(int i = 0; i < PFSize; ++i) \
97  { \
98  PFs_ << PF_[i]; \
99  } \
100  if(PFSize < __OFFSET__) \
101  { \
102  for(int i = 0; i < __OFFSET__ + 3 - PFSize; ++i) \
103  { \
104  PFs_ << " "; \
105  } \
106  } \
107  if(PFSize < (int)PF_.size()) \
108  { \
109  maxL -= 4; \
110  PFs_ << ACRed << "..."; \
111  } \
112  else \
113  { \
114  PFs_ << ACPlain << ACPlain << ACPlain << ACPlain; \
115  } \
116  msg_ << ACCyan << ACBold << __LINE__ << ACPlain << ACYellow << "\t] [" \
117  << ACPlain << ACWhite << ACBold << PFs_.str() << ACPlain << ACYellow \
118  << "]"; \
119  int msgS = msg_.str().size() + 1; \
120  if(msgS < __OFFSET__) \
121  msgS = __OFFSET__; \
122  int blankSize = maxL - msgS; \
123  if(blankSize < 0) \
124  blankSize = 3; \
125  std::string blanks = ""; \
126  for(int i = 0; i < blankSize; ++i) \
127  { \
128  blanks += " "; \
129  } \
130  std::cout << msg_.str() << blanks << COLOR << ACBold << MSG << ACPlain \
131  << std::endl; \
132  }
133 
134 #define STDSNAP(MSG, COLOR) \
135  { \
136  std::stringstream msg_; \
137  std::stringstream PFs_; \
138  std::string PF_ = __MODULE__; \
139  int PFSize = PF_.size(); \
140  int maxL = 90; \
141  if(PFSize > __OFFSET__) \
142  { \
143  PFSize = __OFFSET__; \
144  } \
145  for(int i = 0; i < PFSize; ++i) \
146  { \
147  PFs_ << PF_[i]; \
148  } \
149  if(PFSize <= __OFFSET__) \
150  { \
151  for(int i = 0; i < __OFFSET__ + 3 - PFSize; ++i) \
152  { \
153  PFs_ << " "; \
154  } \
155  } \
156  if(PFSize < (int)PF_.size()) \
157  { \
158  maxL -= 4; \
159  PFs_ << ACRed << "..."; \
160  } \
161  msg_ << ACCR << ACCyan << ACBold << __LINE__ << ACPlain << ACYellow << "\t] [" \
162  << ACPlain << ACWhite << ACBold << PFs_.str() << ACPlain << ACYellow \
163  << "]"; \
164  int blankSize = maxL - msg_.str().size() + 1; \
165  if(blankSize < 0) \
166  blankSize = 3; \
167  std::string blanks = ""; \
168  for(int i = 0; i < blankSize; ++i) \
169  { \
170  blanks += " "; \
171  } \
172  std::cout << msg_.str() << blanks << COLOR << ACBold << MSG << ACPlain \
173  << std::flush; \
174  }
175 
176 class Utils
177 {
178  public:
179  Utils() { ; }
180 
181  inline static int toInt(std::string inputString)
182  {
183  std::istringstream iss_;
184  iss_.str(inputString);
185  int n;
186  iss_ >> n;
187 
188  return n;
189  }
190  //=============================================================================
191  inline static double toDouble(std::string inputString)
192  {
193  std::istringstream iss_;
194  iss_.str(inputString);
195  double n;
196  iss_ >> n;
197 
198  return n;
199  }
200  //=============================================================================
201  inline static int toDec(unsigned long long inputBinary)
202  {
203  std::stringstream ss;
204  ss.str("");
205  int n = inputBinary;
206  ss << std::dec << n;
207  n = Utils::toInt(ss.str());
208 
209  return n;
210  }
211 };
212 
213 #endif // MESSAGETOOLS_H