Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
log_message.cpp
Go to the documentation of this file.
3#include <fc/variant.hpp>
4#include <fc/time.hpp>
5#include <fc/filesystem.hpp>
6#include <fc/io/json.hpp>
7
8namespace fc
9{
10 const string& get_thread_name();
11 namespace detail
12 {
14 {
15 public:
17 string file;
19 string method;
21 string task_name;
22 string hostname;
23 string context;
25 };
26
28 {
29 public:
31 :context( std::move(ctx) ){}
33
35 string format;
37 };
38 }
39
40
41
43 :my( std::make_shared<detail::log_context_impl>() ){}
44
45 log_context::log_context( log_level ll, const char* file, uint64_t line,
46 const char* method )
47 :my( std::make_shared<detail::log_context_impl>() )
48 {
49 my->level = ll;
50 my->file = fc::path(file).filename().generic_string(); // TODO truncate filename
51 my->line = line;
52 my->method = method;
53 my->timestamp = time_point::now();
54 my->thread_name = fc::get_thread_name();
55 }
56
58 :my( std::make_shared<detail::log_context_impl>() )
59 {
60 auto obj = v.get_object();
61 my->level = obj["level"].as<log_level>();
62 my->file = obj["file"].as_string();
63 my->line = obj["line"].as_uint64();
64 my->method = obj["method"].as_string();
65 my->hostname = obj["hostname"].as_string();
66 my->thread_name = obj["thread_name"].as_string();
67 if (obj.contains("task_name"))
68 my->task_name = obj["task_name"].as_string();
69 my->timestamp = obj["timestamp"].as<time_point>();
70 if( obj.contains( "context" ) )
71 my->context = obj["context"].as<string>();
72 }
73
75 {
76 return my->thread_name + " " + my->file + ":" + fc::to_string(my->line) + " " + my->method;
77
78 }
79
81 {
82 if (!my->context.empty())
83 my->context += " -> ";
84 my->context += s;
85 }
86
88
89
90 void to_variant( const log_context& l, variant& v )
91 {
92 v = l.to_variant();
93 }
94
95 void from_variant( const variant& l, log_context& c )
96 {
97 c = log_context(l);
98 }
99
101 {
102 c = log_message(l);
103 }
104 void to_variant( const log_message& m, variant& v )
105 {
106 v = m.to_variant();
107 }
108
110 {
111 switch( e )
112 {
113 case log_level::all:
114 v = "all";
115 return;
116 case log_level::debug:
117 v = "debug";
118 return;
119 case log_level::info:
120 v = "info";
121 return;
122 case log_level::warn:
123 v = "warn";
124 return;
125 case log_level::error:
126 v = "error";
127 return;
128 case log_level::off:
129 v = "off";
130 return;
131 }
132 }
133 void from_variant( const variant& v, log_level& e )
134 {
135 try
136 {
137 if( v.as_string() == "all" ) e = log_level::all;
138 else if( v.as_string() == "debug" ) e = log_level::debug;
139 else if( v.as_string() == "info" ) e = log_level::info;
140 else if( v.as_string() == "warn" ) e = log_level::warn;
141 else if( v.as_string() == "error" ) e = log_level::error;
142 else if( v.as_string() == "off" ) e = log_level::off;
143 else FC_THROW_EXCEPTION( bad_cast_exception, "Failed to cast from Variant to log_level" );
144 } FC_RETHROW_EXCEPTIONS( error,
145 "Expected 'all|debug|info|warn|error|off', but got '${variant}'",
146 ("variant",v) );
147 }
148
149 string log_level::to_string()const {
150 switch( value )
151 {
152 case log_level::all:
153 return "all";
154 case log_level::debug:
155 return "debug";
156 case log_level::info:
157 return "info";
158 case log_level::warn:
159 return "warn";
160 case log_level::error:
161 return "error";
162 case log_level::off:
163 return "off";
164 }
165 return "unknown";
166 }
167
168 string log_context::get_file()const { return my->file; }
169 uint64_t log_context::get_line_number()const { return my->line; }
170 string log_context::get_method()const { return my->method; }
171 string log_context::get_thread_name()const { return my->thread_name; }
172 string log_context::get_task_name()const { return my->task_name; }
173 string log_context::get_host_name()const { return my->hostname; }
174 time_point log_context::get_timestamp()const { return my->timestamp; }
175 log_level log_context::get_log_level()const{ return my->level; }
176 string log_context::get_context()const { return my->context; }
177
178
180 {
182 o( "level", variant(my->level) )
183 ( "file", my->file )
184 ( "line", my->line )
185 ( "method", my->method )
186 ( "hostname", my->hostname )
187 ( "thread_name", my->thread_name )
188 ( "timestamp", variant(my->timestamp) );
189
190 if( my->context.size() )
191 o( "context", my->context );
192
193 return o;
194 }
195
198 :my( std::make_shared<detail::log_message_impl>() ){}
199
201 :my( std::make_shared<detail::log_message_impl>(std::move(ctx)) )
202 {
203 my->format = std::move(format);
204 my->args = std::move(args);
205 }
206
208 :my( std::make_shared<detail::log_message_impl>( log_context( v.get_object()["context"] ) ) )
209 {
210 my->format = v.get_object()["format"].as_string();
211 my->args = v.get_object()["data"].get_object();
212 }
213
215 {
216 return mutable_variant_object( "context", my->context )
217 ( "format", my->format )
218 ( "data", my->args );
219 }
220
221 log_context log_message::get_context()const { return my->context; }
222 string log_message::get_format()const { return my->format; }
223 variant_object log_message::get_data()const { return my->args; }
224
226 {
227 return format_string( my->format, my->args );
228 }
229
231 {
232 const bool minimize = true;
233 return format_string( my->format, my->args, minimize );
234 }
235
236
237} // fc
238
log_message_impl(log_context &&ctx)
provides information about where and when a log message was generated.
void append_context(const fc::string &c)
log_level get_log_level() const
string get_method() const
string get_thread_name() const
string get_task_name() const
string get_file() const
time_point get_timestamp() const
uint64_t get_line_number() const
string to_string() const
string get_host_name() const
variant to_variant() const
string get_context() const
string to_string() const
aggregates a message along with the context and associated meta-information.
string get_message() const
string get_limited_message() const
log_context get_context() const
variant to_variant() const
string get_format() const
variant_object get_data() const
An order-preserving dictionary of variants.
wraps boost::filesystem::path to provide platform independent path manipulation.
fc::path filename() const
std::string generic_string() const
static time_point now()
Definition time.cpp:14
An order-preserving dictionary of variants.
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
variant_object & get_object()
Definition variant.cpp:554
string as_string() const
Definition variant.cpp:469
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
Defines types and helper macros necessary for generating log messages.
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
fc::string to_string(double)
Definition string.cpp:131
const string & get_thread_name()
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
fc::string format_string(const fc::string &, const variant_object &, bool minimize=false)
Definition variant.cpp:773
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
Definition name.hpp:106
unsigned __int64 uint64_t
Definition stdint.h:136
cmd_format format
char * s
int l