OpenVDB  8.1.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 {
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 
79  bool error(const std::string& message, const CodeLocation& lineCol = CodeLocation(0,0));
80 
86  bool error(const std::string& message, const ax::ast::Node* node);
87 
94  bool warning(const std::string& message, const CodeLocation& lineCol = CodeLocation(0,0));
95 
101  bool warning(const std::string& message, const ax::ast::Node* node);
102 
104 
106  inline size_t errors() const { return mNumErrors; }
108  inline size_t warnings() const { return mNumWarnings; }
109 
111  inline bool hasError() const { return this->errors() > 0; }
113  inline bool hasWarning() const { return this->warnings() > 0; }
115  inline bool atErrorLimit() const {
116  return this->getMaxErrors() > 0 && this->errors() >= this->getMaxErrors();
117  }
118 
124  void clear();
125 
128  void setWarningsAsErrors(const bool warnAsError = false);
130  bool getWarningsAsErrors() const;
131 
135  void setMaxErrors(const size_t maxErrors = 0);
137  size_t getMaxErrors() const;
138 
140 
143  void setNumberedOutput(const bool numbered = true);
145  void setIndent(const size_t ident = 0);
147  void setErrorPrefix(const char* prefix = "error: ");
149  void setWarningPrefix(const char* prefix = "warning: ");
153  void setPrintLines(const bool print = true);
154 
156  bool getNumberedOutput() const;
158  size_t getIndent() const;
160  const char* getErrorPrefix() const;
162  const char* getWarningPrefix() const;
164  bool getPrintLines() const;
165 
169  void setSourceCode(const char* code);
170 
174 
182  void setSourceTree(openvdb::ax::ast::Tree::ConstPtr tree);
183 
187  void addNodeLocation(const ax::ast::Node* node, const CodeLocation& location);
188 
189  // forward declaration
190  struct Settings;
191  struct SourceCode;
192 
193 private:
194 
195  friend class ::TestLogger;
196 
197  std::function<void(const std::string&)> mErrorOutput;
198  std::function<void(const std::string&)> mWarningOutput;
199 
200  size_t mNumErrors;
201  size_t mNumWarnings;
202 
203  std::unique_ptr<Settings> mSettings;
204 
205  // components needed for verbose error info i.e. line/column numbers and
206  // lines from source code
207  std::unique_ptr<SourceCode> mCode;
208  ax::ast::Tree::ConstPtr mTreePtr;
209  std::unordered_map<const ax::ast::Node*, CodeLocation> mNodeToLineColMap;
210 };
211 
212 } // namespace ax
213 } // namespace OPENVDB_VERSION_NAME
214 } // namespace openvdb
215 
216 #endif // OPENVDB_AX_COMPILER_LOGGER_HAS_BEEN_INCLUDED
217 
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178
openvdb::v8_1::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.
openvdb::v8_1::ax::Logger::hasWarning
bool hasWarning() const
Returns true if a warning has been found, false otherwise.
Definition: Logger.h:113
openvdb::v8_1::ax::Logger::errors
size_t errors() const
Returns the number of errors that have been encountered.
Definition: Logger.h:106
openvdb::v8_1::ax::ast::Tree::ConstPtr
std::shared_ptr< const Tree > ConstPtr
Definition: AST.h:564
openvdb::v8_1::ax::Logger::CodeLocation
std::pair< size_t, size_t > CodeLocation
Definition: Logger.h:59
openvdb::v8_1::ax::Logger::warnings
size_t warnings() const
Returns the number of warnings that have been encountered.
Definition: Logger.h:108
openvdb::v8_1::ax::Logger::OutputFunction
std::function< void(const std::string &)> OutputFunction
Definition: Logger.h:60
openvdb::v8_1::ax::Logger::atErrorLimit
bool atErrorLimit() const
Returns true if it has errored and the max errors has been hit.
Definition: Logger.h:115
openvdb::v8_1::ax::ast::Node
The base abstract node which determines the interface and required methods for all derived concrete n...
Definition: AST.h:101
openvdb::v8_1::ax::Logger
Logger for collecting errors and warnings that occur during AX compilation.
Definition: Logger.h:54
openvdb::v8_1::ax::Logger::Ptr
std::shared_ptr< Logger > Ptr
Definition: Logger.h:57
openvdb::v8_1::ax::Logger::hasError
bool hasError() const
Returns true if an error has been found, false otherwise.
Definition: Logger.h:111
openvdb
Definition: openvdb/Exceptions.h:13