otsdaq  v2_03_00
DMMacros.h
1 #ifndef __DM_MACROS_H__
2 #define __DM_MACROS_H__
3 
4 /*
5  * C++ ASCII output decorator
6  *
7  * Copyright (C) 2018 by
8  * Dario Menasce dario.menasce@cern.ch
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * ----------------------------------------------------------------------
21 
22  This macro decorates a cout statement with auxilary information
23  elements such as line number, file name etc...
24  The most general form of decoration provides the following elements:
25 
26  line # function-name file-name date time
27 user-cout arguments 34 ] [int main(int argc, cha**argv))] [example.cc] [Jun 22 2018]
28 [10:13:54] Result is 1.2
29 
30  The following name-spaced variables control the actual output produced:
31 
32  int DMM::__span__ : controls the width of the function/file name field
33 (number of character)s
34  int DMM::__pthr__ : controls the priority level: only cout
35 statements declared with an equal or above level with respect to the current value of the
36 DMM::__pthr__ will be printed out bool DMM::__enablePrintouts__ : switches of the
37 following cout statment (does not produce output bool DMM::__long__ :
38 shortens the name of the function (removes signature if false) bool DMM::__meth__
39 : enables/disables output of the function-name field bool DMM::__file__ :
40 enables/disables output of the file-name field bool DMM::__date__ :
41 enables/disables output of the date field bool DMM::__time__ :
42 enables/disables output of the time field std::string DMM::__c_meth__ : determines the
43 color of the function-name field
44  std::string DMM::__c_file__ : determines the color of the file-name field
45  std::string DMM::__c_date__ : determines the color of the date field
46  std::string DMM::__c_time__ : determines the color of the time field
47 
48  To GLOBALLY enable/diable colors, just edit this header file and modify the
49  #define __USE_COLORS__ true
50  statement below to false
51 
52  The list of colors is defined below (variables beginning with AC and ending with _)
53 
54  Usage example:
55 
56  #include <DMMacros.h>
57 
58  int main(int argc, char** argv)
59  {
60 
61  int i = 5 ;
62  double d = 4.6 ;
63  string s = "Just a regular string" ;
64 
65  someClassWithAVeryLongName * sc = new someClassWithAVeryLongName() ;
66  sc->doSomethingInThisClass() ;
67 
68  DMM::__pthr__ = 0 ;
69  __PRE__(0,{cout << "A: " << i << " " << ACBCyan_ << d << ACPlain_ << " " << s
70  << " --------------------------------------------------------------"
71  << endl ;})
72  __PRE__(3,{cout << "B: " << i << " " << ACBCyan_ << d << ACPlain_ << " " << s <<
73 endl ;}) DMM::__enablePrintouts__ = false ;
74  __PRE__(0,{cout << "C: " << i << " " << ACBCyan_ << d << ACPlain_ << " " << s <<
75 endl ;}) DMM::__enablePrintouts__ = true ;
76  __PRE__(0,{cout << "D: " << i << " " << ACBCyan_ << d << ACPlain_ << " " << s <<
77 endl ;}) DMM::__long__ = true ; DMM::__file__ = true ;
78  __PRE__(0,{cout << "E: " << i << " " << ACBCyan_ << d << ACPlain_ << " " << s <<
79 endl ;}) DMM::__c_meth__ = ACBGreen_;
80  __PRE__(0,{cout << "F: " << i << " " << ACBCyan_ << d << ACPlain_ << " " << s <<
81 endl ;}) DMM::__c_meth__ = ACBGreen_ + ACReverse_; DMM::__meth__ = false ; DMM::__file__ =
82 false ; DMM::__date__ = true ; DMM::__time__ = true ;
83  __PRE__(0,{cout << "G: " << i << " " << ACBCyan_ << d << ACPlain_ << " " << s <<
84 endl ;}) DMM::__meth__ = false ; DMM::__file__ = true ; DMM::__date__ = false ;
85 DMM::__time__ = false ;
86  __PRE__(0,{cout << "H: " << i << " " << ACBCyan_ << d << ACPlain_ << " " << s <<
87 endl ;}) DMM::__meth__ = true ; DMM::__file__ = true ; DMM::__date__ = true ;
88 DMM::__time__ = true ; DMM::__c_file__ = ACBBrown_ + ACReverse_;
89  __PRE0__( cout << "I: " << i << " " << ACBCyan_ << d << ACPlain_ << " " << s <<
90 endl ; )
91  }
92 
93  Output (except colors...)
94 
95 17 ] [someClassWithAVeryLongName::so... ] 1 Ctor
96 23 ] [doSomethingInThisClass ] 2 do whatever needs to be done
97 39 ] [main ] A: 5 4.6 Just a regular string
98 -------------------------------------------------------------- 40 ] [main
99 ] B: 5 4.6 Just a regular string 44 ] [main ] D: 5 4.6
100 Just a regular string 46 ] [int main(int, char**) ] [main.cc
101 ] E: 5 4.6 Just a regular string 48 ] [int main(int, char**) ] [main.cc
102 ] F: 5 4.6 Just a regular string 51 ] [Jun 22 2018] [13:32:26] G: 5 4.6 Just a regular
103 string 53 ] [main.cc ] H: 5 4.6 Just a regular string 56
104 ] [int main(int, char**) ] [main.cc ] [Jun 22
105 2018] [13:32:26] I: 5 4.6 Just a regular string
106 
107  To see color, type: more DMMacros.h
108 
109 17 ]
110 [someClassWithAVeryLongName::so... ] 1
111 Ctor 23 ]
112 [doSomethingInThisClass  ] 2
113 do whatever needs to be done 39 ]
114 [main  ] A:
115 5 4.6 Just a regular string
116 -------------------------------------------------------------- 40 ]
117 [main  ] B:
118 5 4.6 Just a regular string 44 ]
119 [main  ] D:
120 5 4.6 Just a regular string 46 ]
121 [int main(int, char**)  ]
122 [main.cc  ] E:
123 5 4.6 Just a regular string 48 ]
124 [int main(int, char**)  ]
125 [main.cc  ] F:
126 5 4.6 Just a regular string 51 ]
127 [Jun 22 2018] [13:44:32]
128 G: 5 4.6 Just a regular string 53 ]
129 [main.cc  ] H:
130 5 4.6 Just a regular string 56 ]
131 [int main(int, char**)  ]
132 [main.cc  ]
133 [Jun 22 2018] [13:44:32]
134 I: 5 4.6 Just a regular string
135  */
136 
137 #include <iostream>
138 #include <sstream>
139 
140 using namespace std;
141 
142 #define __USE_COLORS__ true
143 
144 #if __USE_COLORS__ == false
145 
146 #define ACBlack ""
147 #define ACBlue ""
148 #define ACGreen ""
149 #define ACCyan ""
150 #define ACRed ""
151 #define ACPurple ""
152 #define ACBrown ""
153 #define ACGray ""
154 #define ACDarkGray ""
155 #define ACLightBlue ""
156 #define ACLightGreen ""
157 #define ACLightCyan ""
158 #define ACLightRed ""
159 #define ACLightPurple ""
160 #define ACYellow ""
161 #define ACWhite ""
162 
163 #define ACPlain ""
164 #define ACBold ""
165 #define ACUnderline ""
166 #define ACBlink ""
167 #define ACReverse ""
168 
169 #define ACClear ""
170 #define ACClearL ""
171 
172 #define ACCR ""
173 
174 #define ACSave ""
175 #define ACRecall ""
176 
177 #elif __USE_COLORS__ == true
178 
179 #define ACBlack "\033[0;30m"
180 #define ACBlue "\033[0;34m"
181 #define ACGreen "\033[0;32m"
182 #define ACCyan "\033[0;36m"
183 #define ACRed "\033[0;31m"
184 #define ACPurple "\033[0;35m"
185 #define ACBrown "\033[0;33m"
186 #define ACGray "\033[0;37m"
187 #define ACDarkGray "\033[1;30m"
188 #define ACLightBlue "\033[1;34m"
189 #define ACLightGreen "\033[1;32m"
190 #define ACLightCyan "\033[1;36m"
191 #define ACLightRed "\033[1;31m"
192 #define ACLightPurple "\033[1;35m"
193 #define ACYellow "\033[1;33m"
194 #define ACWhite "\033[1;37m"
195 
196 #define ACPlain "\033[0m"
197 #define ACBold "\033[1m"
198 #define ACUnderline "\033[4m"
199 #define ACBlink "\033[5m"
200 #define ACReverse "\033[7m"
201 
202 #define ACClear "\033[2J"
203 #define ACClearL "\033[2K"
204 
205 #define ACCR "\r"
206 
207 #define ACSave "\033[s"
208 #define ACRecall "\033[u"
209 
210 #endif
211 
212 static std::string ACBlack_ = ACBlack;
213 static std::string ACBlue_ = ACBlue;
214 static std::string ACGreen_ = ACGreen;
215 static std::string ACCyan_ = ACCyan;
216 static std::string ACRed_ = ACRed;
217 static std::string ACPurple_ = ACPurple;
218 static std::string ACBrown_ = ACBrown;
219 static std::string ACGray_ = ACGray;
220 static std::string ACDarkGray_ = ACDarkGray;
221 static std::string ACLightBlue_ = ACLightBlue;
222 static std::string ACLightGreen_ = ACLightGreen;
223 static std::string ACLightCyan_ = ACLightCyan;
224 static std::string ACLightRed_ = ACLightRed;
225 static std::string ACLightPurple_ = ACLightPurple;
226 static std::string ACYellow_ = ACYellow;
227 static std::string ACWhite_ = ACWhite;
228 
229 static std::string ACPlain_ = ACPlain;
230 static std::string ACBold_ = ACBold;
231 static std::string ACUnderline_ = ACUnderline;
232 static std::string ACBlink_ = ACBlink;
233 static std::string ACReverse_ = ACReverse;
234 
235 static std::string ACClear_ = ACClear;
236 static std::string ACClearL_ = ACClearL;
237 
238 static std::string ACCR_ = ACCR;
239 
240 static std::string ACSave_ = ACSave;
241 static std::string ACRecall_ = ACRecall;
242 
243 static std::string ACBBlack_ = ACBlack + ACBold_;
244 static std::string ACBBlue_ = ACBlue + ACBold_;
245 static std::string ACBGreen_ = ACGreen + ACBold_;
246 static std::string ACBCyan_ = ACCyan + ACBold_;
247 static std::string ACBRed_ = ACRed + ACBold_;
248 static std::string ACBPurple_ = ACPurple + ACBold_;
249 static std::string ACBBrown_ = ACBrown + ACBold_;
250 static std::string ACBGray_ = ACGray + ACBold_;
251 static std::string ACBDarkGray_ = ACDarkGray + ACBold_;
252 static std::string ACBLightBlue_ = ACLightBlue + ACBold_;
253 static std::string ACBLightGreen_ = ACLightGreen + ACBold_;
254 static std::string ACBLightCyan_ = ACLightCyan + ACBold_;
255 static std::string ACBLightRed_ = ACLightRed + ACBold_;
256 static std::string ACBLightPurple_ = ACLightPurple + ACBold_;
257 static std::string ACBYellow_ = ACYellow + ACBold_;
258 static std::string ACBWhite_ = ACWhite + ACBold_;
259 
260 namespace DMM
261 {
262 static int __span__ = 30;
263 static int __pthr__ = 0;
264 static bool __enablePrintouts__ = true;
265 // static bool __disableColors__ = false ;
266 static bool __long__ = true;
267 static bool __meth__ = true;
268 static bool __file__ = false;
269 static bool __date__ = false;
270 static bool __time__ = false;
271 static std::string __c_file__ = ACBCyan_;
272 static std::string __c_date__ = ACBLightCyan_;
273 static std::string __c_time__ = ACBLightBlue_;
274 static std::string __c_meth__ = ACBLightPurple_;
275 
276 #define __PRE__(priority, iostream) \
277  { \
278  if(DMM::__enablePrintouts__) \
279  { \
280  std::stringstream msg_; \
281  std::stringstream ss; \
282  std::string toTrim; \
283  std::string PF_; \
284  std::stringstream PFs_; \
285  std::string blanks = ""; \
286  int PFSize = 0; \
287  int maxL = 30; \
288  int msgS; \
289  int blankSize; \
290  \
291  msg_ << ACBCyan_ << __LINE__ << ACPlain << ACYellow << "\t] " << ACPlain; \
292  \
293  if(DMM::__meth__) \
294  { \
295  PF_ = __FUNCTION__; \
296  if(DMM::__long__) \
297  PF_ = __PRETTY_FUNCTION__; \
298  PFSize = PF_.size(); \
299  if(PFSize >= DMM::__span__) \
300  { \
301  PFSize = DMM::__span__; \
302  } \
303  for(int i = 0; i < PFSize; ++i) \
304  { \
305  PFs_ << PF_[i]; \
306  } \
307  if(PFSize < DMM::__span__) \
308  { \
309  for(int i = 0; i < DMM::__span__ + 3 - PFSize; ++i) \
310  { \
311  PFs_ << " "; \
312  } \
313  } \
314  if(PFSize < (int)PF_.size()) \
315  { \
316  maxL -= 4; \
317  PFs_ << ACRed << "..."; \
318  } \
319  else \
320  { \
321  PFs_ << ACPlain << ACPlain << ACPlain; \
322  } \
323  msgS = PFs_.str().size() + 1; \
324  if(msgS <= DMM::__span__) \
325  msgS = DMM::__span__; \
326  blankSize = maxL - msgS; \
327  if(blankSize < 0) \
328  blankSize = 3; \
329  for(int i = 0; i < blankSize; ++i) \
330  { \
331  blanks += " "; \
332  } \
333  PFs_ << blanks; \
334  msg_ << ACYellow << "[" << DMM::__c_meth__ << PFs_.str() << ACPlain \
335  << ACYellow << "] " << ACPlain; \
336  } \
337  \
338  if(DMM::__file__) \
339  { \
340  PFs_.str(""); \
341  PF_ = __FILE__; \
342  PFSize = PF_.size(); \
343  if(PFSize >= DMM::__span__) \
344  { \
345  PFSize = DMM::__span__; \
346  } \
347  for(int i = 0; i < PFSize; ++i) \
348  { \
349  PFs_ << PF_[i]; \
350  } \
351  if(PFSize < DMM::__span__) \
352  { \
353  for(int i = 0; i < DMM::__span__ + 3 - PFSize; ++i) \
354  { \
355  PFs_ << " "; \
356  } \
357  } \
358  if(PFSize < (int)PF_.size()) \
359  { \
360  maxL -= 4; \
361  PFs_ << ACRed << "..."; \
362  } \
363  else \
364  { \
365  PFs_ << ACPlain << ACPlain << ACPlain; \
366  } \
367  msgS = PFs_.str().size() + 1; \
368  if(msgS <= DMM::__span__) \
369  msgS = DMM::__span__; \
370  blankSize = maxL - msgS; \
371  if(blankSize < 0) \
372  blankSize = 3; \
373  for(int i = 0; i < blankSize; ++i) \
374  { \
375  blanks += " "; \
376  } \
377  PFs_ << blanks; \
378  msg_ << ACYellow << "[" << DMM::__c_file__ << PFs_.str() << ACPlain \
379  << ACYellow << "] " << ACPlain; \
380  } \
381  \
382  if(DMM::__date__) \
383  { \
384  msg_ << ACYellow << "[" << DMM::__c_date__ << __DATE__ << ACPlain \
385  << ACYellow << "] " << ACPlain; \
386  } \
387  \
388  if(DMM::__time__) \
389  { \
390  msg_ << ACYellow << "[" << DMM ::__c_time__ << __TIME__ << ACPlain \
391  << ACYellow << "] " << ACPlain; \
392  } \
393  if(priority >= DMM::__pthr__) \
394  { \
395  std::cout << msg_.str(); \
396  iostream; \
397  cout << ACPlain; \
398  } \
399  } \
400  }
401 } // namespace DMM
402 
403 #define __PRE0__(iostream) __PRE__(0, iostream)
404 
405 #endif // __DM_MACROS__