ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
clockwerk::SimLogger Class Reference

Class for logging data to a file. More...

#include <SimLogger.h>

Inheritance diagram for clockwerk::SimLogger:
Collaboration diagram for clockwerk::SimLogger:

Classes

struct  Group
 Grouping methods – for some variables, such as arrays and vectors, we may want to lump all of the data elements together, even though they are tracked/"logged" as separate native datatypes. The following struct and associated tracking variables provide a mechanism for doing that. More...
 

Public Member Functions

 SimLogger (Executive &executive, const std::string &filename="output.csv", unsigned int buffer_size=1)
 Constructor for the SimLogger object – has a default rate of every ms.
 
unsigned int bufferSize ()
 Functions to get and set buffer size.
 
int bufferSize (unsigned int buffer_size)
 
std::string filename ()
 Functions to get and set filename.
 
int filename (const std::string &name)
 
std::string outDir ()
 Functions to get and set output directory.
 
int outDir (const std::string &directory)
 
virtual int setup (const std::string &file_name)
 Function to set up the SimLogger.
 
int addParameter (GraphTreeObject &parameter, const std::string &param_name, bool recursive=false)
 Function to add a logging parameter to the SimLogger. Overloaded to add graph node by string or by object.
 
int addParameter (const std::string &parameter, const std::string &param_name, bool recursive=false)
 Function to add a logging parameter to the SimLogger. Overloaded to add graph node by string or by object.
 
virtual int lockForLogging ()
 Function to lock the file for logging once all parameters have been defined. Generates headers and creates buffers for logging based on parameters that have already been added above.
 
virtual int log ()
 Function to log data to buffer. Flushes buffer to file when full.
 
virtual int close ()
 Function to close down the file – logs remaining buffered data and closes.
 
int writeHeaders (const int &val)
 
int writeHeaders (const unsigned int &val)
 
int writeHeaders (const double &val)
 
int writeHeaders (const GraphTreeObject &val)
 
int writeHeaders (void *val)
 
int writeHeaders (const std::string &val)
 
template<typename T >
int writeHeaders (const std::vector< T > &val)
 
template<typename T , long unsigned int N>
int writeHeaders (const std::array< T, N > &val)
 
template<typename T , unsigned int R, unsigned int C>
int writeHeaders (const Matrix< T, R, C > &val)
 
int writeToBuffer (const bool &val)
 Overloaded function to write data to the buffer. Should not be called directly by the user – is reserved for call within graph object providing the logging.
 
int writeToBuffer (const int &val)
 
int writeToBuffer (const long unsigned int &val)
 
int writeToBuffer (const float &val)
 
int writeToBuffer (const double &val)
 
int writeToBuffer (const std::string &val)
 
int writeToBuffer (void *val)
 
int writeToBuffer (GraphTreeObject &val)
 
template<typename T >
int writeToBuffer (const std::vector< T > &val)
 Function to handle iteratable values – note here that we only need to define one templated instance because this function will be fully templated out to all DataIO objects that are instantiated, and calls the above ^ under the hood.
 
template<typename T , long unsigned int N>
int writeToBuffer (const std::array< T, N > &val)
 
template<typename T , unsigned int R, unsigned int C>
int writeToBuffer (const Matrix< T, R, C > &val)
 
void logLevel (log_level_e new_level)
 Function to set the local model log level.
 
GraphTreeObjectparent ()
 Functions to get object's parent/children.
 
int parent (GraphTreeObject *new_parent)
 Function to assign the node's parent via pointer.
 
int parent (GraphTreeObject &new_parent)
 Function to assign the node's parent via reference.
 
std::vector< GraphTreeObject * > & children ()
 
unsigned int nChildren ()
 Getters for number of children and descendants.
 
unsigned int nDescendants ()
 
std::string name () const
 Getter and setter for object name.
 
int name (const std::string &new_name)
 
int rank ()
 Getter for the object rank.
 
virtual int type ()
 Function to indicate type – -1 by default unless implemented downstream.
 
bool loggable ()
 Getter for object logability flag – note no setter because should only be set by this or a derived class in its definition.
 
std::string address ()
 Getter for object's string address.
 
std::vector< std::string > search (const std::string &address)
 Function to search through the graph tree by string address.
 
GraphTreeObjectgetByAddress (const std::string &address)
 Function to get an object from the graph tree by string address.
 
void dump (bool recursive=false)
 Function to dump the graph node, optionally including descendents.
 
void graphNodeInfo ()
 Function to return all info associated with a given graph node.
 
void recurseGraphNodeInfo (unsigned int counter)
 Function to recurse through graph tree and print all node info.
 
