ETSI STF525 / Internal Testing test suite This project provides an internal testing test suite and its associated Test Adapter/Codec

loggers.hh
Go to the documentation of this file.
1
11 #pragma once
12
13 #include <memory>
14 #include <map>
15 #include <string>
16 #include <ctime>
17 #include <cstdarg>
18
33 using namespace std; // Required for isnan()
34 #include <TTCN3.hh>
35
42 class loggers {
43 private:
44 static std::unique_ptr<loggers> _instance;
45 std::map<std::string, std::clock_t> _times;
46
52 explicit loggers() : _times() { };
53
54 inline void log_time_exec(const char *p_fmt, ...);
55
56 public:
57
60 virtual ~loggers() {};
61
67 static inline loggers& get_instance() { return *_instance.get(); };
68
76 inline void log_to_hexa(const char *p_prompt, const TTCN_Buffer& buffer);
84 inline void log_to_hexa(const char *p_prompt, const OCTETSTRING& msg);
92 inline void log_to_hexa(const char *p_prompt, const unsigned char* msg, const size_t msg_size);
100 inline void log_msg(const char *p_prompt, const Base_Type& p_type);
108 inline void log(const char *p_fmt, ...);
109
117 inline void user_msg(const char *p_prompt, const Base_Type& p_type);
125 inline void user(const char *p_fmt, ...);
126
134 inline void warning_msg(const char *p_prompt, const Base_Type& p_type);
142 inline void warning(const char *p_fmt, ...);
143
151 inline void error(const char *p_fmt, ...);
152
159 inline void set_start_time(std::string& p_time_key);
167 inline void set_stop_time(std::string& p_time_key, float& p_time);
168 }; // End of class loggers
169
170 void loggers::log_to_hexa(const char *p_prompt, const TTCN_Buffer & buffer)
171 {
172 TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
173 TTCN_Logger::log_event_str(p_prompt);
174 buffer.log();
175 TTCN_Logger::end_event();
176 }
177
178 void loggers::log_to_hexa(const char *p_prompt, const OCTETSTRING& msg)
179 {
180 TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
181 TTCN_Logger::log_event_str(p_prompt);
182 TTCN_Logger::log_event("Size: %d,\nMsg: ", msg.lengthof());
183
184 for(int i = 0; i < msg.lengthof(); i++) {
185 TTCN_Logger::log_event(" %02x", ((const unsigned char*)msg)[i]);
186 }
187 TTCN_Logger::log_event("\n");
188 TTCN_Logger::end_event();
189 }
190
191 void loggers::log_to_hexa(const char *p_prompt, const unsigned char* msg, const size_t msg_size)
192 {
193 TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
194 TTCN_Logger::log_event_str(p_prompt);
195 for(size_t i = 0; i < msg_size; i++) {
196 TTCN_Logger::log_event(" %02x", *(msg + i));
197 }
198 TTCN_Logger::log_event("\n");
199 TTCN_Logger::end_event();
200 }
201
202 void loggers::log_msg(const char *p_prompt, const Base_Type& p_type)
203 {
204 TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
205 TTCN_Logger::log_event_str(p_prompt);
206 p_type.log();
207 TTCN_Logger::end_event();
208 }
209
210 void loggers::log(const char *p_fmt, ...)
211 {
212 TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED);
213 va_list args;
214 va_start(args, p_fmt);
215 TTCN_Logger::log_event_va_list(p_fmt, args);
216 va_end(args);
217 TTCN_Logger::end_event();
218 }
219
220 void loggers::user_msg(const char *p_prompt, const Base_Type& p_type)
221 {
222 TTCN_Logger::begin_event(TTCN_Logger::USER_UNQUALIFIED);
223 TTCN_Logger::log_event_str(p_prompt);
224 p_type.log();
225 TTCN_Logger::end_event();
226 }
227
228 void loggers::user(const char *p_fmt, ...)
229 {
230 TTCN_Logger::begin_event(TTCN_Logger::USER_UNQUALIFIED);
231 va_list args;
232 va_start(args, p_fmt);
233 TTCN_Logger::log_event_va_list(p_fmt, args);
234 va_end(args);
235 TTCN_Logger::end_event();
236 }
237
238 void loggers::warning(const char *p_fmt, ...)
239 {
240 TTCN_Logger::begin_event(TTCN_Logger::WARNING_UNQUALIFIED);
241 va_list args;
242 va_start(args, p_fmt);
243 TTCN_Logger::log_event_va_list(p_fmt, args);
244 va_end(args);
245 TTCN_Logger::end_event();
246 }
247
248 void loggers::warning_msg(const char *p_prompt, const Base_Type& p_type)
249 {
250 TTCN_Logger::begin_event(TTCN_Logger::WARNING_UNQUALIFIED);
251 TTCN_Logger::log_event_str(p_prompt);
252 p_type.log();
253 TTCN_Logger::end_event();
254 }
255
256 void loggers::error(const char *p_fmt, ...)
257 {
258 va_list args;
259 va_start(args, p_fmt);
260 TTCN_error(p_fmt, args);
261 va_end(args);
262 }
263
264 void loggers::set_start_time(std::string& p_time_key)
265 {
266 _times[p_time_key] = std::clock();
267 }
268
269 void loggers::set_stop_time(std::string& p_time_key, float& p_time)
270 {
271 std::map<std::string, std::clock_t>::iterator it = _times.find(p_time_key);
272 if (it != loggers::_times.end()) {
273 p_time = (std::clock() - _times[p_time_key]) * 1000.0 / CLOCKS_PER_SEC; // in milliseconds
274 _times.erase(it);
275 loggers::get_instance().log_time_exec("%s: Execution duration: %f ms", p_time_key.c_str(), p_time);
276
277 }
278 }
279
280 void loggers::log_time_exec(const char *p_fmt, ...)
281 {
282 TTCN_Logger::begin_event(TTCN_Logger::EXECUTOR_RUNTIME);
283 va_list args;
284 va_start(args, p_fmt);
285 TTCN_Logger::log_event_va_list(p_fmt, args);
286 va_end(args);
287 TTCN_Logger::end_event();
288 }
289
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