Chi-Tech
chi::ChiLog Class Reference

#include <chi_log.h>

Inheritance diagram for chi::ChiLog:
chi::TimingLog

Data Structures

struct  Event
 
struct  EventInfo
 
class  RepeatingEvent
 

Public Types

enum  LOG_LVL {
  LOG_0 = 1 , LOG_0WARNING = 2 , LOG_0ERROR = 3 , LOG_0VERBOSE_0 = 4 ,
  LOG_0VERBOSE_1 = 5 , LOG_0VERBOSE_2 = 6 , LOG_ALL = 7 , LOG_ALLWARNING = 8 ,
  LOG_ALLERROR = 9 , LOG_ALLVERBOSE_0 = 10 , LOG_ALLVERBOSE_1 = 11 , LOG_ALLVERBOSE_2 = 12
}
 
enum  StdTags { MAX_MEMORY_USAGE = 0 }
 
enum class  EventType { EVENT_CREATED = 0 , SINGLE_OCCURRENCE = 1 , EVENT_BEGIN = 2 , EVENT_END = 3 }
 
enum class  EventOperation {
  NUMBER_OF_OCCURRENCES , TOTAL_DURATION = 1 , AVERAGE_DURATION = 2 , MAX_VALUE = 3 ,
  AVERAGE_VALUE = 4
}
 

Public Member Functions

LogStream Log (LOG_LVL level=LOG_0)
 
void SetVerbosity (int int_level)
 
int GetVerbosity () const
 
LogStream Log0 ()
 
LogStream Log0Warning ()
 
LogStream Log0Error ()
 
LogStream Log0Verbose0 ()
 
LogStream Log0Verbose1 ()
 
LogStream Log0Verbose2 ()
 
LogStream LogAll ()
 
LogStream LogAllWarning ()
 
LogStream LogAllError ()
 
LogStream LogAllVerbose0 ()
 
LogStream LogAllVerbose1 ()
 
LogStream LogAllVerbose2 ()
 
size_t GetRepeatingEventTag (std::string event_name)
 
size_t GetExistingRepeatingEventTag (std::string event_name)
 
void LogEvent (size_t ev_tag, EventType ev_type, const std::shared_ptr< EventInfo > &ev_info)
 
void LogEvent (size_t ev_tag, EventType ev_type)
 
std::string PrintEventHistory (size_t ev_tag)
 
double ProcessEvent (size_t ev_tag, EventOperation ev_operation)
 
- Public Member Functions inherited from chi::TimingLog
TimingBlockCreateTimingBlock (const std::string &name, const std::string &parent_name="")
 
TimingBlockCreateOrGetTimingBlock (const std::string &name, const std::string &parent_name="")
 
TimingBlockGetTimingBlock (const std::string &name)
 

Static Public Member Functions

static ChiLogGetInstance () noexcept
 

Private Member Functions

 ChiLog () noexcept
 

Private Attributes

DummyStream dummy_stream_
 
int verbosity_
 
std::vector< RepeatingEventrepeating_events
 

Additional Inherited Members

- Protected Attributes inherited from chi::TimingLog
std::map< std::string, std::unique_ptr< TimingBlock > > timing_blocks_
 

Detailed Description

Object for controlling logging.

Part A: Output logs

There are three levels of verbosity in ChiTech: Zero(Default), One and Two. These can be set on the command line via the switch -v followed by a space and the number for the verbosity (0,1 or 2).

./bin/ChiTech InputFile.lua -v 1

The lua command chiLogSetVerbosity(int_level) achieves the same.

Printing a log under the auspices of a verbosity level again has numerous options. Firstly, any log can be a normal log, a warning or an error. Secondly, a log can be either location 0 or all the locations in a parallel process environment. The log option enums defined under LOG_LVL are

  • LOG_0, Used only for location 0
  • LOG_0WARNING, Warning only for location 0
  • LOG_0ERROR, Error only for location 0
  • LOG_0VERBOSE_0, Default verbosity level
  • LOG_0VERBOSE_1, Used only if verbosity level equals 1
  • LOG_0VERBOSE_2, Used only if verbosity level equals 2
  • LOG_ALL, Verbose level 0 all locations
  • LOG_ALLWARNING, Warning for any location
  • LOG_ALLERROR, Error for any location
  • LOG_ALLVERBOSE_0, Default verbosity level
  • LOG_ALLVERBOSE_1, Used only if verbosity level equals 1
  • LOG_ALLVERBOSE_2, Used only if verbosity level equals 2

    A log can be made by first connecting code with the logger. This is done by including the log header and then defining an extern reference to the global object.

    #include <chi::log.h>
    extern ChiLog& chi::log;
    ChiLog() noexcept
    Definition: chi_log.cc:20

    A log is then written inside a piece of code as follows:

    void PrintSomethingToLog()
    {
    chi::log.Log() << "This is printed on location 0 only";
    chi::log.Log0Warning() << "This is a warning";
    chi::log.Log0Error() << "This is an error";
    }
[0]  This is printed on location 0 only
[0]  **WARNING** This is a warning
[0]  **!**ERROR**!** This is an error