virtual int header (void *logger)
 Function to pass headers for this object to the logger.
 
virtual std::vector< std::string > header_info () const
 
void findLoggable (std::vector< GraphTreeObject * > &matches)
 Function to find all loggable parameters in this node and its children.
 
virtual int log (void *logger)
 Function to log data to a logger.
 
void lock (bool recursive=true)
 Function to lock the graph tree object and its children to changes.
 
void unlock (bool recursive=true)
 Function to unlock the graph tree and its children for changes.
 
bool locked ()
 Function to return whether graph tree object is locked.
 

Public Attributes

Executiveexc
 Handle to the executive object – used for event logging, etc.
 

Protected Member Functions

void _fillBuffers ()
 Function to set all buffers to full size.
 
virtual int _createSetupFile ()
 Function to create a file, in whatever format is chosen, for logging.
 
virtual int _writeToFile ()
 Function to write buffered data to file.
 
void recalculateDescendants ()
 Function to recursively re-calculate the number of descendants of a given node on the tree.
 
void recalculateRank ()
 Function to recursively re-calculate the rank of a given node on the tree.
 
std::vector< std::string > decomposeAddress (std::string address)
 Function to decompose a string into a series of substrings via indexing.
 
void findMatches (const std::vector< std::string > &subaddresses, const unsigned int &num_subaddresses, unsigned int index, bool match_found, std::vector< GraphTreeObject * > &matches)
 Function to find all addresses mathcing the set of substrings @parem subaddresses A vector of subaddresses to match.
 
std::vector< GraphTreeObject * > searchNodes (const std::string &address)
 Function to search through the graph tree by string address.
 
int addChild (GraphTreeObject *child)
 Function to add a child to the graph tree object.
 
int removeChild (GraphTreeObject *child)
 Function to remove a child from the graph node's children.
 

Protected Attributes

std::vector< GraphTreeObject * > _logged_nodes
 A vector to hold all graph tree object pointers.
 
std::vector< std::string > _log_names
 
std::string _current_name
 
std::string _filename
 String for the filename.
 
std::string _output_directory
 
unsigned int _buffer_size = 10
 Ok, so this is important. At the end of the day, every variable resolves somehow into a basic type, because it is mandated in our logging system. The catch is that we need to resolve variables into headers and then data streams, which can be difficult because C++ is a strongly typed language and we're not really detecting our type until runtime. The solution here is a little messy, but we're basically going to create a bunch of different vectors to hold our different variables, then tie each element to its vector via indices. Got it? God knows I sure don't. Here we go... Vectors to hold defined/accepted data types and read/write indices.
 
unsigned int _buffer_write_idx = 0
 
unsigned int _buffer_read_idx = 0
 
std::vector< std::vector< bool > > _bool_vector
 
std::vector< std::vector< int > > _int_vector
 0
 
std::vector< std::vector< long unsigned int > > _uint_vector
 1
 
std::vector< std::vector< float > > _float_vector
 2
 
std::vector< std::vector< double > > _double_vector
 3
 
std::vector< std::vector< std::string > > _string_vector
 4
 
bool _locked
 5
 
unsigned int _seq_idx = 0
 
std::vector< std::string > _headers
 
std::vector< unsigned int_sequence
 
std::vector< vector_select_e > _type
 
std::vector< int_group_idx
 
std::vector< Group_groups
 Set to -1 if variable is not part of group.
 
log_level_e _local_log_level = NONE
 Our local log level – allows a higher log level locally than the overall system.
 
GraphTreeObject_parent = nullptr
 Pointer to the object's parent – should be null if the object is the root. Is set to null by default.
 
std::vector< GraphTreeObject * > _children
 Pointers to the object's children – automatically set to an empty vector, and can be increased to any size.
 
std::string _name = DEFAULT_NAME
 String name for object.
 
uint16_t _num_descendants = 0
 Total number of descendants (includes children of children) of the tree, again not including the tree itself.
 
uint8_t _num_children = 0
 Number of direct children of the tree (not including the tree itself)
 
uint8_t _rank = 0
 Rank for how far down on the graph tree the object is – starts at zero and maxes out at 256 via variable size.
 
uint8_t _graph_tree_type = BASE_GRAPH_TREE
 Variable to store graph tree object type.
 
bool _loggable = false
 Variable to indicate whether the selected object is loggable. Set to false by default.
 

Detailed Description

Class for logging data to a file.

