Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*!
* \file loogers.hh
* \brief Header file for the logger framework.
* \author ETSI STF525
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#pragma once
#include <memory>
#include <map>
#include <string>
#include <ctime>
#include <cstdarg>
/**
class Base_Type;
class OCTETSTRING;
class TTCN_Buffer;
class TTCN_Logger;
enum TTCN_Logger::Severity;
extern void TTCN_error(const char *err_msg, ...) __attribute__ ((__format__ (__printf__, 1, 2), __noreturn__));
extern void TTCN_error_begin(const char *err_msg, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
extern void TTCN_error_end() __attribute__ ((__noreturn__));
void TTCN_warning(const char *warning_msg, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
extern void TTCN_warning_begin(const char *warning_msg, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
extern void TTCN_warning_end();
**/
using namespace std; // Required for isnan()
#include <TTCN3.hh>
/*!
* \class loggers
* \brief This class provides basic functionalities for an ITS dictionary
* \implements Singleton pattern
* \todo Remove reference to TTCN3.hh
*/
class loggers {
private: //! \privatesection
static std::unique_ptr<loggers> _instance; //! Smart pointer to the unique instance of the logger framework
std::map<std::string, std::clock_t> _times; //! Timer used to measure execution time between calls to methods \loggers::set_start_time and \loggers::set_stop_time
/*!
* \brief Default constructor
* Create a new instance of the loggers class
* \private
*/
explicit loggers() : _times() { };
inline void log_time_exec(const char *p_fmt, ...);
public: //! \publicsection
/*!
* \brief Default destructor
*/
virtual ~loggers() {};
/*!
* \fn static loggers& get_instance();
* \brief Accessor for the unique instance of the logger framework
* \inline
*/
static inline loggers& get_instance() { return *_instance.get(); };
/*!
* \fn void log_to_hexa(const char *p_prompt, const TTCN_Buffer& buffer);
* \brief Hexa dump of the \see TTCN_Buffer buffer
* \param[in] p_prompt Label of the log to be produced
* \param[in] buffer The TTCN_Buffer buffer to dump
* \inline
*/
inline void log_to_hexa(const char *p_prompt, const TTCN_Buffer& buffer);
/*!
* \fn void log_to_hexa(const char *p_prompt, const OCTETSTRING& msg);
* \brief Hexa dump of the \see OCTETSTRING buffer
* \param[in] p_prompt Label of the log to be produced
* \param[in] msg The OCTETSTRING buffer to dump
* \inline
*/
inline void log_to_hexa(const char *p_prompt, const OCTETSTRING& msg);
/*!
* \fn void log_to_hexa(const char *p_prompt, const unsigned char* msg, const size_t msg_size);
* \brief Hexa dump of the provided buffer
* \param[in] p_prompt Label of the log to be produced
* \param[in] msg The buffer to dump
* \inline
*/
inline void log_to_hexa(const char *p_prompt, const unsigned char* msg, const size_t msg_size);
/*!
* \fn void log_msg(const char *p_prompt, const Base_Type& p_type);
* \brief Debug log of TITAN data structures
* \param[in] p_prompt Label of the log to be produced
* \param[in] msg The TITAN data structure to log
* \inline
*/
inline void log_msg(const char *p_prompt, const Base_Type& p_type);
/*!
* \fn void log(const char *p_fmt, ...);
* \brief Debug message based on printf-compliant formatting message
* \param[in] p_fmt The printf-compliant format of the message to log
* \param[in] ... The arguments
* \inline
*/
inline void log(const char *p_fmt, ...);
/*!
* \fn void user_msg(const char *p_prompt, const Base_Type& p_type);
* \brief User message of TITAN data structures
* \param[in] p_prompt Label of the log to be produced
* \param[in] msg The TITAN data structure to log
* \inline
*/
inline void user_msg(const char *p_prompt, const Base_Type& p_type);
/*!
* \fn void user(const char *p_fmt, ...);
* \brief User message based on printf-compliant formatting message
* \param[in] p_fmt The printf-compliant format of the message to log
* \param[in] ... The arguments
* \inline
*/
inline void user(const char *p_fmt, ...);
/*!
* \fn void user_msg(const char *p_prompt, const Base_Type& p_type);
* \brief Warning message of TITAN data structures
* \param[in] p_prompt Label of the log to be produced
* \param[in] msg The TITAN data structure to log
* \inline
*/
inline void warning_msg(const char *p_prompt, const Base_Type& p_type);
/*!
* \fn void user(const char *p_fmt, ...);
* \brief Warning message based on printf-compliant formatting message
* \param[in] p_fmt The printf-compliant format of the message to log
* \param[in] ... The arguments
* \inline
*/
inline void warning(const char *p_fmt, ...);
/*!
* \fn void user(const char *p_fmt, ...);
* \brief Error message based on printf-compliant formatting message
* \param[in] p_fmt The printf-compliant format of the message to log
* \param[in] ... The arguments
* \inline
*/
inline void error(const char *p_fmt, ...);
/*!
* \fn void set_start_time(std::string& p_time_key);
* \brief Start execution time measurement
* \param[in] p_time_key A timer identifier (any string)
* \inline
*/
inline void set_start_time(std::string& p_time_key);
/*!
* \fn void set_stop_time(std::string& p_time_key, float& p_time);
* \brief Stop execution time measurement
* \param[in] p_time_key The timer identifier provided while calling \see loggers::set_start_time method
* \param[out] p_time The execution time measured in milliseconds
* \inline
*/
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();
}