CBICA Toolkit  1.0.0
Logging Class Reference

The logging class. More...

#include <cbicaLogging.h>

Public Member Functions

void EnableConsoleLogging ()
 Switches from text to console file logging. More...
 
void EnableGMTLogging ()
 This enables logging the date and time in GMT rather than in local (which is the default behavior)
 
void EnableMultiUserLogging ()
 This is useful in scenarios where the application has been installed in a multi-user machine. More...
 
void EnableTextLogging (const std::string &newLogFile)
 Switches from console to text file logging. More...
 
std::string getLoggingFileName ()
 Get the file name with full path where log has happened. More...
 
 Logging (const std::string file_name, const std::string FreeText_input)
 Actual Constructor. More...
 
 Logging ()
 Default constructor. More...
 
 Logging (const Logging &origin)
 Default constructor. More...
 
void UseNewFile (const std::string &newLogFile)
 Change Logging file after initializing class. More...
 
void Write (const std::string FreeText_input)
 Function to call to write to log file. More...
 
void WriteError (const std::string FreeText_input)
 Function to call to write error messages to log file without any free text. More...
 
virtual ~Logging ()
 The Destructor.
 

Protected Member Functions

void initialize_class (std::string &file_name_with_path_wrap, std::ofstream &log_file_wrap, std::string &exe_name_wrap, std::string &user_name_wrap)
 The function used to initialize the class. More...
 
void writing_function (const std::string &FreeText_wrap, std::ofstream &log_file_wrap, const std::string &exe_name_wrap, const std::string &user_name_wrap, bool isError=false)
 The function used to do the actual writing onto the file. More...
 

Detailed Description

The logging class.

This automatically generates a machine-parseable log specified by the file name. The user also has the option of submitting free text to be put along with the log. The generated log is in the format show below:

<4 digit year>:<2 digit month>:<2 digit date>,<2 digit 24 hour>:<2 digit minute>:<2 digit second>;<exe name>="">;<user name>="">;<free text>="">

Usage:

// writing to a file
cbica::Logging logger( "file_name.txt", "randomness is highly underrated" ); // the file has already been written at this point and can be viewed
// to write to console, either initialize the class as 'cbica::Logging logger;' or call EnableConsoleLogging() after the initialization.
...
// more code
...
logger.Write( "'I accept chaos, I'm not sure whether it accepts me' - Bob Dylan" ); // writes to file_name.txt

The class defaults to console logging. Use Logging::EnableTextLogging() or UseNewFile() to switch; by default, it writes to a text file 'EXE_NAME-log.txt' in directory specified by cbica::createTemporaryDirectory()

Constructor & Destructor Documentation

◆ Logging() [1/3]

Logging ( const std::string  file_name,
const std::string  FreeText_input 
)
explicit

Actual Constructor.

Parameters
file_name_with_pathThe file onto which the log file is to be written
FreeText_inputFree text which the user wants to be present in the log, defaults to an empty string

◆ Logging() [2/3]

Logging ( )
explicit

Default constructor.

Just used to keep a track of the user name and executable run at a particular time.

◆ Logging() [3/3]

Logging ( const Logging origin)

Default constructor.

Just used to keep a track of the user name and executable run at a particular time.

Member Function Documentation

◆ EnableConsoleLogging()

void EnableConsoleLogging ( )

Switches from text to console file logging.

This is helpful if the user wants to visualize the console output. If it is done for saving, the recommended way is to call EnableTextLogging().

The output stamps are of the form:

<4 digit year>:<2 digit month>:<2 digit date>,<2 digit 24 hour>:<2 digit minute>:<2 digit second>;<free text>="">

◆ EnableMultiUserLogging()

void EnableMultiUserLogging ( )
inline

This is useful in scenarios where the application has been installed in a multi-user machine.

This also disables writing of the date in the log since the assumption is that on a single user machine, the filename should be in the following format: ${userHomeDir}/.${appName}/${currentDate} (see CaPTk logging as example). This also disables the writing of the executable name.

◆ EnableTextLogging()

void EnableTextLogging ( const std::string &  newLogFile)

Switches from console to text file logging.

The output stamps are of the form:

<4 digit year>:<2 digit month>:<2 digit date>,<2 digit 24 hour>:<2 digit minute>:<2 digit second>;<free text>="">

◆ getLoggingFileName()

std::string getLoggingFileName ( )

Get the file name with full path where log has happened.

Returns
file_name_with_path

◆ initialize_class()

void initialize_class ( std::string &  file_name_with_path_wrap,
std::ofstream &  log_file_wrap,
std::string &  exe_name_wrap,
std::string &  user_name_wrap 
)
inlineprotected

The function used to initialize the class.

Kept private to avoid cluttering global namespace.

Parameters
file_name_with_path_wrapWrap for file_name_with_path
log_file_wrapWrap for log_file
exe_name_wrapWrap for exe_name
user_name_wrapWrap for user_name

◆ UseNewFile()

void UseNewFile ( const std::string &  newLogFile)

Change Logging file after initializing class.

Parameters
newLogFilePath of new log file. If empty, it becomes 'cbica::createTmpDir() + cbica::getExecutableName() + "-log.txt"'

◆ Write()

void Write ( const std::string  FreeText_input)

Function to call to write to log file.

Parameters
FreeText_inputFree text which the user wants to be present in the log, defaults to an empty string

◆ WriteError()

void WriteError ( const std::string  FreeText_input)

Function to call to write error messages to log file without any free text.

Parameters
FreeText_inputFree text which the user wants to be present in the log

◆ writing_function()

void writing_function ( const std::string &  FreeText_wrap,
std::ofstream &  log_file_wrap,
const std::string &  exe_name_wrap,
const std::string &  user_name_wrap,
bool  isError = false 
)
inlineprotected

The function used to do the actual writing onto the file.

Kept private to avoid cluttering global namespace.

Parameters
FreeText_wrapWrap for FreeText
log_file_wrapWrap for log_file
timer_wrapWrap for timer
exe_name_wrapWrap for exe_name
user_name_wrapWrap for user_name

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