SObjectizer  5.8
Loading...
Searching...
No Matches
error_logger.cpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \since
7 * v.5.5.0
8 *
9 * \file
10 * \brief Tools for logging error messages inside SObjectizer core.
11 */
12
13#include <so_5/error_logger.hpp>
14
15#include <so_5/compiler_features.hpp>
16
17#include <chrono>
18#include <ctime>
19#include <cstdio>
20#include <iostream>
21
22#include <so_5/current_thread_id.hpp>
23
24#if defined( SO_5_MSVC )
25 #pragma warning(push)
26 // Warning about non-secure localtime and sprintf.
27 #pragma warning(disable: 4996)
28#endif
29
30#if defined( SO_5_CLANG )
31#pragma clang diagnostic push
32#pragma clang diagnostic ignored "-Wdeprecated-declarations"
33#endif
34
35namespace so_5
36{
37
38//
39// stderr_logger_t
40//
41/*!
42 * \since
43 * v.5.5.0
44 *
45 * \brief A standard implementation of error_logger interface.
46 */
48 {
49 public :
52
53 virtual void
54 log(
55 const char * file,
56 unsigned int line,
57 const std::string & message ) override;
58 };
59
60void
62 const char * file,
63 unsigned int line,
64 const std::string & message )
65 {
66 using namespace std;
67 using namespace chrono;
68
69 ostringstream total_message;
70
71 auto now = system_clock::now();
72 auto ms = duration_cast< milliseconds >( now.time_since_epoch() );
73 time_t unix_time = duration_cast< seconds >( ms ).count();
74
75 char date_time_first_part[ 64 ];
76 strftime( date_time_first_part, sizeof( date_time_first_part ) - 1,
77 "%Y-%m-%d %H:%M:%S", localtime( &unix_time ) );
78 char date_time_second_part[ 16 ];
79 sprintf( date_time_second_part, ".%03u",
80 static_cast< unsigned int >( ms.count() % 1000u ) );
81
82 total_message << "[" << date_time_first_part
83 << date_time_second_part
84 << " TID:" << query_current_thread_id()
85 << "] " << message
86 << " (" << file << ":" << line
87 << ")\n";
88
89 cerr << total_message.str();
90 }
91
92//
93// create_stderr_logger
94//
97 {
98 return error_logger_shptr_t( new stderr_logger_t() );
99 }
100
101} /* namespace so_5 */
102
103#if defined( SO_5_CLANG )
104#pragma clang diagnostic pop
105#endif
106
107#if defined( SO_5_MSVC )
108 #pragma warning(pop)
109#endif
An interface for logging error messages.
A standard implementation of error_logger interface.
virtual void log(const char *file, unsigned int line, const std::string &message) override
A method for logging message.
#define SO_5_FUNC
Definition declspec.hpp:48
Private part of message limit implementation.
Definition agent.cpp:33
SO_5_FUNC error_logger_shptr_t create_stderr_logger()
A factory for creating error_logger implemenation which uses std::stderr as log stream.