Part B: Repeating events log

Suppose a routine or segment of code gets called multiple times. Now suppose we want to know how many times this routine was called, how long it ran (total and on-avg), or we want to know at which timestamp it ran every time. We do this by using a repeating event. A repeating event is identified by a tag. Therefore the first step is to obtain a unique id for the event using ChiLog::GetRepeatingEventTag.

size_t tag = chi::log.GetRepeatingEventTag(std::string("Sweep Timing"))

Events can now be logged using ChiLog::LogEvent(size_t ev_tag, ChiLog::EventType ev_type). As an example, consider we want to count the number of occurrences of an event. We trigger multiple occurences using

chi::log.LogEvent(tag,ChiLog::EventType::SINGLE_OCCURRENCE);
@ SINGLE_OCCURRENCE
Signals a single occurrence.

At the end of the block for which you wanted to log these events we can now extract the number of occurrences using

double number_of_occ =
@ NUMBER_OF_OCCURRENCES
Counts creation events, single occurrences and begins.

Below is a complete example:

size_t tag = chi::log.GetRepeatingEventTag(std::string());
for (int i=0; i<5; ++i)
chi::log.LogAll()
<< chi::log.ProcessEvent(tag,
void LogEvent(size_t ev_tag, EventType ev_type, const std::shared_ptr< EventInfo > &ev_info)
Definition: chi_log.cc:204
6

Since chi::log is a global object this functionality is really powerful for use in modules or class objects where multiple methods can contribute to the same event track. For example an object can have an event tag as a member and initialize it at construction then all of the objects methods can contribute to the event.

Supplying event information

In addition to the ChiLog::EventType the user can also supply a reference to a ChiLog::EventInfo structure. Be cautious with this though because each event stored a double and a string which can look like a memory leak if multiple events are pushed up with event information. Developers can supply either a double or a string or both to an event info constructor to instantiate an instance. The event arb_value is by default 0.0 and the event arb_info is by default an empty string. An example is shown below:

size_t tag = chi::log.GetRepeatingEventTag(std::string());
for (int i=0; i<5; ++i)
{
auto ev_info = std::make_shared<ChiLog::EventInfo>(i*2.0);
chi::log.LogEvent(tag,ChiLog::EventType::SINGLE_OCCURRENCE,ev_info);
}
chi::log.LogAll()
<< chi::log.ProcessEvent(tag, ChiLog::EventOperation::AVERAGE_VALUE);
@ AVERAGE_VALUE
Computes the average of the EventInfo arb_value.
4.0

Event information can also be supplied by string values but then the average value is meaningless unless the average value is also supplied. For example:

size_t tag = chi::log.GetRepeatingEventTag(std::string());
chi::log.LogEvent(tag,
std::make_shared<ChiLog::EventInfo>(std::string("A"),2.0));
chi::log.LogEvent(tag,
std::make_shared<ChiLog::EventInfo>(std::string("B")));
chi::log.LogEvent(tag,
std::make_shared<ChiLog::EventInfo>(std::string("C"),2.0));
chi::log.LogAll()
<< chi::log.ProcessEvent(tag, ChiLog::EventOperation::AVERAGE_VALUE);
1.33333

To get a string value of the event history developers can use ChiLog::PrintEventHistory along with the event tag. Just note that it will automatically be formatted for each location so no need to use chi::log to print it. Also, each event will be prepended with a program timestamp in seconds.

std::cout << chi::log.PrintEventHistory(tag);
1.33333
[0]      3.813119000 EVENT_CREATED
[0]      3.813120000 SINGLE_OCCURRENCE A
[0]      3.813121000 SINGLE_OCCURRENCE B
[0]      3.813122000 SINGLE_OCCURRENCE C

Definition at line 193 of file chi_log.h.

Member Enumeration Documentation

◆ EventOperation

enum class chi::ChiLog::EventOperation
strong
Enumerator
NUMBER_OF_OCCURRENCES 

Counts creation events, single occurrences and begins.

TOTAL_DURATION 

Integrates times between begins and ends.

AVERAGE_DURATION 

Computes average time between begins and ends.

MAX_VALUE 

Computes the maximum of the EventInfo arb_value.

AVERAGE_VALUE 

Computes the average of the EventInfo arb_value.

Definition at line 259 of file chi_log.h.

◆ EventType

enum class chi::ChiLog::EventType
strong
Enumerator
EVENT_CREATED 

Automatically signalled when event is created.

SINGLE_OCCURRENCE 

Signals a single occurrence.

EVENT_BEGIN 

Signals the begin of an event.

EVENT_END 

Signals the end of an event.

Definition at line 252 of file chi_log.h.

◆ LOG_LVL

Logging level

Enumerator
LOG_0 

Used only for location 0.

LOG_0WARNING 

Warning only for location 0.

LOG_0ERROR 

Error only for location 0.

LOG_0VERBOSE_0 

Default verbosity level.

LOG_0VERBOSE_1 

Used only if verbosity level equals 1.

LOG_0VERBOSE_2 

Used only if verbosity level equals 2.

LOG_ALL 

Verbose level 0 all locations.

LOG_ALLWARNING 

Warning for any location.

LOG_ALLERROR 

Error for any location.

LOG_ALLVERBOSE_0 

Default verbosity level.

LOG_ALLVERBOSE_1 

Used only if verbosity level equals 1.

LOG_ALLVERBOSE_2 

Definition at line 197 of file chi_log.h.

◆ StdTags

Enumerator
MAX_MEMORY_USAGE 

Tag reserved for logging process memory.

Definition at line 248 of file chi_log.h.

Constructor & Destructor Documentation

◆ ChiLog()

chi::ChiLog::ChiLog ( )
privatenoexcept

Default constructor

Definition at line 20 of file chi_log.cc.

Member Function Documentation

◆ GetExistingRepeatingEventTag()

size_t chi::ChiLog::GetExistingRepeatingEventTag ( std::string  event_name)

Returns a unique tag to the latest version of an existing repeating event.

Definition at line 191 of file chi_log.cc.

◆ GetInstance()

chi::ChiLog & chi::ChiLog::GetInstance ( )
staticnoexcept

Access to the singleton

Definition at line 12 of file chi_log.cc.

◆ GetRepeatingEventTag()

size_t chi::ChiLog::GetRepeatingEventTag ( std::string  event_name)

Returns a unique tag to a newly created repeating event.

Definition at line 176 of file chi_log.cc.

◆ GetVerbosity()

int chi::ChiLog::GetVerbosity ( ) const

Gets the current verbosity level.

Definition at line 172 of file chi_log.cc.

◆ Log()

chi::LogStream chi::ChiLog::Log ( LOG_LVL  level = LOG_0)

Makes a log entry.

Definition at line 35 of file chi_log.cc.

◆ Log0()

LogStream chi::ChiLog::Log0 ( )
inline

Definition at line 230 of file chi_log.h.

◆ Log0Error()

LogStream chi::ChiLog::Log0Error ( )
inline

Definition at line 232 of file chi_log.h.

◆ Log0Verbose0()

LogStream chi::ChiLog::Log0Verbose0 ( )
inline

Definition at line 233 of file chi_log.h.

◆ Log0Verbose1()

LogStream chi::ChiLog::Log0Verbose1 ( )
inline

Definition at line 234 of file chi_log.h.

◆ Log0Verbose2()

LogStream chi::ChiLog::Log0Verbose2 ( )
inline

Definition at line 235 of file chi_log.h.

◆ Log0Warning()

LogStream chi::ChiLog::Log0Warning ( )
inline

Definition at line 231 of file chi_log.h.

◆ LogAll()

LogStream chi::ChiLog::LogAll ( )
inline

Definition at line 237 of file chi_log.h.

◆ LogAllError()

LogStream chi::ChiLog::LogAllError ( )
inline

Definition at line 239 of file chi_log.h.

◆ LogAllVerbose0()

LogStream chi::ChiLog::LogAllVerbose0 ( )
inline

Definition at line 240 of file chi_log.h.

◆ LogAllVerbose1()

LogStream chi::ChiLog::LogAllVerbose1 ( )
inline

Definition at line 241 of file chi_log.h.

◆ LogAllVerbose2()

LogStream chi::ChiLog::LogAllVerbose2 ( )
inline

Definition at line 242 of file chi_log.h.

◆ LogAllWarning()

LogStream chi::ChiLog::LogAllWarning ( )
inline

Definition at line 238 of file chi_log.h.

◆ LogEvent() [1/2]

void chi::ChiLog::LogEvent ( size_t  ev_tag,
EventType  ev_type 
)

Logs an event without any event information.

Definition at line 218 of file chi_log.cc.

◆ LogEvent() [2/2]

void chi::ChiLog::LogEvent ( size_t  ev_tag,
EventType  ev_type,
const std::shared_ptr< EventInfo > &  ev_info 
)

Logs an event with the supplied event information.

Definition at line 204 of file chi_log.cc.

◆ PrintEventHistory()

std::string chi::ChiLog::PrintEventHistory ( size_t  ev_tag)

Returns a string representation of the event history associated with the tag. Each event entry will be prepended by the location id and the program timestamp in seconds. This method uses the ChiLog::EventInfo::GetString method to append information. This allows derived classes to implement more sophisticated outputs.

Definition at line 234 of file chi_log.cc.

◆ ProcessEvent()

double chi::ChiLog::ProcessEvent ( size_t  ev_tag,
EventOperation  ev_operation 
)

Processes an event given an event operation. See ChiLog for further reference.

Definition at line 275 of file chi_log.cc.

◆ SetVerbosity()

void chi::ChiLog::SetVerbosity ( int  int_level)

Sets the verbosity level.

Definition at line 165 of file chi_log.cc.

Field Documentation

◆ dummy_stream_

DummyStream chi::ChiLog::dummy_stream_
private

Definition at line 214 of file chi_log.h.

◆ repeating_events

std::vector<RepeatingEvent> chi::ChiLog::repeating_events
private

Definition at line 272 of file chi_log.h.

◆ verbosity_

int chi::ChiLog::verbosity_
private

Definition at line 215 of file chi_log.h.


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