OpenVDB: Logger Class Reference

OpenVDB 12.1.0

Loading...
Searching...
No Matches
Logger Class Reference

Logger for collecting errors and warnings that occur during AX compilation. More...

`#include <openvdb_ax/compiler/Logger.h>`

## Public Types
using | Ptr = std::shared_ptr<Logger>
using | CodeLocation = std::pair<size_t, size_t>
using | OutputFunction = std::function<void(const std::string&)>

## Public Member Functions
| 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::cerr and suppress warnings.
| ~Logger ()
bool | error (const std::string &message, const CodeLocation &lineCol=CodeLocation(0, 0))
| Log a compiler error and its offending code location. If the offending location is (0,0), the message is treated as not having an associated code location.
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 | warning (const std::string &message, const CodeLocation &lineCol=CodeLocation(0, 0))
| Log a compiler warning and its offending code location. If the offending location is (0,0), the message is treated as not having an associated code location.
bool | warning (const std::string &message, const ax::ast::Node *node)
| Log a compiler warning using the offending AST node. Used in AST traversal.
size_t | errors () const
| Returns the number of errors that have been encountered.
size_t | warnings () const
| Returns the number of warnings that have been encountered.
bool | hasError () const
| Returns true if an error has been found, false otherwise.
bool | hasWarning () const
| Returns true if a warning has been found, false otherwise.
bool | atErrorLimit () const
| Returns true if it has errored and the max errors has been hit.
void | clear ()
| Clear the tree-code mapping and reset the number of errors/warnings.
void | setWarningsAsErrors (const bool warnAsError=false)
| Set any warnings that are encountered to be promoted to errors.
bool | getWarningsAsErrors () const
| Returns if warning are promoted to errors.
void | setMaxErrors (const size_t maxErrors=0)
| Sets the maximum number of errors that are allowed before compilation should exit.
size_t | getMaxErrors () const
| Returns the number of allowed errors.
void | setNumberedOutput (const bool numbered=true)
| Error/warning formatting options.
void | setIndent (const size_t ident=0)
| Number of spaces to indent every new line before the message is formatted.
void | setErrorPrefix (const char *prefix="error: ")
| Set a prefix for each warning message.
void | setWarningPrefix (const char *prefix="warning: ")
| Set a prefix for each warning message.
void | setPrintLines (const bool print=true)
| Set whether the output should include the offending line of code.
bool | getNumberedOutput () const
| Returns whether the messages will be numbered.
size_t | getIndent () const
| Returns the number of spaces to be printed before every new line.
const char * | getErrorPrefix () const
| Returns the prefix for each error message.
const char * | getWarningPrefix () const
| Returns the prefix for each warning message.
bool | getPrintLines () const
| Returns whether the messages will include the line of offending code.
void | setSourceCode (const char *code)
| Set the source code that lines can be printed from if an error or warning is raised.
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 line and column numbers during AST traversal.
void | addNodeLocation (const ax::ast::Node *node, const CodeLocation &location)
| Add a node to the code location map.

## Friends
struct | ::TestLoggerAcc

## Detailed Description

Logger for collecting errors and warnings that occur during AX compilation.

Error and warning output can be customised using the function pointer arguments. These require a function that takes the formatted error or warning string and handles the output, returning void. e.g. void streamCerr(const std::string& message) { std::cerr << message << std::endl; }

The Logger handles formatting of messages, tracking of number of errors or warnings and retrieval of errored lines of code to be printed if needed. Use of the Logger to track new errors or warnings can be done either with the line/column numbers directly (e.g during lexing and parsing where the code is being iterated through) or referring to the AST node using its position in the Tree (e.g. during codegen where only the AST node is known directly, not the corresponding line/column numbers). To find the line or column numbers for events logged using AST nodes, the Logger stores a map of Node* to line and column numbers. This must be populated e.g. during parsing, to allow resolution of code locations when they are not explicitly available. The Logger also stores a pointer to the AST Tree that these nodes belong to and the code used to create it.

Warning
  The logger is not thread safe. A unique instance of the Logger should be used for unique invocations of ax pipelines.

## Member Typedef Documentation

## ◆ CodeLocation

using CodeLocation = std::pair<size_t, size_t>

## ◆ OutputFunction

using OutputFunction = std::function<void(const std::string&)>

## ◆ Ptr

using Ptr = std::shared_ptr<Logger>

## Constructor & Destructor Documentation

## ◆ 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::cerr and suppress warnings.

Parameters

  errors | Optional error output function
  warnings | Optional warning output function

## ◆ ~Logger()

~Logger | ( |  | ) |

## Member Function Documentation

## ◆ addNodeLocation()

void addNodeLocation | ( | const ax::ast::Node * | node,
|  | const CodeLocation & | location )

Add a node to the code location map.

Parameters

  node | Pointer to AST node
  location | Line and column number in code

## ◆ atErrorLimit()

bool atErrorLimit | ( |  | ) | const
| inline

Returns true if it has errored and the max errors has been hit.

## ◆ clear()

void clear | ( |  | ) |

