otsdaq  v2_05_02_indev
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 #define __USE_COLORS__ true
141 
142 #if __USE_COLORS__ == false
143 
144 #define ACBlack ""
145 #define ACBlue ""
146 #define ACGreen ""
147 #define ACCyan ""
148 #define ACRed ""
149 #define ACPurple ""
150 #define ACBrown ""
151 #define ACGray ""
152 #define ACDarkGray ""
153 #define ACLightBlue ""
154 #define ACLightGreen ""
155 #define ACLightCyan ""
156 #define ACLightRed ""
157 #define ACLightPurple ""
158 #define ACYellow ""
159 #define ACWhite ""
160 
161 #define ACPlain ""
162 #define ACBold ""
163 #define ACUnderline ""
164 #define ACBlink ""
165 #define ACReverse ""
166 
167 #define ACClear ""
168 #define ACClearL ""
169 
170 #define ACCR ""
171 
172 #define ACSave ""
173 #define ACRecall ""
174 
175 #elif __USE_COLORS__ == true
176 
177 #define ACBlack "\033[0;30m"
178 #define ACBlue "\033[0;34m"
179 #define ACGreen "\033[0;32m"
180 #define ACCyan "\033[0;36m"
181 #define ACRed "\033[0;31m"
182 #define ACPurple "\033[0;35m"
183 #define ACBrown "\033[0;33m"
184 #define ACGray "\033[0;37m"
185 #define ACDarkGray "\033[1;30m"
186 #define ACLightBlue "\033[1;34m"
187 #define ACLightGreen "\033[1;32m"
188 #define ACLightCyan "\033[1;36m"
189 #define ACLightRed "\033[1;31m"
190 #define ACLightPurple "\033[1;35m"
191 #define ACYellow "\033[1;33m"
192 #define ACWhite "\033[1;37m"
193 
194 #define ACPlain "\033[0m"
195 #define ACBold "\033[1m"
196 #define ACUnderline "\033[4m"
197 #define ACBlink "\033[5m"
198 #define ACReverse "\033[7m"
199 
200 #define ACClear "\033[2J"
201 #define ACClearL "\033[2K"
202 
203 #define ACCR "\r"
204 
205 #define ACSave "\033[s"
206 #define ACRecall "\033[u"
207 
208 #endif
209 
210 static std::string ACBlack_ = ACBlack;
211 static std::string ACBlue_ = ACBlue;
212 static std::string ACGreen_ = ACGreen;
213 static std::string ACCyan_ = ACCyan;
214 static std::string ACRed_ = ACRed;
215 static std::string ACPurple_ = ACPurple;
216 static std::string ACBrown_ = ACBrown;
217 static std::string ACGray_ = ACGray;
218 static std::string ACDarkGray_ = ACDarkGray;
219 static std::string ACLightBlue_ = ACLightBlue;
220 static std::string ACLightGreen_ = ACLightGreen;
221 static std::string ACLightCyan_ = ACLightCyan;
222 static std::string ACLightRed_ = ACLightRed;
223 static std::string ACLightPurple_ = ACLightPurple;
224 static std::string ACYellow_ = ACYellow;
225 static std::string ACWhite_ = ACWhite;
226 
227 static std::string ACPlain_ = ACPlain;
228 static std::string ACBold_ = ACBold;
229 static std::string ACUnderline_ = ACUnderline;
230 static std::string ACBlink_ = ACBlink;
231 static std::string ACReverse_ = ACReverse;
232 
233 static std::string ACClear_ = ACClear;
234 static std::string ACClearL_ = ACClearL;
235 
236 static std::string ACCR_ = ACCR;
237 
238 static std::string ACSave_ = ACSave;
239 static std::string ACRecall_ = ACRecall;
240 
241 static std::string ACBBlack_ = ACBlack + ACBold_;
242 static std::string ACBBlue_ = ACBlue + ACBold_;
243 static std::string ACBGreen_ = ACGreen + ACBold_;
244 static std::string ACBCyan_ = ACCyan + ACBold_;
245 static std::string ACBRed_ = ACRed + ACBold_;
246 static std::string ACBPurple_ = ACPurple + ACBold_;
247 static std::string ACBBrown_ = ACBrown + ACBold_;
248 static std::string ACBGray_ = ACGray + ACBold_;
249 static std::string ACBDarkGray_ = ACDarkGray + ACBold_;
250 static std::string ACBLightBlue_ = ACLightBlue + ACBold_;
251 static std::string ACBLightGreen_ = ACLightGreen + ACBold_;
252 static std::string ACBLightCyan_ = ACLightCyan + ACBold_;
253 static std::string ACBLightRed_ = ACLightRed + ACBold_;
254 static std::string ACBLightPurple_ = ACLightPurple + ACBold_;
255 static std::string ACBYellow_ = ACYellow + ACBold_;
256 static std::string ACBWhite_ = ACWhite + ACBold_;
257 
258 namespace DMM
259 {
260 static int __span__ = 30;
261 static int __pthr__ = 0;
262 static bool __enablePrintouts__ = true;
263 // static bool __disableColors__ = false ;
264 static bool __long__ = true;
265 static bool __meth__ = true;
266 static bool __file__ = false;
267 static bool __date__ = false;
268 static bool __time__ = false;
269 static std::string __c_file__ = ACBCyan_;
270 static std::string __c_date__ = ACBLightCyan_;
271 static std::string __c_time__ = ACBLightBlue_;
272 static std::string __c_meth__ = ACBLightPurple_;
273 
274 #define __PRE__(priority, iostream) \
275  { \
276  if(DMM::__enablePrintouts__) \
277  { \
278  std::stringstream msg_; \
279  std::stringstream ss; \
280  std::string toTrim; \
281  std::string PF_; \
282  std::stringstream PFs_; \
283  std::string blanks = ""; \
284  int PFSize = 0; \
285  int maxL = 30; \
286  int msgS; \
287  int blankSize; \
288  \
289  msg_ << ACBCyan_ << __LINE__ << ACPlain << ACYellow << "\t] " << ACPlain; \
290  \
291  if(DMM::__meth__) \
292  { \
293  PF_ = __FUNCTION__; \
294  if(DMM::__long__) \
295  PF_ = __PRETTY_FUNCTION__; \
296  PFSize = PF_.size(); \
297  if(PFSize >= DMM::__span__) \
298  { \
299  PFSize = DMM::__span__; \
300  } \
301  for(int i = 0; i < PFSize; ++i) \
302  { \
303  PFs_ << PF_[i]; \
304  } \
305  if(PFSize < DMM::__span__) \
306  { \
307  for(int i = 0; i < DMM::__span__ + 3 - PFSize; ++i) \
308  { \
309  PFs_ << " "; \
310  } \
311  } \
312  if(PFSize < (int)PF_.size()) \
313  { \
314  maxL -= 4; \
315  PFs_ << ACRed << "..."; \
316  } \
317  else \
318  { \
319  PFs_ << ACPlain << ACPlain << ACPlain; \
320  } \
321  msgS = PFs_.str().size() + 1; \
322  if(msgS <= DMM::__span__) \
323  msgS = DMM::__span__; \
324  blankSize = maxL - msgS; \
325  if(blankSize < 0) \
326  blankSize = 3; \
327  for(int i = 0; i < blankSize; ++i) \
328  { \
329  blanks += " "; \
330  } \
331  PFs_ << blanks; \
332  msg_ << ACYellow << "[" << DMM::__c_meth__ << PFs_.str() << ACPlain << ACYellow << "] " << ACPlain; \
333  } \
334  \
335  if(DMM::__file__) \
336  { \
337  PFs_.str(""); \
338  PF_ = __FILE__; \
339  PFSize = PF_.size(); \
340  if(PFSize >= DMM::__span__) \
341  { \
342  PFSize = DMM::__span__; \
343  } \
344  for(int i = 0; i < PFSize; ++i) \
345  { \
346  PFs_ << PF_[i]; \
347  } \
348  if(PFSize < DMM::__span__) \
349  { \
350  for(int i = 0; i < DMM::__span__ + 3 - PFSize; ++i) \
351  { \
352  PFs_ << " "; \
353  } \
354  } \
355  if(PFSize < (int)PF_.size()) \
356  { \
357  maxL -= 4; \
358  PFs_ << ACRed << "..."; \
359  } \
360  else \
361  { \
362  PFs_ << ACPlain << ACPlain << ACPlain; \
363  } \
364  msgS = PFs_.str().size() + 1; \
365  if(msgS <= DMM::__span__) \
366  msgS = DMM::__span__; \
367  blankSize = maxL - msgS; \
368  if(blankSize < 0) \
369  blankSize = 3; \
370  for(int i = 0; i < blankSize; ++i) \
371  { \
372  blanks += " "; \
373  } \
374  PFs_ << blanks; \
375  msg_ << ACYellow << "[" << DMM::__c_file__ << PFs_.str() << ACPlain << ACYellow << "] " << ACPlain; \
376  } \
377  \
378  if(DMM::__date__) \
379  { \
380  msg_ << ACYellow << "[" << DMM::__c_date__ << __DATE__ << ACPlain << ACYellow << "] " << ACPlain; \
381  } \
382  \
383  if(DMM::__time__) \
384  { \
385  msg_ << ACYellow << "[" << DMM ::__c_time__ << __TIME__ << ACPlain << ACYellow << "] " << ACPlain; \
386  } \
387  if(priority >= DMM::__pthr__) \
388  { \
389  std::cout << msg_.str(); \
390  iostream; \
391  cout << ACPlain; \
392  } \
393  } \
394  }
395 } // namespace DMM
396 
397 #define __PRE0__(iostream) __PRE__(0, iostream)
398 
399 #endif // __DM_MACROS__