~ma5/madanalysis5/madanalysis-development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
////////////////////////////////////////////////////////////////////////////////
//  
//  Copyright (C) 2012-2016 Eric Conte, Benjamin Fuks
//  The MadAnalysis development team, email: <ma5team@iphc.cnrs.fr>
//  
//  This file is part of MadAnalysis 5.
//  Official website: <https://launchpad.net/madanalysis5>
//  
//  MadAnalysis 5 is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  MadAnalysis 5 is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with MadAnalysis 5. If not, see <http://www.gnu.org/licenses/>
//  
////////////////////////////////////////////////////////////////////////////////


#ifndef LOG_STREAM_H
#define LOG_STREAM_H


// SampleAnalyzer headers
#include "SampleAnalyzer/Commons/Base/PortableDatatypes.h"

// STL headers
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <typeinfo>
#include <sstream>


namespace MA5
{

//////////////////////////////////////////////////////////////////////////////
/// The class LogStream is a logger which extends the class std::ostream
/// such as std::cout.
//////////////////////////////////////////////////////////////////////////////
class LogStream
{
 public:

  enum ColorType {NONE=0, BLACK=30, BLUE=34, GREEN=32, CYAN=36, 
                  RED=31, PURPLE=35, YELLOW=33, WHITE=37};

  // -------------------------------------------------------------
  //                        data members
  // -------------------------------------------------------------
 private:

  /// Special manipulator which replaces std::endl for logger of LogStream type
  friend LogStream& endmsg(LogStream& os);

  /// Pointer to the output stream used 
  mutable std::ostream* Stream_; 

  /// Stream used as bugger 
  mutable std::stringstream Buffer_;

  /// Using color ?
  MAbool ColorMode_;

  /// Color type
  ColorType Color_;

  /// Mute or UnMute ?
  MAbool Mute_;

  /// Tag specifying if a new line is begun
  MAbool NewLine_;

  /// String displayed at the beginning of a new line 
  std::string BeginLine_;

  /// String displayed at the end of a new line
  std::string EndLine_;

  /// Word used as prompt
  std::string Prompt_;

  // -------------------------------------------------------------
  //                       method members
  // -------------------------------------------------------------

 public:

  /// Constructor without argument
  LogStream() : Stream_(&std::cout), ColorMode_(true), 
                Color_(NONE), Mute_(false),
                NewLine_(true)
  {}
  
  /// Copy constructor
  LogStream(const LogStream& ref)
  {
    Stream_    = ref.Stream_; 
    ColorMode_ = ref.ColorMode_;
    Mute_      = ref.Mute_;
    NewLine_   = ref.NewLine_;
    Color_     = ref.Color_;
    BeginLine_ = ref.BeginLine_;
    EndLine_   = ref.EndLine_;
    Prompt_    = ref.Prompt_;
  }

  /// Clearing the content
  void Reset()
  {
    ColorMode_    = true;
    Color_        = NONE;
    Stream_       = &std::cout;
    Mute_         = false;
    NewLine_      = true;
    Prompt_       = "";
  }

  /// Mutator related to the output stream
  void SetStream(std::ostream* stream)
  { Stream_=stream; }

  /// Accessor to the output stream
  std::ostream* GetStream() const
  { return Stream_; }