Clear the tree-code mapping and reset the number of errors/warnings.

Note
  The tree-code mapping must be repopulated to retrieve line and column numbers during AST traversal i.e. code generation. The openvdb::ax::ast::parse() function does this for a given input code string.

## ◆ error() [1/2]

bool error | ( | const std::string & | message,
|  | const ax::ast::Node * | node )

Log a compiler error using the offending AST node. Used in AST traversal.

Parameters

  message | The error message
  node | The offending AST node causing the error

Returns
  true if can continue to capture future messages.

## ◆ error() [2/2]

bool error | ( | const std::string & | message,
|  | const CodeLocation & | lineCol = CodeLocation(0, 0) )

Log a compiler error and its offending code location. If the offending location is (0,0), the message is treated as not having an associated code location.

Parameters

  message | The error message
  lineCol | The line/column number of the offending code

Returns
  true if can continue to capture future messages.

## ◆ errors()

size_t errors | ( |  | ) | const
| inline

Returns the number of errors that have been encountered.

## ◆ getErrorPrefix()

const char * getErrorPrefix | ( |  | ) | const

Returns the prefix for each error message.

## ◆ getIndent()

size_t getIndent | ( |  | ) | const

Returns the number of spaces to be printed before every new line.

## ◆ getMaxErrors()

size_t getMaxErrors | ( |  | ) | const

Returns the number of allowed errors.

## ◆ getNumberedOutput()

bool getNumberedOutput | ( |  | ) | const

Returns whether the messages will be numbered.

## ◆ getPrintLines()

bool getPrintLines | ( |  | ) | const

Returns whether the messages will include the line of offending code.

## ◆ getWarningPrefix()

const char * getWarningPrefix | ( |  | ) | const

Returns the prefix for each warning message.

## ◆ getWarningsAsErrors()

bool getWarningsAsErrors | ( |  | ) | const

Returns if warning are promoted to errors.

## ◆ hasError()

bool hasError | ( |  | ) | const
| inline

Returns true if an error has been found, false otherwise.

## ◆ hasWarning()

bool hasWarning | ( |  | ) | const
| inline

Returns true if a warning has been found, false otherwise.

## ◆ setErrorPrefix()

void setErrorPrefix | ( | const char * | prefix = "error: " | ) |

Set a prefix for each warning message.

## ◆ setIndent()

void setIndent | ( | const size_t | ident = 0 | ) |

Number of spaces to indent every new line before the message is formatted.

## ◆ setMaxErrors()

void setMaxErrors | ( | const size_t | maxErrors = 0 | ) |

Sets the maximum number of errors that are allowed before compilation should exit.

Note
  The logger will continue to increment the error counter beyond this value but, once reached, it will not invoke the error callback.

Parameters

  maxErrors | The number of allowed errors

## ◆ setNumberedOutput()

void setNumberedOutput | ( | const bool | numbered = true | ) |

Error/warning formatting options.

Set whether the output should number the errors/warnings

Parameters

  numbered | If true, messages will be numbered

## ◆ setPrintLines()

void setPrintLines | ( | const bool | print = true | ) |

Set whether the output should include the offending line of code.

Parameters

  print | If true, offending lines of code will be appended to the output message

## ◆ setSourceCode()

void setSourceCode | ( | const char * | code | ) |

Set the source code that lines can be printed from if an error or warning is raised.

Parameters

  code | The AX code as a c-style string

## ◆ 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 line and column numbers during AST traversal.

These functions are only to be used during parsing to allow line and column number retrieval during later stages of compilation when working solely with an AST

Note
  To be used just by ax::parse before any AST modifications to ensure traversal of original source tree is possible, when adding messages using Node* which may correspond to modified trees

Parameters

  tree | Pointer to const AST

## ◆ setWarningPrefix()

void setWarningPrefix | ( | const char * | prefix = "warning: " | ) |

Set a prefix for each warning message.

## ◆ setWarningsAsErrors()

void setWarningsAsErrors | ( | const bool | warnAsError = false | ) |

Set any warnings that are encountered to be promoted to errors.

Parameters

  warnAsError | If true, warnings will be treated as errors

## ◆ warning() [1/2]

bool warning | ( | const std::string & | message,
|  | const ax::ast::Node * | node )

Log a compiler warning using the offending AST node. Used in AST traversal.

Parameters

  message | The warning message
  node | The offending AST node causing the warning

Returns
  true if can continue to capture future messages.

## ◆ warning() [2/2]

bool warning | ( | const std::string & | message,
|  | const CodeLocation & | lineCol = CodeLocation(0, 0) )

Log a compiler warning and its offending code location. If the offending location is (0,0), the message is treated as not having an associated code location.

Parameters

  message | The warning message
  lineCol | The line/column number of the offending code

Returns
  true if can continue to capture future messages.

## ◆ warnings()

size_t warnings | ( |  | ) | const
| inline

Returns the number of warnings that have been encountered.

## Friends And Related Symbol Documentation

## ◆ ::TestLoggerAcc

friend struct ::TestLoggerAcc
| friend