OpenVDB  8.0.1
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 {
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&){});
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 
Logger for collecting errors and warnings that occur during AX compilation.
Definition: Logger.h:55
size_t warnings() const
Returns the number of warnings that have been encountered.
Definition: Logger.h:105
void setWarningPrefix(const char *prefix="warning: ")
Set a prefix for each warning message.
void addNodeLocation(const ax::ast::Node *node, const CodeLocation &location)
Add a node to the code location map.
std::function< void(const std::string &)> OutputFunction
Definition: Logger.h:60
bool atErrorLimit() const
Returns true if it has errored and the max errors has been hit.
Definition: Logger.h:112
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...
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...
bool getPrintLines() const
Returns whether the messages will include the line of offending code.
std::shared_ptr< Logger > Ptr
Definition: Logger.h:57
size_t errors() const
Returns the number of errors that have been encountered.
Definition: Logger.h:103
void setNumberedOutput(const bool numbered=true)
Error/warning formatting options.
bool warning(const std::string &message, const ax::ast::Node *node)
Log a compiler warning using the offending AST node. Used in AST traversal.
bool hasError() const
Returns true if an error has been found, false otherwise.
Definition: Logger.h:108
bool error(const std::string &message, const ax::ast::Node *node)
Log a compiler error using the offending AST node. Used in AST traversal.
bool getNumberedOutput() const
Returns whether the messages will be numbered.
void setMaxErrors(const size_t maxErrors=0)
Sets the maximum number of errors that are allowed before compilation should exit.
void setWarningsAsErrors(const bool warnAsError=false)
Set any warnings that are encountered to be promoted to errors.
const char * getWarningPrefix() const
Returns the prefix for each warning message.
void setPrintLines(const bool print=true)
Set whether the output should include the offending line of code.
bool warning(const std::string &message, const CodeLocation &lineCol)
Log a compiler warning and its offending code location.
bool hasWarning() const
Returns true if a warning has been found, false otherwise.
Definition: Logger.h:110
bool getWarningsAsErrors() const
Returns if warning are promoted to errors.
void setErrorPrefix(const char *prefix="error: ")
Set a prefix for each error message.
void clear()
Clear the tree-code mapping and reset the number of errors/warnings.
const char * getErrorPrefix() const
Returns the prefix for each error message.
size_t getMaxErrors() const
Returns the number of allowed errors.
std::pair< size_t, size_t > CodeLocation
Definition: Logger.h:59
bool error(const std::string &message, const CodeLocation &lineCol)
Log a compiler error and its offending code location.
void setSourceCode(const char *code)
Set the source code that lines can be printed from if an error or warning is raised.
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.
Definition: openvdb/Exceptions.h:13
The base abstract node which determines the interface and required methods for all derived concrete n...
Definition: AST.h:102
std::shared_ptr< const Tree > ConstPtr
Definition: AST.h:564
Library and file format version numbers.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153