Newer
Older
#include <memory>
#include <map>
#include <string>
#include <ctime>
/*class OCTETSTRING;
class TTCN_Buffer;
class TTCN_Logger;
extern void TTCN_error(const char *err_msg, ...);*/
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class loggers {
static std::unique_ptr<loggers> _instance;
std::map<std::string, std::clock_t> _times;
loggers() : _times() {};
inline void log_time_exec(const char *fmt, ...);
public:
virtual ~loggers() {};
static inline loggers& get_instance() { return *_instance.get(); };
inline void log_to_hexa(const char *prompt, const TTCN_Buffer & buffer);
inline void log_to_hexa(const char *prompt, const OCTETSTRING& msg);
inline void log_msg(const char *prompt, const Base_Type& type);
inline void log(const char *fmt, ...);
inline void error(const char *fmt, ...);
inline void set_start_time(std::string& p_time_key);
inline void set_stop_time(std::string& p_time_key, float& p_time);
};
void loggers::log_to_hexa(const char *prompt, const TTCN_Buffer & buffer)
{
TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
TTCN_Logger::log_event_str(prompt);
buffer.log();
TTCN_Logger::end_event();
}
void loggers::log_to_hexa(const char *prompt, const OCTETSTRING& msg)
{
TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
TTCN_Logger::log_event_str(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_msg(const char *prompt, const Base_Type& type)
{
TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
TTCN_Logger::log_event_str(prompt);
type.log();
TTCN_Logger::end_event();
}
void loggers::log(const char *fmt, ...)
{
TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
va_list args;
va_start(args, fmt);
TTCN_Logger::log_event_va_list(fmt, args);
va_end(args);
TTCN_Logger::end_event();
}
void loggers::error(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
TTCN_error(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 *fmt, ...)
{
TTCN_Logger::begin_event(TTCN_Logger::EXECUTOR_RUNTIME);
va_list args;
va_start(args, fmt);
TTCN_Logger::log_event_va_list(fmt, args);
va_end(args);
TTCN_Logger::end_event();