This function defines a base logger class with functions as outlined and described in the header. It fully defines the updstream logging interface (interface to logged objects and buffering), but does not define a downstream interface to the actual output file. That interface is reserved for inherited classes. With that in mind, this class is essentially useless until inherited, and should not be used directly. Current planned inherited file structures are CSV and HDF5. The logger class is a generic class targeted at collecting, buffering, and writing data to a file. It operates on the graph tree by interfacing with objects inherited from it (graph tree by default cannot be logged). Desired log parameters can be passed either by a direct handle to the class or by string address, and may be passed as a single unit (just one data point) or a recursive inheritence (log an entire branch of the tree). Data may be buffered in the logger for speed and efficiency, but is not buffered by default for simplicity. Regardless, the buffer class is used as the primary method for collecting data off of graph tree objects for writing.

Constructor & Destructor Documentation

◆ SimLogger()

clockwerk::SimLogger::SimLogger ( Executive executive,
const std::string &  filename = "output.csv",
unsigned int  buffer_size = 1 
)

Constructor for the SimLogger object – has a default rate of every ms.

Parameters
execHandle to the executive object

Member Function Documentation

◆ _createSetupFile()

virtual int clockwerk::SimLogger::_createSetupFile ( )
inlineprotectedvirtual

Function to create a file, in whatever format is chosen, for logging.

Reimplemented in modelspace::CsvLogger, and modelspace::Hdf5Logger.

◆ _writeToFile()

virtual int clockwerk::SimLogger::_writeToFile ( )
inlineprotectedvirtual

Function to write buffered data to file.

Reimplemented in modelspace::CsvLogger, and modelspace::Hdf5Logger.

◆ addChild()

int clockwerk::GraphTreeObject::addChild ( GraphTreeObject child)
protectedinherited

Function to add a child to the graph tree object.

Parameters
childA pointer to the graph tree object to add to the tree
Returns
An error code corresponding to success/failure

◆ addParameter() [1/2]

int clockwerk::SimLogger::addParameter ( const std::string &  parameter,
const std::string &  param_name,
bool  recursive = false 
)

Function to add a logging parameter to the SimLogger. Overloaded to add graph node by string or by object.

Parameters
parameterGraph tree node passed as address string
param_nameThe name to which the parameter will be logged
recursiveFlag indicating whether parameter should be added recursively (include all lower items on its tree) or not (the default)

◆ addParameter() [2/2]

int clockwerk::SimLogger::addParameter ( GraphTreeObject parameter,
const std::string &  param_name,
bool  recursive = false 
)

Function to add a logging parameter to the SimLogger. Overloaded to add graph node by string or by object.

Parameters
parameterGraph tree node passed as object
param_nameThe name to which the parameter will be logged
recursiveFlag indicating whether parameter should be added recursively (include all lower items on its tree) or not (the default)

◆ address()

std::string clockwerk::GraphTreeObject::address ( )
inherited

Getter for object's string address.

Returns
Returns the string address for the object

◆ close()

int clockwerk::SimLogger::close ( )
virtual

Function to close down the file – logs remaining buffered data and closes.

Reimplemented in modelspace::CsvLogger, and modelspace::Hdf5Logger.

◆ decomposeAddress()

std::vector< std::string > clockwerk::GraphTreeObject::decomposeAddress ( std::string  address)
protectedinherited

Function to decompose a string into a series of substrings via indexing.

Parameters
addressThe address to be decomposed
Returns
A vector of strings containing each delimeter-separated substring

◆ dump()

void clockwerk::GraphTreeObject::dump ( bool  recursive = false)
inherited

Function to dump the graph node, optionally including descendents.

Parameters
recursiveParameter indicating whether to dump only one node (default/false) or the entire tree (true)
Note
Recursive dumps the whole tree... be careful what you wish for

◆ findLoggable()

void clockwerk::GraphTreeObject::findLoggable ( std::vector< GraphTreeObject * > &  matches)
inherited

Function to find all loggable parameters in this node and its children.

Parameters
matchesExpandable vector to hold all matches in the tree

◆ findMatches()

void clockwerk::GraphTreeObject::findMatches ( const std::vector< std::string > &  subaddresses,
const unsigned int num_subaddresses,
unsigned int  index,
bool  match_found,
std::vector< GraphTreeObject * > &  matches 
)
protectedinherited

Function to find all addresses mathcing the set of substrings @parem subaddresses A vector of subaddresses to match.

Parameters
num_subaddressesThe number of subaddresses to match
indexThe index of subaddresses to search in
match_foundVariable indicating whether a match was found higher in the tree
matchesImplicit return of all matches to the specified address

◆ getByAddress()

GraphTreeObject * clockwerk::GraphTreeObject::getByAddress ( const std::string &  address)
inherited

