BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
gelf_appender.cpp
Go to the documentation of this file.
2 #include <fc/network/ip.hpp>
3 #include <fc/network/resolve.hpp>
6 #include <fc/reflect/variant.hpp>
7 #include <fc/thread/thread.hpp>
8 #include <fc/variant.hpp>
9 #include <fc/io/json.hpp>
10 #include <fc/crypto/city.hpp>
11 #include <fc/compress/zlib.hpp>
12 
13 #include <boost/lexical_cast.hpp>
14 #include <iomanip>
15 #include <iostream>
16 #include <queue>
17 #include <sstream>
18 #include <iostream>
19 
20 namespace fc
21 {
22 
24  {
25  public:
29 
30  impl(const config& c) :
31  cfg(c)
32  {
33  }
34 
36  {
37  }
38  };
39 
41  my( new impl( args.as<config>( FC_MAX_LOG_OBJECT_DEPTH ) ) )
42  {
43  try
44  {
45  try
46  {
47  // if it's a numeric address:port, this will parse it
48  my->gelf_endpoint = ip::endpoint::from_string(my->cfg.endpoint);
49  }
50  catch (...)
51  {
52  }
53  if (!my->gelf_endpoint)
54  {
55  // couldn't parse as a numeric ip address, try resolving as a DNS name.
56  // This can yield, so don't do it in the catch block above
57  string::size_type colon_pos = my->cfg.endpoint.find(':');
58  try
59  {
60  uint16_t port = boost::lexical_cast<uint16_t>(my->cfg.endpoint.substr(colon_pos + 1, my->cfg.endpoint.size()));
61 
62  string hostname = my->cfg.endpoint.substr( 0, colon_pos );
63  std::vector<ip::endpoint> endpoints = resolve(hostname, port);
64  if (endpoints.empty())
65  FC_THROW_EXCEPTION(unknown_host_exception, "The host name can not be resolved: ${hostname}",
66  ("hostname", hostname));
67  my->gelf_endpoint = endpoints.back();
68  }
69  catch (const boost::bad_lexical_cast&)
70  {
71  FC_THROW("Bad port: ${port}", ("port", my->cfg.endpoint.substr(colon_pos + 1, my->cfg.endpoint.size())));
72  }
73  }
74 
75  if (my->gelf_endpoint)
76  my->gelf_socket.open();
77  }
78  catch (...)
79  {
80  std::cerr << "error opening GELF socket to endpoint ${endpoint}" << my->cfg.endpoint << "\n";
81  }
82  }
83 
85  {}
86 
87  void gelf_appender::log(const log_message& message)
88  {
89  if (!my->gelf_endpoint)
90  return;
91 
92  log_context context = message.get_context();
93 
94  mutable_variant_object gelf_message;
95  gelf_message["version"] = "1.1";
96  gelf_message["host"] = my->cfg.host;
97  gelf_message["short_message"] = format_string( message.get_format(), message.get_data(), my->cfg.max_object_depth );
98 
99  gelf_message["timestamp"] = context.get_timestamp().time_since_epoch().count() / 1000000.;
100 
101  switch (context.get_log_level())
102  {
103  case log_level::debug:
104  gelf_message["level"] = 7; // debug
105  break;
106  case log_level::info:
107  gelf_message["level"] = 6; // info
108  break;
109  case log_level::warn:
110  gelf_message["level"] = 4; // warning
111  break;
112  case log_level::error:
113  gelf_message["level"] = 3; // error
114  break;
115  case log_level::all:
116  case log_level::off:
117  // these shouldn't be used in log messages, but do something deterministic just in case
118  gelf_message["level"] = 6; // info
119  break;
120  }
121 
122  if (!context.get_context().empty())
123  gelf_message["context"] = context.get_context();
124  gelf_message["_line"] = context.get_line_number();
125  gelf_message["_file"] = context.get_file();
126  gelf_message["_method_name"] = context.get_method();
127  gelf_message["_thread_name"] = context.get_thread_name();
128  if (!context.get_task_name().empty())
129  gelf_message["_task_name"] = context.get_task_name();
130 
131  string gelf_message_as_string;
132  try
133  {
134  gelf_message_as_string = json::to_string(gelf_message);
135  }
136  catch( const fc::assert_exception& e )
137  {
138  gelf_message_as_string = "{\"level\":3,\"short_message\":\"ERROR while generating log message\"}";
139  }
140  gelf_message_as_string = zlib_compress(gelf_message_as_string);
141 
142  // graylog2 expects the zlib header to be 0x78 0x9c
143  // but miniz.c generates 0x78 0x01 (indicating
144  // low compression instead of default compression)
145  // so change that here
146  assert(gelf_message_as_string[0] == (char)0x78);
147  if (gelf_message_as_string[1] == (char)0x01 ||
148  gelf_message_as_string[1] == (char)0xda)
149  gelf_message_as_string[1] = (char)0x9c;
150  assert(gelf_message_as_string[1] == (char)0x9c);
151 
152  // packets are sent by UDP, and they tend to disappear if they
153  // get too large. It's hard to find any solid numbers on how
154  // large they can be before they get dropped -- datagrams can
155  // be up to 64k, but anything over 512 is not guaranteed.
156  // You can play with this number, intermediate values like
157  // 1400 and 8100 are likely to work on most intranets.
158  const unsigned max_payload_size = 512;
159 
160  if (gelf_message_as_string.size() <= max_payload_size)
161  {
162  // no need to split
163  std::shared_ptr<char> send_buffer(new char[gelf_message_as_string.size()],
164  [](char* p){ delete[] p; });
165  memcpy(send_buffer.get(), gelf_message_as_string.c_str(),
166  gelf_message_as_string.size());
167 
168  my->gelf_socket.send_to(send_buffer, gelf_message_as_string.size(),
169  *my->gelf_endpoint);
170  }
171  else
172  {
173  // split the message
174  // we need to generate an 8-byte ID for this message.
175  // city hash should do
176  uint64_t message_id = city_hash64(gelf_message_as_string.c_str(), gelf_message_as_string.size());
177  const unsigned header_length = 2 /* magic */ + 8 /* msg id */ + 1 /* seq */ + 1 /* count */;
178  const unsigned body_length = max_payload_size - header_length;
179  unsigned total_number_of_packets = (gelf_message_as_string.size() + body_length - 1) / body_length;
180  unsigned bytes_sent = 0;
181  unsigned number_of_packets_sent = 0;
182  while (bytes_sent < gelf_message_as_string.size())
183  {
184  unsigned bytes_to_send = std::min((unsigned)gelf_message_as_string.size() - bytes_sent,
185  body_length);
186 
187  std::shared_ptr<char> send_buffer(new char[max_payload_size],
188  [](char* p){ delete[] p; });
189  char* ptr = send_buffer.get();
190  // magic number for chunked message
191  *(unsigned char*)ptr++ = 0x1e;
192  *(unsigned char*)ptr++ = 0x0f;
193 
194  // message id
195  memcpy(ptr, (char*)&message_id, sizeof(message_id));
196  ptr += sizeof(message_id);
197 
198  *(unsigned char*)(ptr++) = number_of_packets_sent;
199  *(unsigned char*)(ptr++) = total_number_of_packets;
200  memcpy(ptr, gelf_message_as_string.c_str() + bytes_sent,
201  bytes_to_send);
202  my->gelf_socket.send_to(send_buffer, header_length + bytes_to_send,
203  *my->gelf_endpoint);
204  ++number_of_packets_sent;
205  bytes_sent += bytes_to_send;
206  }
207  assert(number_of_packets_sent == total_number_of_packets);
208  }
209  }
210 } // fc
fc::log_message::get_data
variant_object get_data() const
Definition: log_message.cpp:211
fc::log_level::error
@ error
Definition: log_message.hpp:38
ip.hpp
fc::gelf_appender::impl::gelf_endpoint
optional< ip::endpoint > gelf_endpoint
Definition: gelf_appender.cpp:27
fc::udp_socket
Definition: udp_socket.hpp:14
fc::log_level::all
@ all
Definition: log_message.hpp:34
fc::mutable_variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:108
variant.hpp
fc::log_level::warn
@ warn
Definition: log_message.hpp:37
fc::log_level::debug
@ debug
Definition: log_message.hpp:35
fc::log_message::get_format
std::string get_format() const
Definition: log_message.cpp:210
fc::gelf_appender::~gelf_appender
~gelf_appender()
Definition: gelf_appender.cpp:84
fc::log_level::off
@ off
Definition: log_message.hpp:39
fc
Definition: api.hpp:15
FC_THROW
#define FC_THROW( ...)
Definition: exception.hpp:366
fc::gelf_appender::impl::cfg
config cfg
Definition: gelf_appender.cpp:26
fc::ip::endpoint::from_string
static endpoint from_string(const string &s)
Definition: ip.cpp:81
FC_MAX_LOG_OBJECT_DEPTH
#define FC_MAX_LOG_OBJECT_DEPTH
Definition: config.hpp:8
fc::json::to_string
static string to_string(const variant &v, output_formatting format=stringify_large_ints_and_doubles, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:650
zlib.hpp
fc::context
Definition: context.hpp:47
fc::city_hash64
uint64_t city_hash64(const char *buf, size_t len)
Definition: city.cpp:377
fc::zlib_compress
std::string zlib_compress(const std::string &in)
Definition: zlib.cpp:7
fc::gelf_appender::impl::gelf_socket
udp_socket gelf_socket
Definition: gelf_appender.cpp:28
thread.hpp
fc::log_context
provides information about where and when a log message was generated.
Definition: log_message.hpp:56
fc::gelf_appender::gelf_appender
gelf_appender(const variant &args)
Definition: gelf_appender.cpp:40
fc::gelf_appender::log
virtual void log(const log_message &m) override
Definition: gelf_appender.cpp:87
fc::cerr
cerr_t & cerr
Definition: iostream.cpp:176
fc::format_string
std::string format_string(const std::string &, const variant_object &, uint32_t max_object_depth=200)
json.hpp
fc::log_message
aggregates a message along with the context and associated meta-information.
Definition: log_message.hpp:106
city.hpp
fc::appender::ptr
std::shared_ptr< appender > ptr
Definition: appender.hpp:30
fc::gelf_appender::config
Definition: gelf_appender.hpp:14
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
exception.hpp
Defines exception's used by fc.
resolve.hpp
fc::resolve
std::vector< fc::ip::endpoint > resolve(const std::string &host, uint16_t port)
Definition: resolve.cpp:7
fc::log_level::info
@ info
Definition: log_message.hpp:36
variant.hpp
gelf_appender.hpp
fc::gelf_appender::impl::impl
impl(const config &c)
Definition: gelf_appender.cpp:30
fc::optional
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
udp_socket.hpp
FC_THROW_EXCEPTION
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
Definition: exception.hpp:379
fc::log_message::get_context
log_context get_context() const
Definition: log_message.cpp:209
fc::gelf_appender::impl::~impl
~impl()
Definition: gelf_appender.cpp:35
fc::gelf_appender::impl
Definition: gelf_appender.cpp:23