otsdaq  v2_05_02_indev
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 << ACBold << ACReverse << "FATAL:" << ACPlain << ACWhite << ACBold \
71  << " " << PFs_.str() << ACPlain << ACYellow << "]"; \
72  int blankSize = maxL - msg_.str().size() + 1; \
73  if(blankSize < 0) \
74  blankSize = 2; \
75  std::string blanks = ""; \
76  for(int i = 0; i < blankSize; ++i) \
77  { \
78  blanks += " "; \
79  } \
80  std::cout << msg_.str() << blanks << COLOR << ACBold << MSG << ACPlain << std::endl; \
81  }
82 
83 #define STDLINE(MSG, COLOR) \
84  { \
85  std::stringstream msg_; \
86  std::stringstream PFs_; \
87  std::string PF_ = __MODULE__; \
88  int PFSize = PF_.size(); \
89  int maxL = 90; \
90  if(PFSize > __OFFSET__) \
91  { \
92  PFSize = __OFFSET__; \
93  } \
94  for(int i = 0; i < PFSize; ++i) \
95  { \
96  PFs_ << PF_[i]; \
97  } \
98  if(PFSize < __OFFSET__) \
99  { \
100  for(int i = 0; i < __OFFSET__ + 3 - PFSize; ++i) \
101  { \
102  PFs_ << " "; \
103  } \
104  } \
105  if(PFSize < (int)PF_.size()) \
106  { \
107  maxL -= 4; \
108  PFs_ << ACRed << "..."; \
109  } \
110  else \
111  { \
112  PFs_ << ACPlain << ACPlain << ACPlain << ACPlain; \
113  } \
114  msg_ << ACCyan << ACBold << __LINE__ << ACPlain << ACYellow << "\t] [" << ACPlain << ACWhite << ACBold << PFs_.str() << ACPlain << ACYellow << "]"; \
115  int msgS = msg_.str().size() + 1; \
116  if(msgS < __OFFSET__) \
117  msgS = __OFFSET__; \
118  int blankSize = maxL - msgS; \
119  if(blankSize < 0) \
120  blankSize = 3; \
121  std::string blanks = ""; \
122  for(int i = 0; i < blankSize; ++i) \
123  { \
124  blanks += " "; \
125  } \
126  std::cout << msg_.str() << blanks << COLOR << ACBold << MSG << ACPlain << std::endl; \
127  }
128 
129 #define STDSNAP(MSG, COLOR) \
130  { \
131  std::stringstream msg_; \
132  std::stringstream PFs_; \
133  std::string PF_ = __MODULE__; \
134  int PFSize = PF_.size(); \
135  int maxL = 90; \
136  if(PFSize > __OFFSET__) \
137  { \
138  PFSize = __OFFSET__; \
139  } \
140  for(int i = 0; i < PFSize; ++i) \
141  { \
142  PFs_ << PF_[i]; \
143  } \
144  if(PFSize <= __OFFSET__) \
145  { \
146  for(int i = 0; i < __OFFSET__ + 3 - PFSize; ++i) \
147  { \
148  PFs_ << " "; \
149  } \
150  } \
151  if(PFSize < (int)PF_.size()) \
152  { \
153  maxL -= 4; \
154  PFs_ << ACRed << "..."; \
155  } \
156  msg_ << ACCR << ACCyan << ACBold << __LINE__ << ACPlain << ACYellow << "\t] [" << ACPlain << ACWhite << ACBold << PFs_.str() << ACPlain << ACYellow \
157  << "]"; \
158  int blankSize = maxL - msg_.str().size() + 1; \
159  if(blankSize < 0) \
160  blankSize = 3; \
161  std::string blanks = ""; \
162  for(int i = 0; i < blankSize; ++i) \
163  { \
164  blanks += " "; \
165  } \
166  std::cout << msg_.str() << blanks << COLOR << ACBold << MSG << ACPlain << std::flush; \
167  }
168 
169 class Utils
170 {
171  public:
172  Utils() { ; }
173 
174  inline static int toInt(std::string inputString)
175  {
176  std::istringstream iss_;
177  iss_.str(inputString);
178  int n;
179  iss_ >> n;
180 
181  return n;
182  }
183  //=============================================================================
184  inline static double toDouble(std::string inputString)
185  {
186  std::istringstream iss_;
187  iss_.str(inputString);
188  double n;
189  iss_ >> n;
190 
191  return n;
192  }
193  //=============================================================================
194  inline static int toDec(unsigned long long inputBinary)
195  {
196  std::stringstream ss;
197  ss.str("");
198  int n = inputBinary;
199  ss << std::dec << n;
200  n = Utils::toInt(ss.str());
201 
202  return n;
203  }
204 };
205 
206 #endif // MESSAGETOOLS_H