  /// Overloading operator << for bool value
  LogStream& operator<< (bool val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for short value
  LogStream& operator<< (short val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for ushort value
  LogStream& operator<< (unsigned short val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for char value
  LogStream& operator<< (char val)
  { 
    if (NewEntry()) Buffer_ << static_cast<signed int>(val);
    return *this;
  }

  /// Overloading operator << for uchar value
  LogStream& operator<< (unsigned char val)
  { 
    if (NewEntry()) Buffer_ << static_cast<unsigned int>(val);
    return *this;
  }

  /// Overloading operator << for int value
  LogStream& operator<< (int val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for uint value
  LogStream& operator<< (unsigned int val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for long value
  LogStream& operator<< (long val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for long value
  LogStream& operator<< (long long val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for ulong value
  LogStream& operator<< (unsigned long val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for ulong value
  LogStream& operator<< (unsigned long long val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for float value
  LogStream& operator<< (float val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for double value
  LogStream& operator<< (double val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for long double value
  LogStream& operator<< (long double val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for const char* value
  LogStream& operator<< (const char * val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for const signed char* value
  LogStream& operator<< (const signed char * val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for const unsigned char* value
  LogStream& operator<< (const unsigned char * val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for string value
  LogStream& operator<< (std::string val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for any function
  LogStream& operator<< (const void* val)
  { 
    if (NewEntry()) Buffer_ << val;
    return *this;
  }

  /// Overloading operator << for other stream 
  LogStream& operator<< (std::ostream& ( *pf )(std::ostream&))
  {
    if (NewEntry()) pf(Buffer_);
    return *this;
  }

  /// Overloading operator << for LogStream stream 
  LogStream& operator<< (LogStream& ( *pf )(LogStream&))
  {
    if (NewEntry()) pf(*this);
    return *this;
  }

  /// Overloading operator << for manipulator
  LogStream& operator<< (std::ios& ( *pf )(std::ios&))
  {
    if (NewEntry()) pf(Buffer_);
    return *this;
  }

  /// Overloading operator << for manipulator
  LogStream& operator<< (std::ios_base& ( *pf )(std::ios_base&))
  {
    if (NewEntry()) pf(Buffer_);
    return *this;
  }

  /// Overloading operator << for manipulator std::setfill
//  LogStream& operator<<(std::_Setfill<char> v)
//  {
//    if (NewEntry()) Buffer_<<v;
//    return *this;
//  }
//
//  /// Overloading operator << for manipulator std::setiosflags
//  LogStream& operator<<(std::_Setiosflags v)
//  {
//    if (NewEntry()) Buffer_<<v;
//    return *this;
//  }
//
//  /// Overloading operator << for manipulator std::resetiosflags
//  LogStream& operator<<(std::_Resetiosflags v)
//  {
//    if (NewEntry()) Buffer_<<v;
//    return *this;
//  }
//
//  /// Overloading operator << for manipulator std::setbase
//  LogStream& operator<<(std::_Setbase v)
//  {
//    if (NewEntry()) Buffer_<<v;
//    return *this;
//  }
//
//  /// Overloading operator << for manipulator std::setprecision
//  LogStream& operator<<(std::_Setprecision v)
//  {
//    if (NewEntry()) Buffer_<<v;
//    return *this;
//  }
//
//  /// Overloading operator << for manipulator std::setw
//  LogStream& operator<<(std::_Setw v)
//  {
//    if (NewEntry()) Buffer_<<v;
//    return *this;
//  }

  /// Enabling the color mode
  void EnableColor()
  { ColorMode_=true; Update(); }

  /// Disabling the color mode
  void DisableColor()
  { ColorMode_=false; Update(); }

  /// Is the stream mute ?
  MAbool IsMute()
  { return Mute_;}

  /// Is the stream unmute ?
  MAbool IsUnMute()
  { return !Mute_;}

  /// Mute the stream
  void SetMute()
  { Mute_=true; }

  /// UnMute the stream
  void SetUnMute()
  { Mute_=false; }

  /// Mutator relative to the prompt
  void SetPrompt(const std::string& prompt)
  { Prompt_=prompt; Update(); }

  /// Accessor to the prompt
  const std::string& GetPrompt() const
  { return Prompt_; }

  /// Mutator relative to the prompt
  void SetColor(ColorType color)
  { Color_=color; Update(); }

  /// Accessor to the color
  ColorType GetColor() const
  { return Color_; }

  /// Getting the character used for filling
  char fill() const
  { return Buffer_.fill(); }

  /// Setting the character used for filling
  char fill(char fillch) 
  { return Buffer_.fill(fillch); }

  /// Setting specific format flags
  std::ios_base::fmtflags setf(std::ios_base::fmtflags fmtfl)
  { return Buffer_.setf(fmtfl); }

  /// Setting specific format flags and mask
  std::ios_base::fmtflags setf(std::ios_base::fmtflags fmtfl,
                               std::ios_base::fmtflags mask)
  { return Buffer_.setf(fmtfl, mask); }

  /// Clearing specific format flags
  void unsetf(std::ios_base::fmtflags mask)
  { Buffer_.unsetf(mask); }

  /// Getting the digit precision
  std::streamsize precision() const
  { return Buffer_.precision(); }

  /// Setting the digit precision
  std::streamsize precision(std::streamsize prec)
  { return Buffer_.precision(prec); }

  /// Getting the width
  std::streamsize width() const
  { return Buffer_.width(); }

  /// Setting the width
  std::streamsize width(std::streamsize wide)
  { return Buffer_.width(wide); }

  /// Displaying n times a character c 
  void repeat(char c, MAuint32 n)
  {
    if (NewEntry())
    { 
      Buffer_ << ""; 
      std::streamsize fillc = Buffer_.fill();
      Buffer_.fill(c); 
      Buffer_.width(n); 
      Buffer_ << "";
      Buffer_.fill(fillc);
    }
  }
 
 private:

  /// Global veto applied on the stream
  MAbool NewEntry()
  {
    if (Mute_) return false;
    if (NewLine_) {Buffer_ << BeginLine_; NewLine_=false;}
    return true;
  }

  /// Updating header and foot string
  void Update()
  {
    if (!ColorMode_  || Color_==NONE)
    {
      BeginLine_="";
      EndLine_="";
    }
    else
    {
      std::stringstream str;
      str << "\x1b[" << static_cast<MAuint32>(Color_) << "m";
      BeginLine_=str.str();
      EndLine_="\x1b[0m";
    }
    BeginLine_+=Prompt_;
  }

};


/// Special manipulator which replaces std::endl for logger of LogStream type
LogStream& endmsg(LogStream& os);


}



#endif