Function to get an object from the graph tree by string address.

Parameters
addressThe exact string address to access the variable with
Returns
Pointer to the graph tree object at the address, or nullptr if address is not valid or returns

◆ header()

◆ header_info()

virtual std::vector< std::string > clockwerk::GraphTreeObject::header_info ( ) const
inlinevirtualinherited

Reimplemented in clockwerk::Time.

◆ lock()

void clockwerk::GraphTreeObject::lock ( bool  recursive = true)
inherited

Function to lock the graph tree object and its children to changes.

Parameters
recursiveFlag to indicate whether lock should be recursive

◆ lockForLogging()

int clockwerk::SimLogger::lockForLogging ( )
virtual

Function to lock the file for logging once all parameters have been defined. Generates headers and creates buffers for logging based on parameters that have already been added above.

Note
Should be called once, before sim starts
If anything changes after this point, it could potentially cause problems in the SimLoggers
Should NOT be overridden in standard logging implementation.

◆ log() [1/2]

int clockwerk::SimLogger::log ( )
virtual

Function to log data to buffer. Flushes buffer to file when full.

Note
Should NOT be overridden in standard implementation. Virtual call is for Python virtual implementation but should generally be disregarded

◆ log() [2/2]

◆ logLevel()

void clockwerk::SimLogger::logLevel ( log_level_e  new_level)
inline

Function to set the local model log level.

Parameters
new_levelThe new level to set logging to

◆ parent() [1/2]

int clockwerk::GraphTreeObject::parent ( GraphTreeObject new_parent)
inherited

Function to assign the node's parent via reference.

Parameters
new_parentThe new parent to assign the node to
Returns
Error code corresponding to success/failure
Note
This function is the only method to ADD or REMOVE a node from a tree. To add, pass the address to a GraphTreeObject to this function. To remove, pass a nullptr. Note that parents for objects on an existing tree can be reassigned

◆ parent() [2/2]

int clockwerk::GraphTreeObject::parent ( GraphTreeObject new_parent)
inherited

Function to assign the node's parent via pointer.

Parameters
new_parentThe new parent to assign the node to
Returns
Error code corresponding to success/failure
Note
This function is the only method to ADD or REMOVE a node from a tree. To add, pass the address to a GraphTreeObject to this function. To remove, pass a nullptr. Note that parents for objects on an existing tree can be reassigned

◆ recurseGraphNodeInfo()

void clockwerk::GraphTreeObject::recurseGraphNodeInfo ( unsigned int  counter)
inherited

Function to recurse through graph tree and print all node info.

Returns
String containing all recursive graph node information in tab cascade format

◆ removeChild()

int clockwerk::GraphTreeObject::removeChild ( GraphTreeObject child)
protectedinherited

Function to remove a child from the graph node's children.

Parameters
childThe child to remove
Returns
Success/fail error code based on whether the child is present

◆ search()

std::vector< std::string > clockwerk::GraphTreeObject::search ( const std::string &  address)
inherited

Function to search through the graph tree by string address.

Parameters
address`The address or address chunk to search for
Returns
A vector of string addresses meeting the criteria specified by address

◆ searchNodes()

std::vector< GraphTreeObject * > clockwerk::GraphTreeObject::searchNodes ( const std::string &  address)
protectedinherited

Function to search through the graph tree by string address.

Parameters
address`The address or address chunk to search for
Returns
A vector of objects addresses meeting the criteria specified by address

◆ setup()

virtual int clockwerk::SimLogger::setup ( const std::string &  file_name)
inlinevirtual

Function to set up the SimLogger.

Parameters
file_nameName of the file to log to
Returns
Integer code indicating success/failure

◆ type()

virtual int clockwerk::GraphTreeObject::type ( )
inlinevirtualinherited

Function to indicate type – -1 by default unless implemented downstream.

Returns
Int indicating frame type – unimplemented always -1

◆ unlock()

void clockwerk::GraphTreeObject::unlock ( bool  recursive = true)
inherited

Function to unlock the graph tree and its children for changes.

Parameters
recursiveFlag to indicate whether unlock should be recursive

◆ writeToBuffer()

int clockwerk::SimLogger::writeToBuffer ( const bool val)

Overloaded function to write data to the buffer. Should not be called directly by the user – is reserved for call within graph object providing the logging.

Note
If init is true, pushes back element into buffer. Otherwise, writes to appropriate buffer

Member Data Documentation

◆ _locked

bool clockwerk::SimLogger::_locked
protected

5

Header and indexing variables


The documentation for this class was generated from the following files: