loggers.hh
Go to the documentation of this file.
#pragma once
#include <memory>
#include <map>
#include <string>
#include <ctime>
#include <cstdarg>
using namespace std; // Required for isnan()
#include <TTCN3.hh>
class loggers {
private:
static std::unique_ptr<loggers> _instance;
std::map<std::string, std::clock_t> _times;
explicit loggers() : _times() { };
inline void log_time_exec(const char *p_fmt, ...);
public:
virtual ~loggers() {};
static inline loggers& get_instance() { return *_instance.get(); };
inline void log_to_hexa(const char *p_prompt, const TTCN_Buffer& buffer);
inline void log_to_hexa(const char *p_prompt, const OCTETSTRING& msg);
inline void log_to_hexa(const char *p_prompt, const unsigned char* msg, const size_t msg_size);
inline void log_msg(const char *p_prompt, const Base_Type& p_type);
inline void log(const char *p_fmt, ...);
inline void user_msg(const char *p_prompt, const Base_Type& p_type);
inline void user(const char *p_fmt, ...);
inline void warning_msg(const char *p_prompt, const Base_Type& p_type);
inline void warning(const char *p_fmt, ...);
inline void error(const char *p_fmt, ...);
inline void set_start_time(std::string& p_time_key);
inline void set_stop_time(std::string& p_time_key, float& p_time);
}; // End of class loggers
void loggers::log_to_hexa(const char *p_prompt, const TTCN_Buffer & buffer)
{
TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
TTCN_Logger::log_event_str(p_prompt);
buffer.log();
TTCN_Logger::end_event();
}
void loggers::log_to_hexa(const char *p_prompt, const OCTETSTRING& msg)
{
TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
TTCN_Logger::log_event_str(p_prompt);
TTCN_Logger::log_event("Size: %d,\nMsg: ", msg.lengthof());
for(int i = 0; i < msg.lengthof(); i++) {
TTCN_Logger::log_event(" %02x", ((const unsigned char*)msg)[i]);
}
TTCN_Logger::log_event("\n");
TTCN_Logger::end_event();
}
void loggers::log_to_hexa(const char *p_prompt, const unsigned char* msg, const size_t msg_size)
{
TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
TTCN_Logger::log_event_str(p_prompt);
for(size_t i = 0; i < msg_size; i++) {
TTCN_Logger::log_event(" %02x", *(msg + i));
}
TTCN_Logger::log_event("\n");
TTCN_Logger::end_event();
}
void loggers::log_msg(const char *p_prompt, const Base_Type& p_type)
{
TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
TTCN_Logger::log_event_str(p_prompt);
p_type.log();
TTCN_Logger::end_event();
}
void loggers::log(const char *p_fmt, ...)
{
TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
va_list args;
va_start(args, p_fmt);
TTCN_Logger::log_event_va_list(p_fmt, args);
va_end(args);
TTCN_Logger::end_event();
}
void loggers::user_msg(const char *p_prompt, const Base_Type& p_type)
{
TTCN_Logger::begin_event(TTCN_Logger::USER_UNQUALIFIED);
TTCN_Logger::log_event_str(p_prompt);
p_type.log();
TTCN_Logger::end_event();
}
void loggers::user(const char *p_fmt, ...)
{
TTCN_Logger::begin_event(TTCN_Logger::USER_UNQUALIFIED);
va_list args;
va_start(args, p_fmt);
TTCN_Logger::log_event_va_list(p_fmt, args);
va_end(args);
TTCN_Logger::end_event();
}
void loggers::warning(const char *p_fmt, ...)
{
TTCN_Logger::begin_event(TTCN_Logger::WARNING_UNQUALIFIED);
va_list args;
va_start(args, p_fmt);
TTCN_Logger::log_event_va_list(p_fmt, args);
va_end(args);
TTCN_Logger::end_event();
}
void loggers::warning_msg(const char *p_prompt, const Base_Type& p_type)
{
TTCN_Logger::begin_event(TTCN_Logger::WARNING_UNQUALIFIED);
TTCN_Logger::log_event_str(p_prompt);
p_type.log();
TTCN_Logger::end_event();
}
void loggers::error(const char *p_fmt, ...)
{
va_list args;
va_start(args, p_fmt);
TTCN_error(p_fmt, args);
va_end(args);
}
void loggers::set_start_time(std::string& p_time_key)
{
_times[p_time_key] = std::clock();
}
void loggers::set_stop_time(std::string& p_time_key, float& p_time)
{
std::map<std::string, std::clock_t>::iterator it = _times.find(p_time_key);
if (it != loggers::_times.end()) {
p_time = (std::clock() - _times[p_time_key]) * 1000.0 / CLOCKS_PER_SEC; // in milliseconds
_times.erase(it);
loggers::get_instance().log_time_exec("%s: Execution duration: %f ms", p_time_key.c_str(), p_time);
}
}
void loggers::log_time_exec(const char *p_fmt, ...)
{
TTCN_Logger::begin_event(TTCN_Logger::EXECUTOR_RUNTIME);
va_list args;
va_start(args, p_fmt);
TTCN_Logger::log_event_va_list(p_fmt, args);
va_end(args);
TTCN_Logger::end_event();
}
loggers::warning
void warning(const char *p_fmt,...)
Definition: loggers.hh:238
loggers::log
void log(const char *p_fmt,...)
Debug message based on printf-compliant formatting message.
Definition: loggers.hh:210
loggers::log_to_hexa
void log_to_hexa(const char *p_prompt, const TTCN_Buffer &buffer)
Hexa dump of the.
Definition: loggers.hh:67
loggers::loggers
loggers()
Timer used to measure execution time between calls to methods ::set_start_time and ::set_stop_time...
Definition: loggers.hh:52
std
loggers::~loggers
virtual ~loggers()
Default destructor.
Definition: loggers.hh:60
loggers::user_msg
void user_msg(const char *p_prompt, const Base_Type &p_type)
User message of TITAN data structures.
Definition: loggers.hh:220
loggers::log_msg
void log_msg(const char *p_prompt, const Base_Type &p_type)
Debug log of TITAN data structures.
Definition: loggers.hh:202
loggers::_times
std::map< std::string, std::clock_t > _times
Smart pointer to the unique instance of the logger framework.
Definition: loggers.hh:45
loggers::_instance
static std::unique_ptr< loggers > _instance
Definition: loggers.hh:44
loggers
This class provides basic functionalities for an ITS dictionary.
Definition: loggers.hh:42
loggers::user
void user(const char *p_fmt,...)
User message based on printf-compliant formatting message.
Definition: loggers.hh:228
loggers::get_instance
static loggers & get_instance()
Accessor for the unique instance of the logger framework .
Definition: loggers.hh:67
loggers::error
void error(const char *p_fmt,...)
Definition: loggers.hh:256
loggers::set_stop_time
void set_stop_time(std::string &p_time_key, float &p_time)
Stop execution time measurement.
Definition: loggers.hh:269
loggers::log_time_exec
void log_time_exec(const char *p_fmt,...)
Definition: loggers.hh:280
loggers::set_start_time
void set_start_time(std::string &p_time_key)
Start execution time measurement.
Definition: loggers.hh:264
loggers::warning_msg
void warning_msg(const char *p_prompt, const Base_Type &p_type)
Definition: loggers.hh:248