OpenVDB: Logger.h Source File

OpenVDB 8.0.0

Logger.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
11
12 #ifndef OPENVDB_AX_COMPILER_LOGGER_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_COMPILER_LOGGER_HAS_BEEN_INCLUDED
14
15 #include "../ast/AST.h"
16
17 #include <openvdb/version.h>
18
19 #include <functional>
20 #include <string>
21 #include <unordered_map>
22
23 class TestLogger;
24
25 namespace openvdb {
26 OPENVDB_USE_VERSION_NAMESPACE
27 namespace OPENVDB_VERSION_NAME {
28
29 namespace ax {
30
54 class Logger
55 {
56 public:
57 using Ptr = std::shared_ptr<Logger>;
58
59 using CodeLocation = std::pair<size_t, size_t>;
60 using OutputFunction = std::function<void(const std::string&)>;
61
66 Logger(const OutputFunction& errors =
67 [](const std::string& msg){
68 std::cerr << msg << std::endl;
69 },
70 const OutputFunction& warnings = [](const std::string&){});
71 ~Logger();
72
78 bool error(const std::string& message, const CodeLocation& lineCol);
79
85 bool error(const std::string& message, const ax::ast::Node* node);
86
91 bool warning(const std::string& message, const CodeLocation& lineCol);
92
98 bool warning(const std::string& message, const ax::ast::Node* node);
99
101
103 inline size_t errors() const { return mNumErrors; }
105 inline size_t warnings() const { return mNumWarnings; }
106
108 inline bool hasError() const { return this->errors() > 0; }
110 inline bool hasWarning() const { return this->warnings() > 0; }
112 inline bool atErrorLimit() const {
113 return this->getMaxErrors() > 0 && this->errors() >= this->getMaxErrors();
114 }
115
121 void clear();
122
125 void setWarningsAsErrors(const bool warnAsError = false);
127 bool getWarningsAsErrors() const;
128
132 void setMaxErrors(const size_t maxErrors = 0);
134 size_t getMaxErrors() const;
135
137
140 void setNumberedOutput(const bool numbered = true);
142 void setErrorPrefix(const char* prefix = "error: ");
144 void setWarningPrefix(const char* prefix = "warning: ");
148 void setPrintLines(const bool print = true);
149
151 bool getNumberedOutput() const;
153 const char* getErrorPrefix() const;
155 const char* getWarningPrefix() const;
157 bool getPrintLines() const;
158
162 void setSourceCode(const char* code);
163
167
175 void setSourceTree(openvdb::ax::ast::Tree::ConstPtr tree);
176
180 void addNodeLocation(const ax::ast::Node* node, const CodeLocation& location);
181
182 // forward declaration
183 struct Settings;
184 struct SourceCode;
185
186 private:
187
188 friend class ::TestLogger;
189
190 std::function<void(const std::string&)> mErrorOutput;
191 std::function<void(const std::string&)> mWarningOutput;
192
193 size_t mNumErrors;
194 size_t mNumWarnings;
195
196 std::unique_ptr<Settings> mSettings;
197
198 // components needed for verbose error info i.e. line/column numbers and
199 // lines from source code
200 std::unique_ptr<SourceCode> mCode;
201 ax::ast::Tree::ConstPtr mTreePtr;
202 std::unordered_map<const ax::ast::Node*, CodeLocation> mNodeToLineColMap;
203 };
204
205 } // namespace ax
206 } // namespace OPENVDB_VERSION_NAME
207 } // namespace openvdb
208
209 #endif // OPENVDB_AX_COMPILER_LOGGER_HAS_BEEN_INCLUDED
210
openvdb::v8_2::ax::Logger::addNodeLocation
void addNodeLocation(const ax::ast::Node *node, const CodeLocation &location)
Add a node to the code location map.
openvdb::v8_2::ax::Logger::setWarningPrefix
void setWarningPrefix(const char *prefix="warning: ")
Set a prefix for each warning message.
openvdb::v8_2::ax::Logger::getMaxErrors
size_t getMaxErrors() const
Returns the number of allowed errors.
openvdb::v8_2::ax::Logger::error
bool error(const std::string &message, const CodeLocation &lineCol)
Log a compiler error and its offending code location.
openvdb::v8_2::ax::Logger::hasWarning
bool hasWarning() const
Returns true if a warning has been found, false otherwise.
Definition: Logger.h:110
openvdb::v8_2::ax::Logger::getWarningsAsErrors
bool getWarningsAsErrors() const
Returns if warning are promoted to errors.
openvdb::v8_2::ax::Logger::getErrorPrefix
const char * getErrorPrefix() const
Returns the prefix for each error message.
openvdb::v8_2::ax::Logger::errors
size_t errors() const
Returns the number of errors that have been encountered.
Definition: Logger.h:103
openvdb::v8_2::ax::Logger::CodeLocation
std::pair< size_t, size_t > CodeLocation
Definition: Logger.h:59
openvdb::v8_2::ax::Logger::warning
bool warning(const std::string &message, const ax::ast::Node *node)
Log a compiler warning using the offending AST node. Used in AST traversal.
openvdb::v8_2::ax::Logger::getNumberedOutput
bool getNumberedOutput() const
Returns whether the messages will be numbered.
openvdb::v8_2::ax::Logger::warnings
size_t warnings() const
Returns the number of warnings that have been encountered.
Definition: Logger.h:105
openvdb::v8_2::ax::Logger::Logger
Logger(const OutputFunction &errors=[](const std::string &msg){ std::cerr<< msg<< std::endl;}, const OutputFunction &warnings=[](const std::string &){})
Construct a Logger with optional error and warning output functions, defaults stream errors to std::c...
version.h
Library and file format version numbers.
openvdb::v8_2::ax::Logger::setNumberedOutput
void setNumberedOutput(const bool numbered=true)
Error/warning formatting options.
openvdb::v8_2::ax::Logger::atErrorLimit
bool atErrorLimit() const
Returns true if it has errored and the max errors has been hit.
Definition: Logger.h:112
openvdb::v8_2::ax::Logger::OutputFunction
std::function< void(const std::string &)> OutputFunction
Definition: Logger.h:60
openvdb::v8_2::ax::ast::Tree::ConstPtr
std::shared_ptr< const Tree > ConstPtr
Definition: AST.h:564
openvdb::v8_2::ax::Logger::setPrintLines
void setPrintLines(const bool print=true)
Set whether the output should include the offending line of code.
openvdb::v8_2::ax::Logger::warning
bool warning(const std::string &message, const CodeLocation &lineCol)
Log a compiler warning and its offending code location.
openvdb::v8_2::ax::ast::Node
The base abstract node which determines the interface and required methods for all derived concrete n...
Definition: AST.h:102
openvdb::v8_2::ax::Logger::getWarningPrefix
const char * getWarningPrefix() const
Returns the prefix for each warning message.
openvdb::v8_2::ax::Logger::~Logger
~Logger()
openvdb::v8_2::ax::Logger::Ptr
std::shared_ptr< Logger > Ptr
Definition: Logger.h:57
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153
openvdb::v8_2::ax::Logger::setWarningsAsErrors
void setWarningsAsErrors(const bool warnAsError=false)
Set any warnings that are encountered to be promoted to errors.
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
openvdb::v8_2::ax::Logger::setSourceCode
void setSourceCode(const char *code)
Set the source code that lines can be printed from if an error or warning is raised.
openvdb::v8_2::ax::Logger::hasError
bool hasError() const
Returns true if an error has been found, false otherwise.
Definition: Logger.h:108
openvdb::v8_2::ax::Logger
Logger for collecting errors and warnings that occur during AX compilation.
Definition: Logger.h:55
openvdb::v8_2::ax::Logger::setSourceTree
void setSourceTree(openvdb::ax::ast::Tree::ConstPtr tree)
Set the AST source tree which will be used as reference for the locations of nodes when resolving lin...
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_2::ax::Logger::getPrintLines
bool getPrintLines() const
Returns whether the messages will include the line of offending code.
openvdb::v8_2::ax::Logger::clear
void clear()
Clear the tree-code mapping and reset the number of errors/warnings.
openvdb::v8_2::ax::Logger::setMaxErrors
void setMaxErrors(const size_t maxErrors=0)
Sets the maximum number of errors that are allowed before compilation should exit.
openvdb::v8_2::ax::Logger::setErrorPrefix
void setErrorPrefix(const char *prefix="error: ")
Set a prefix for each error message.
openvdb::v8_2::ax::Logger::error
bool error(const std::string &message, const ax::ast::Node *node)
Log a compiler error using the offending AST node. Used in AST traversal.
openvdb::v8_2::ax::ast::print
void print(const ast::Node &node, const bool numberStatements=true, std::ostream &os=std::cout, const char *indent=" ")
Writes a descriptive printout of a Node hierarchy into a target stream.