Peano
Loading...
Searching...
No Matches
LogFilterFileReader.cpp
Go to the documentation of this file.
3
4#include <fstream>
5#include <stdlib.h>
6#include <string>
7
9
10tarch::logging::Log tarch::logging::LogFilterFileReader::_log( "tarch::logging::LogFilterFileReader" );
11
12bool tarch::logging::LogFilterFileReader::interpretTokens( const std::string& levelToken, const std::string& classNameToken, const std::string& rankToken, const std::string& onOffToken, const std::string& phaseToken ) {
13 bool result = true;
14
15 if (
17 and
19 and
21 and
23 ) {
24 logError( "interpretTokens(...)", "expected \"debug\", \"info\", \"all\" or \"trace\" but got " << levelToken );
25 result = false;
26 }
27
28 int rank = rankToken=="-1" ? -1 : atoi( rankToken.c_str() );
29 if ( rank<-1 ) {
30 logError( "interpretTokens(...)", "expected positive rank or -1 for all ranks but got " << rankToken );
31 result = false;
32 }
33
34 bool isFilter = onOffToken=="black";
35 if (onOffToken.compare("black")!=0 && onOffToken.compare("white")!=0) {
36 logError( "interpretTokens(...)", "expected \"black\" or \"white\" but got \"" << onOffToken << "\"");
37 result = false;
38 }
39
40 if (result) {
42 levelToken, rank, classNameToken, isFilter,
43 phaseToken
44 );
46 }
47
48 return result;
49}
50
51bool tarch::logging::LogFilterFileReader::parseLine(std::ifstream& file, const std::string& filename, const std::string& line, int linenumber) {
52 std::string lineWithNewline = line + '\n';
53
54 //Loop through line
55 int characterPosition = 0;
56 const int NumberOfTokensPerLine = 5;
57 std::string tokens[NumberOfTokensPerLine];
58 int currentToken = 0;
59 while (currentToken<NumberOfTokensPerLine && !file.eof() && characterPosition < static_cast<int>(lineWithNewline.length())) {
60 char buffer = lineWithNewline[characterPosition];
61
62 const bool bufferIsWhiteSpace =
63 buffer == ' ' ||
64 buffer == '\t' ||
65 buffer == '\r' ||
66 buffer == '\n';
67 if (bufferIsWhiteSpace && !tokens[currentToken].empty()) {
68 currentToken++;
69 }
70 if (!bufferIsWhiteSpace) {
71 tokens[currentToken] += buffer;
72 }
73
74 characterPosition++;
75 }
76
77 if (currentToken == NumberOfTokensPerLine-1) {
78 logWarning( "parsePlainTextFile(string)", "legacy filter file. Please add program phase (default:" << LogFilter::FilterListEntry::AlwaysOn << ") as last argument" );
80 currentToken++;
81 }
82
83 if (currentToken != NumberOfTokensPerLine) {
84 logError( "parsePlainTextFile(string)", "syntax error in input file " << filename << ", line " << linenumber << ":" << line );
85 return false;
86 } else {
87 bool result = interpretTokens(tokens[0],tokens[1],tokens[2],tokens[3],tokens[4]);
88 if (not result) {
89 logError( "parsePlainTextFile(string)", "syntax error in input file " << filename << ", line " << linenumber << ":" << line );
90 }
91 return result;
92 }
93}
94
95std::string tarch::logging::LogFilterFileReader::trimLine( const std::string& line ) {
96 std::size_t first = line.find_first_not_of(' ');
97 if (first == std::string::npos)
98 return "";
99 std::size_t last = line.find_last_not_of(' ');
100 return line.substr(first, (last-first+1));
101}
102
104 bool result = true;
105
107
111 "",
114 ));
115
116 std::ifstream file;
117 file.open(filename.c_str()); // open a file
118 if (!file.good()) {
119 logWarning( "parsePlainTextFile(string)", "was not able to open input file " << filename );
120 result = false;
121 }
122
123 int linenumber = 1;
124 std::string line;
125 while (!file.eof() && result) {
126 std::getline(file, line);
127 line = trimLine(line);
128 if(line.length() > 0 && line[0]!='#') {
129 result = parseLine(file, filename, line, linenumber);
130 }
131
132 linenumber++;
133 }
134
135 if (!result) {
136 logWarning( "parsePlainTextFile(string)", "filter file " << filename << " was invalid. Switch on all log statements" );
139 }
140
141 return result;
142}
#define logError(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:464
#define logWarning(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:440
static std::string trimLine(const std::string &line)
static bool parsePlainTextFile(const std::string &filename)
Tries to parse a simple text file where each line contains an entry.
static bool interpretTokens(const std::string &levelToken, const std::string &classNameToken, const std::string &rankToken, const std::string &onOffToken, const std::string &phaseToken)
static bool parseLine(std::ifstream &file, const std::string &filename, const std::string &line, int linenumber)
void addFilterListEntry(const FilterListEntry &entry)
Add one filter list entry.
static LogFilter & getInstance()
Log Device.
Definition Log.h:516
Represents one entry of the filter list.
Definition LogFilter.h:30
static const std::string TargetTrace
Definition LogFilter.h:36
static const std::string TargetDebug
Definition LogFilter.h:35
static const std::string TargetInfo
Definition LogFilter.h:34
static const std::string TargetAll
Definition LogFilter.h:33
static const std::string AlwaysOn
Definition LogFilter.h:39