ErrorLogger.cc
Go to the documentation of this file.
1 #include "utl/ErrorLogger.h"
2 #include <config.h>
3 
4 #include <string>
5 #include <vector>
6 #include <iostream>
7 #include <sstream>
8 #include <cstdio>
9 #include <unistd.h>
10 
11 using namespace utl;
12 using namespace std;
13 
14 
16  fBaseDirLength(string(__FILE__).size() - string("Utilities/ErrorLogger/ErrorLogger.cc").size()),
17  fColorOutput(isatty(fileno(stderr)))
18 { }
19 
20 
21 string
23  const
24 {
25  /*
26  VT100 console control codes for color:
27 
28  <ESC>[{attr1};...;{attrn}m
29 
30  The following lists standard attributes:
31 
32  0 Reset all attributes
33  1 Bright
34  2 Dim
35  4 Underscore
36  5 Blink
37  7 Reverse
38  8 Hidden
39 
40  Foreground Colours
41  30 Black
42  31 Red
43  32 Green
44  33 Yellow
45  34 Blue
46  35 Magenta
47  36 Cyan
48  37 White
49 
50  Background Colours
51  40 Black
52  41 Red
53  42 Green
54  43 Yellow
55  44 Blue
56  45 Magenta
57  46 Cyan
58  47 White
59  */
60  if (HasColor()) {
61  const char* const names[] = {
62  "\033[1;35m[DEBUG]\033[0m",
63  "\033[1;34m[INFO]\033[0m",
64  "\033[1;36m[WARN]\033[0m",
65  "\033[1;31m[ERROR]\033[0m",
66  "\033[1;31m[FATAL]\033[0m"
67  };
68  return names[severity - eMinSeverity];
69  } else {
70  const char* const names[] = {
71  "[DEBUG]",
72  "[INFO]",
73  "[WARN]",
74  "[ERROR]",
75  "[FATAL]"
76  };
77  return names[severity - eMinSeverity];
78  }
79 }
80 
81 
88 void
90  const string& functionName,
91  const string& fileName,
92  const int lineNumber,
93  const string& message,
94  EVerbosity verbosity,
95  bool stripBaseDir)
96  const
97 {
98  if (severity < fMinSeverity)
99  return;
100 
101  if (verbosity == Verbosity::eDefault)
102  verbosity = fVerbosity;
103 
104  ostringstream temp;
105  if (verbosity >= Verbosity::eSeverity) {
106  temp << GetSeverityName(severity) << ' ';
107  }
108  if (verbosity >= Verbosity::eFileInfo) {
109  temp << ((stripBaseDir && fBaseDirLength < fileName.size()) ?
110  fileName.substr(fBaseDirLength, fileName.size()) : fileName)
111  << ':' << lineNumber << ": ";
112  }
113  if (verbosity >= Verbosity::eFunction)
114  temp << functionName << ": ";
115 
116  if (verbosity >= Verbosity::eTerse)
117  temp << message << endl;
118  const string line = temp.str();
119 
120  ostream& output = fOStream ? *fOStream : cerr;
121 
122  output << line;
123 
124  if (eError <= severity)
125  ++fListOfErrorMessages[line];
126 }
127 
128 
129 void
131 {
132  if (!fListOfErrorMessages.empty()) {
133  ostream& output = fOStream ? *fOStream : cerr;
134  if (HasColor())
135  output << "\n\033[1;36mSummary of previous errors:\033[0m\n";
136  else
137  output << "\nSummary of previous errors:\n";
138  const int maxLines = 50;
139  int nLines = 0;
140  for (const auto& mn : fListOfErrorMessages) {
141  const auto& message = mn.first;
142  const auto& count = mn.second;
143  output << "\033[1;36m" << count << "x\033[0m " << message;
144  ++nLines;
145  if (nLines >= maxLines) {
146  WARNING("... too long ...");
147  ostringstream warn;
148  warn << "List of previous errors was truncated at the " << nLines << " lines "
149  "out of total " << fListOfErrorMessages.size();
150  WARNING(warn);
151  break;
152  }
153  }
154  }
155 }
void Log(const ESeverityLevel severity, const std::string &functionName, const std::string &fileName, const int lineNumber, const std::string &message, EVerbosity verbosity=Verbosity::eDefault, bool stripBaseDir=true) const
General interface for logging a message.
Definition: ErrorLogger.cc:89
Include filename and line number.
Definition: Verbosity.h:16
ESeverityLevel fMinSeverity
Definition: ErrorLogger.h:117
void WriteErrorMessagesToStream()
Definition: ErrorLogger.cc:130
std::map< std::string, int > fListOfErrorMessages
Definition: ErrorLogger.h:119
Include function or facility name.
Definition: Verbosity.h:14
bool HasColor() const
Definition: ErrorLogger.h:102
terse output
Definition: Verbosity.h:13
unsigned int fBaseDirLength
Definition: ErrorLogger.h:115
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
std::ostream * fOStream
Current stream for logging messages.
Definition: ErrorLogger.h:113
Report severity level of message.
Definition: Verbosity.h:15
EVerbosity fVerbosity
Definition: ErrorLogger.h:114
ESeverityLevel
Message severity levels.
Definition: ErrorLogger.h:40
std::string GetSeverityName(const ESeverityLevel severity) const
Get string name for severity level.
Definition: ErrorLogger.cc:22
use default verbosity from error logger
Definition: Verbosity.h:11
Error message.
Definition: ErrorLogger.h:45

, generated on Tue Sep 26 2023.