Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
Cout.cxx
1/*
2 * HalCout.cpp
3 *
4 * Created on: 20-06-2013
5 * Author: Daniel Wielanek
6 */
7
8#include "Cout.h"
9#include <TRegexp.h>
10#include <iostream>
11//#ifndef fgLineLength
12//#define fgLineLength 100
13//#define (fgLineLength-4) (fgLineLength-4)
14//#endif
15#if !defined(R__ALPHA) && !defined(R__SOLARIS) && !defined(R__ACC) && !defined(R__FBSD)
16
17#endif
18namespace Hal {
19 Int_t Cout::fgLineLength = 100;
20 Cout* Cout::fgInstance = NULL;
21 Hal::EInfo Cout::fgVerboseMode = Hal::EInfo::kInfo;
22 void Cout::Database(Int_t no, ...) {
23 va_list ap;
24 TString begin;
25 TString capt;
26 TString line;
27 va_start(ap, no);
28 Int_t wide = (fgLineLength - 2 - no) / no;
29 Int_t act_wide;
30 Int_t total = 0;
31 for (Int_t i = 0; i < no; i++) {
32 capt = va_arg(ap, char*);
33 // sl = capt.Length();
34 line = " ";
35 line = line + capt;
36 act_wide = wide - line.Length();
37 for (Int_t j = 0; j < act_wide - 1; j++) {
38 line = " " + line;
39 }
40 total += act_wide;
41 if (i < no - 1) line = line + "|";
42 begin = begin + line;
43 }
44
45 va_end(ap);
46 Text(begin, "R");
47 }
48
49 void Cout::Database(std::initializer_list<TString> list) {
50 auto strings = Hal::Std::GetVector(list);
51 Database(strings);
52 }
53
54 void Cout::Database(const std::vector<TString>& list) {
55 TString begin;
56 TString capt;
57 TString line;
58 Int_t no = list.size();
59 Int_t wide = (fgLineLength - 2 - no) / no;
60 Int_t act_wide;
61 Int_t total = 0;
62 for (unsigned int i = 0; i < list.size(); i++) {
63 capt = list.at(i);
64 line = " ";
65 line = line + capt;
66 act_wide = wide - line.Length();
67 for (Int_t j = 0; j < act_wide - 1; j++) {
68 line = " " + line;
69 }
70 total += act_wide;
71 if (i < (unsigned int) (no - 1)) line = line + "|";
72 begin = begin + line;
73 }
74 Text(begin, "R");
75 }
76
77 void Cout::InStars(TString text, Color_t color) {
78 TString Header = "******";
79 Header = Header + text;
80 Int_t dlugosc = Header.Length();
81 Int_t spacji = (fgLineLength - dlugosc);
82 for (Int_t i = 0; i < spacji; i++) {
83 Header = Header + "*";
84 }
85 if ((Int_t) color == -1) {
86 std::cout << Header << std::endl;
87 } else {
88 std::cout << GetColor(color) << Header << GetDisableColor() << std::endl;
89 }
90 }
91
92 void Cout::Text(TString text, TString option, Color_t color) {
93 if (text.Length() <= fgLineLength - 4) {
94 if (option == "M") {
95 Int_t dlugosc = text.Length();
96 Int_t spacji = (fgLineLength - 2 - dlugosc) / 2;
97 for (Int_t i = 0; i < spacji; i++) {
98 text = " " + text;
99 }
100 for (Int_t i = 0; i < spacji; i++) {
101 text = text + " ";
102 }
103 if (dlugosc % 2 == 1) {
104 text = text + " ";
105 text = text + "*";
106 } else {
107 text = text + "*";
108 }
109 text = "*" + text;
110
111 } else if (option == "L") {
112 Int_t dlugosc = text.Length();
113 text = "* " + text;
114 for (Int_t i = 0; i < fgLineLength - 3 - dlugosc; i++) {
115 text = text + " ";
116 }
117 text = text + "*";
118 }
119 if (option == "R") {
120 Int_t dlugosc = text.Length();
121 text = text + " *";
122 for (Int_t i = 0; i < fgLineLength - 3 - dlugosc; i++) {
123 text = " " + text;
124 }
125 text = "*" + text;
126 }
127
128 if ((Int_t) color == -1)
129 std::cout << text << std::endl;
130 else {
131 std::cout << GetColor(color) << text << GetDisableColor() << std::endl;
132 }
133 } else {
134 Int_t zz = (Int_t) text.Length() / (fgLineLength - 4);
135 for (Int_t i = 0; i < zz; i++) {
136 Int_t beg = i * (fgLineLength - 4);
137 Int_t end = (fgLineLength - 4);
138 TString temp = TString(text(beg, end));
139 Text(temp, option, color);
140 }
141 if (zz * (fgLineLength - 4) < text.Length()) {
142 Int_t beg = zz * (fgLineLength - 4);
143 Int_t end = text.Length() - zz * (fgLineLength - 4);
144 TString temp = TString(text(beg, end));
145 Text(temp, option, color);
146 }
147 }
148 }
149
150 void Cout::Stars(Color_t color) {
151 TString starline;
152 for (Int_t i = 0; i < fgLineLength; i++) {
153 starline = starline + "*";
154 }
155 if ((Int_t) color == -1) {
156 std::cout << starline << std::endl;
157 } else {
158 std::cout << GetColor(color) << starline << GetDisableColor() << std::endl;
159 }
160 }
161
162 void Cout::PrintError(TString error) {
163 InStars("ERROR", kRed);
164 if (error.Length() < (fgLineLength - 4)) {
165 Text(error, "L", kRed);
166 } else {
167 Int_t zz = (Int_t) error.Length() / ((fgLineLength - 4));
168 for (Int_t i = 0; i < zz; i++) {
169 Int_t beg = i * (fgLineLength - 4);
170 Int_t end = (fgLineLength - 4);
171 TString temp = TString(error(beg, end));
172 Text(temp, "L", kRed);
173 }
174 if (zz * (fgLineLength - 4) < error.Length()) {
175 Int_t beg = zz * (fgLineLength - 4);
176 Int_t end = error.Length() - zz * (fgLineLength - 4);
177 TString temp = TString(error(beg, end));
178 Text(temp, "L", kRed);
179 }
180 }
181 Stars(kRed);
182 exit(0);
183 }
184
185 void Cout::PrintWarning(TString warning) {
186 InStars("WARNING", kOrange);
187 if (warning.Length() < fgLineLength - 10) {
188 Text(warning, "L", kOrange);
189 } else {
190 Int_t zz = (Int_t) warning.Length() / ((fgLineLength - 4));
191 for (Int_t i = 0; i < zz; i++) {
192 Int_t beg = i * (fgLineLength - 4);
193 Int_t end = (fgLineLength - 4);
194 TString temp = TString(warning(beg, end));
195 Text(temp, "L", kOrange);
196 }
197 if (zz * (fgLineLength - 4) < warning.Length()) {
198 Int_t beg = zz * (fgLineLength - 4);
199 Int_t end = warning.Length() - zz * (fgLineLength - 4);
200 TString temp = TString(warning(beg, end));
201 Text(temp, "L", kOrange);
202 }
203 }
204 Stars(kOrange);
205 }
206
207 void Cout::Empty(Color_t color) {
208 // TString empline;
209 // empline="*";
210 TString empline(' ', fgLineLength - 2);
211 empline = "*" + empline;
212 if ((Int_t) color == -1) {
213 std::cout << empline << std::endl;
214 } else {
215 std::cout << GetColor(color) << empline << std::endl;
216 }
217 }
218
219 void Cout::Percent(Double_t actual, Double_t total) {
220 // EnableColor(kViolet);
221 double ratio = actual / total;
222 // printf("%4d%% ", (int)(ratio*100) );
223 // printf("\n\033[F\033[J");
224 std::cout << GetColor(kViolet) << Form("%4d%% ", (int) (ratio * 100)) << Form("\n\033[F\033[J") << GetDisableColor()
225 << std::endl;
226 // DisableColor();
227 }
228
229 void Cout::ProgressBar(Double_t actual, Double_t total) {
230 // Only update r times.
231 // if ( 100 % ((Int_t)total/93) != 0 ) return;
232 // Calculuate the ratio of complete-to-incomplete.
233 double ratio = actual / total;
234 int c = ratio * (fgLineLength - 7);
235 // Show the percentage complete.
236 GetColor(kBlue);
237 printf("%3d%% [", (int) (ratio * 100));
238 // Show the load bar.
239 for (int x = 0; x < c; x++)
240 printf("=");
241 for (int x = c; x < fgLineLength - 7; x++)
242 printf(" ");
243 // ANSI Control codes to go back to the
244 // previous line and clear it.
245 printf("]\n\033[F\033[J");
247 }
248
249 void Cout::DebugInfo(Int_t i) {
250 Cout::Stars(kViolet);
251 Cout::Text(Hal::Std::RoundToString(i), "L", kViolet);
252 Cout::Stars(kViolet);
253 }
254
255 void Cout::DebugInfo(TString filename, Int_t i) {
256 Cout::Stars(kViolet);
257 Cout::Text(Form("%s %i", filename.Data(), i), "L", kViolet);
258 Cout::Stars(kViolet);
259 }
260
262#ifdef __linux__
263 return ("\e[0;0m");
264#endif
265 return " ";
266 }
267
268 TString Cout::GetColor(Color_t Color) {
269#ifdef __linux__
270 switch (Color) {
271 case kGray: {
272 return ("\e[0;30m");
273 } break;
274 case kRed: {
275 return ("\e[0;31m");
276 } break;
277 case kOrange: {
278 return ("\e[0;33m");
279 } break;
280 case kYellow: {
281 return ("\e[0;33m");
282 } break;
283 case kBlue: {
284 return ("\e[0;34m");
285 } break;
286 case kCyan: {
287 return ("\e[0;36m");
288 } break;
289 case kWhite: {
290 return ("\e[0;37m");
291 } break;
292 case kGreen: {
293 return ("\e[0;32m");
294 } break;
295 case kViolet: {
296 return ("\e[0;35m");
297 } break;
298 default: break;
299 }
300#endif
301 return "";
302 }
303
304 TString Cout::MergeStrings(Int_t no, ...) {
305 va_list ap;
306 TString begin;
307 TString capt;
308 va_start(ap, no);
309 for (Int_t i = 0; i < no; i++) {
310 capt = va_arg(ap, char*);
311 begin = begin + capt;
312 }
313 va_end(ap);
314 return begin;
315 }
316
317 void Cout::ShiftDatabase(Int_t shift, Int_t no, ...) {
318 va_list ap;
319 TString begin;
320 TString capt;
321 TString line;
322 va_start(ap, no);
323 Int_t wide = (fgLineLength - shift - 1 - 2 - no) / no;
324 Int_t sl, act_wide;
325 Int_t total = 0;
326 line = Cout::GetShift(shift);
327 for (Int_t i = 0; i < no; i++) {
328 capt = va_arg(ap, char*);
329 sl = capt.Length();
330 line = " ";
331 line = line + capt;
332 act_wide = wide - sl;
333 for (Int_t j = 0; j < act_wide - 1; j++) {
334 line = " " + line;
335 }
336 total += act_wide;
337 if (i < no - 1) line = line + "|";
338 begin = begin + line;
339 }
340
341 va_end(ap);
342 Cout::ShiftText(shift, begin, "R", -1);
343 }
344
345 void Cout::InStars(Int_t shift, TString text, Color_t color) {
346 text = Cout::GetShift(shift) + text;
347 Cout::InStars(text, color);
348 }
349
350 void Cout::ShiftText(Int_t shift, TString text, TString option, Color_t color) {
351 text = GetShift(shift) + text;
352 Cout::Text(text, option, color);
353 }
354
355 void Cout::ShiftInStars(Int_t shift, TString text, Color_t color) {
356 text = text + GetShift(shift);
357 InStars(text, color);
358 }
359
360 TString Cout::GetShift(Int_t i) {
361 TString val = "";
362 for (int j = 0; j < i; j++) {
363 val += "-";
364 }
365 return val;
366 }
367
368 void Cout::SetVerboseMode(Hal::EInfo verbose) { fgVerboseMode = verbose; }
369
370 void Cout::PrintInfo(TString text, Hal::EInfo status) {
371 if (status < fgVerboseMode) { return; }
372 switch (status) {
373 case Hal::EInfo::kDebugInfo: FailSucced(text, "DEBUG ", kCyan); break;
374 case Hal::EInfo::kInfo: FailSucced(text, "INFO ", kCyan); break;
375 case Hal::EInfo::kLowWarning: FailSucced(text, "WARNING ", kOrange); break;
376 case Hal::EInfo::kWarning: PrintWarning(text); break;
377 case Hal::EInfo::kError: FailSucced(text, "ERROR ", kRed); break;
378 case Hal::EInfo::kCriticalError: PrintError(text); break;
379 }
380 }
381
382 void Cout::FailSucced(TString value, TString flag, Color_t color) {
383 Int_t total = value.Length() + flag.Length();
384 Int_t nr_spaces = (fgLineLength - 4) - total - 2;
385 if (nr_spaces < 0) nr_spaces = 0;
386 TString spaces(' ', nr_spaces);
387 spaces = value + spaces;
388 std::cout << GetColor(kWhite) << "* " << spaces << "[" << GetColor(color) << flag << GetColor(kWhite) << "] *"
389 << GetDisableColor() << std::endl;
390 }
391
392 Cout::Cout() {
393 if (fgInstance) {
394 PrintWarning("Instance of HalCout exist");
395 } else {
396 fgInstance = this;
397 }
398 }
399
401 if (fgInstance == NULL) { fgInstance = new Cout(); }
402 return fgInstance;
403 }
404
405 Cout::~Cout() {}
406
407 void Cout::PrintLineFileInfo(TString file, Int_t line, TString text, Hal::EInfo flag) {
408 auto Colored = [](TString str, Color_t col) {
409 TString res = Hal::Cout::GetColor(kWhite) + "[" + Hal::Cout::GetDisableColor();
410 res = res + Hal::Cout::GetColor(col);
411 res = res + str;
412 res = res + Hal::Cout::GetColor(kWhite);
413 res = res + "] ";
414 res = res + Hal::Cout::GetDisableColor();
415 return res;
416 };
417 if (flag < fgVerboseMode) return;
418 auto vec = Hal::Std::ExplodeString(file, '/');
419 TString filename = vec[vec.size() - 1];
420 TString dDot = "../";
421 if (vec.size() > 2) { filename = dDot + vec[vec.size() - 2] + "/" + filename; }
422 TString helpText = "";
423 Color_t helpCol = kWhite;
424 switch (flag) {
425 case Hal::EInfo::kDebugInfo: {
426 helpText = "DEBUG ";
427 helpCol = kBlue;
428 } break;
429 case Hal::EInfo::kInfo: {
430 helpText = "INFO ";
431 helpCol = kCyan;
432 } break;
433 case Hal::EInfo::kLowWarning: {
434 helpText = "LOW WARN";
435 helpCol = kOrange;
436 } break;
437 case Hal::EInfo::kWarning: {
438 helpText = "WARNING ";
439 helpCol = kOrange;
440 } break;
441 case Hal::EInfo::kError: {
442 helpText = "ERROR ";
443 helpCol = kRed;
444 } break;
445 case Hal::EInfo::kCriticalError: {
446 helpText = "CRIT ERR";
447 helpCol = kRed;
448 } break;
449 }
450 TString colEn = Hal::Cout::GetColor(kWhite);
451 TString colRed = Hal::Cout::GetColor(kWhite);
452 TString colDis = Hal::Cout::GetDisableColor();
453 TString open = colEn + "[" + colDis;
454 TString close = colEn + "]" + colDis;
455
456 while (filename.Length() < 78) {
457 filename = filename + " ";
458 }
459
460
461 TString fileinfo = Form("[%8i][%s]", line, filename.Data());
462 fileinfo.ReplaceAll("[", open);
463 fileinfo.ReplaceAll("]", close);
464 std::cout << fileinfo << std::endl;
465 std::cout << Colored(helpText, helpCol) << text << std::endl;
466 }
467
468} // namespace Hal
static void Text(TString text, TString option="L", Color_t color=-1)
Definition Cout.cxx:92
static void PrintLineFileInfo(TString file, Int_t line, TString text, Hal::EInfo flag=Hal::EInfo::kDebugInfo)
Definition Cout.cxx:407
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
static void Stars(Color_t color=-1)
Definition Cout.cxx:150
static TString GetColor(Color_t Color)
Definition Cout.cxx:268
static void Empty(Color_t color=-1)
Definition Cout.cxx:207
static TString GetDisableColor()
Definition Cout.cxx:261
static void DebugInfo(Int_t i)
Definition Cout.cxx:249
static void Database(Int_t no...)
static void ShiftDatabase(Int_t shift, Int_t no...)
Definition Cout.cxx:317
static void FailSucced(TString value, TString flag, Color_t color)
Definition Cout.cxx:382
static Cout * Instance()
Definition Cout.cxx:400
static void ShiftInStars(Int_t shift, TString text, Color_t color=-1)
Definition Cout.cxx:355
static void SetVerboseMode(Hal::EInfo verbose)
Definition Cout.cxx:368
static void Percent(Double_t actual, Double_t total)
Definition Cout.cxx:219
static void ProgressBar(Double_t acutal, Double_t total)
Definition Cout.cxx:229
static void ShiftText(Int_t shift, TString text, TString option, Color_t color=-1)
Definition Cout.cxx:350