AUTh-ARL Core Stack  0.7
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
arl::io::Logger Class Reference

Helper class providing logging capabilities. Useful in control loops for logging data and easily parsing to Matlab. More...

#include <logger.h>

Public Member Functions

 Logger ()
 Empty constructor. Calling this constructor will not open the file. After this you need to call Logger::open(). More...
 
 Logger (const std::string &file_name, const std::string &log_path="/home/user/autharl_logfiles/")
 The default constructor which directly opens and initialize the file for logging. More...
 
 ~Logger ()
 
void open (const std::string &file_name, const std::string &log_path="/home/user/autharl_logfiles/")
 Opens the file in the given destination. More...
 
void close ()
 Closes the file. More...
 
template<class V >
void logVector (const V &data, const std::string &name)
 Function for logging vectors containing data. More...
 
template<class T >
void log (const T &data, const std::string &name)
 The main function for logging. More...
 

Detailed Description

Helper class providing logging capabilities. Useful in control loops for logging data and easily parsing to Matlab.

The logger saves the data in the provided path with a time stamp of the current time and the provided file name. Within a loop you can use the Logger::log() function providing the data and the name of the variable. The logger will log in the first line the name of the variables logged in this loop and then in each row of the file the corresponding data. The comma dilimiter is used by this class.

Example of use in a control loop:

Logger logger("my_file", "/home/user/my_logfiles");
double t;
double dt = 0.001;
while (true)
{
t += dt;
logger.log(t, "time");
arma::vec joint_positions = getJointPositionsFromRobot();
logger.log(joint_positions, "joint_positions");
double a = 5;
logger.log(a, "a");
}

If you have a robot with 3 joints the log file will located in /home/user/my_logfiles/20-09-2017_09:34:52_my_file.log and it will look like this:

time,joint_positions_0,joint_positions_1,joint_positions_2
0,1,1,1,5
0.001,1.1,1.1,1.1,5
0.002,1.2,1.2,1.2,5
...etc

Constructor & Destructor Documentation

arl::io::Logger::Logger ( )

Empty constructor. Calling this constructor will not open the file. After this you need to call Logger::open().

arl::io::Logger::Logger ( const std::string &  file_name,
const std::string &  log_path = "/home/user/autharl_logfiles/" 
)

The default constructor which directly opens and initialize the file for logging.

Parameters
file_nameThe desired file name of the log
log_pathThe desired path in the disk. The path should be already exist.
Exceptions
Ifthe directory does not exist and the file couldn't be opened.

Here is the call graph for this function:

arl::io::Logger::~Logger ( )

Here is the call graph for this function:

Member Function Documentation

void arl::io::Logger::close ( )

Closes the file.

template<class T >
void arl::io::Logger::log ( const T &  data,
const std::string &  name 
)
inline

The main function for logging.

Attention
The function will just return without warning if the log file has not been opened yet. This feature helps to leave the calls of the log function in your code even if you do not desire logging and thus you haven't called Logger::open().
Parameters
dataThe data to log
nameThe desired name of the data in the log file.
template<class V >
void arl::io::Logger::logVector ( const V &  data,
const std::string &  name 
)
inline

Function for logging vectors containing data.

You can use it to log Armadillo (arma::vec) or Eigen (Eigen::VectorXd) vectors, or any type that overloads the operator().

Attention
The function will just return without warning if the log file has not been opened yet. This feature helps to leave the calls of the log function in your code even if you do not desire logging and thus you haven't called Logger::open().
Parameters
dataThe vector of data.
nameThe desired name of the data in the log file.

Here is the call graph for this function:

void arl::io::Logger::open ( const std::string &  file_name,
const std::string &  log_path = "/home/user/autharl_logfiles/" 
)

Opens the file in the given destination.

Parameters
file_nameThe desired file name of the log
log_pathThe desired path in the disk. The path should be already exist.
Exceptions
Ifthe directory does not exist and the file couldn't be opened.

Here is the call graph for this function: