1    	// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2    	// vim: ts=8 sw=2 smarttab
3    	
4    	#include "Graylog.h"
5    	#include "common/Formatter.h"
6    	#include "common/LogEntry.h"
7    	#include "log/Entry.h"
8    	#include "log/SubsystemMap.h"
9    	
10   	namespace ceph {
11   	namespace logging {
12   	
13   	Graylog::Graylog(const SubsystemMap * const s, const std::string &logger)
14   	    : m_subs(s),
15   	      m_log_dst_valid(false),
16   	      m_hostname(""),
17   	      m_fsid(""),
18   	      m_logger(std::move(logger)),
19   	      m_ostream_compressed(std::stringstream::in |
20   	                           std::stringstream::out |
21   	                           std::stringstream::binary)
22   	{
23   	  m_formatter = std::unique_ptr<Formatter>(Formatter::create("json"));
24   	  m_formatter_section = std::unique_ptr<Formatter>(Formatter::create("json"));
25   	}
26   	
27   	Graylog::Graylog(const std::string &logger)
28   	    : m_subs(NULL),
29   	      m_log_dst_valid(false),
30   	      m_hostname(""),
31   	      m_fsid(""),
32   	      m_logger(std::move(logger)),
33   	      m_ostream_compressed(std::stringstream::in |
34   	                           std::stringstream::out |
35   	                           std::stringstream::binary)
36   	{
37   	  m_formatter = std::unique_ptr<Formatter>(Formatter::create("json"));
38   	  m_formatter_section = std::unique_ptr<Formatter>(Formatter::create("json"));
39   	}
40   	
41   	Graylog::~Graylog()
42   	{
43   	}
44   	
45   	void Graylog::set_destination(const std::string& host, int port)
46   	{
47   	  try {
48   	    boost::asio::ip::udp::resolver resolver(m_io_service);
49   	    boost::asio::ip::udp::resolver::query query(host, std::to_string(port));
50   	    m_endpoint = *resolver.resolve(query);
51   	    m_log_dst_valid = true;
52   	  } catch (boost::system::system_error const& e) {
53   	    cerr << "Error resolving graylog destination: " << e.what() << std::endl;
54   	    m_log_dst_valid = false;
55   	  }
56   	}
57   	
58   	void Graylog::set_hostname(const std::string& host)
59   	{
60   	  m_hostname = host;
61   	}
62   	
63   	void Graylog::set_fsid(const uuid_d& fsid)
64   	{
65   	  std::vector<char> buf(40);
66   	  fsid.print(&buf[0]);
67   	  m_fsid = std::string(&buf[0]);
68   	}
69   	
70   	void Graylog::log_entry(const Entry& e)
71   	{
72   	  if (m_log_dst_valid) {
73   	    auto s = e.strv();
74   	
75   	    m_formatter->open_object_section("");
76   	    m_formatter->dump_string("version", "1.1");
77   	    m_formatter->dump_string("host", m_hostname);
78   	    m_formatter->dump_string("short_message", s);
79   	    m_formatter->dump_string("_app", "ceph");
80   	    auto t = ceph::logging::log_clock::to_timeval(e.m_stamp);
81   	    m_formatter->dump_float("timestamp", t.tv_sec + (t.tv_usec / 1000000.0));
82   	    m_formatter->dump_unsigned("_thread", (uint64_t)e.m_thread);
83   	    m_formatter->dump_int("_level", e.m_prio);
84   	    if (m_subs != NULL)
85   	    m_formatter->dump_string("_subsys_name", m_subs->get_name(e.m_subsys));
86   	    m_formatter->dump_int("_subsys_id", e.m_subsys);
87   	    m_formatter->dump_string("_fsid", m_fsid);
88   	    m_formatter->dump_string("_logger", m_logger);
89   	    m_formatter->close_section();
90   	
91   	    m_ostream_compressed.clear();
92   	    m_ostream_compressed.str("");
93   	
94   	    m_ostream.reset();
95   	
96   	    m_ostream.push(m_compressor);
97   	    m_ostream.push(m_ostream_compressed);
98   	
99   	    m_formatter->flush(m_ostream);
100  	    m_ostream << std::endl;
101  	
102  	    m_ostream.reset();
103  	
104  	    try {
105  	      boost::asio::ip::udp::socket socket(m_io_service);
106  	      socket.open(m_endpoint.protocol());
107  	      socket.send_to(boost::asio::buffer(m_ostream_compressed.str()), m_endpoint);
108  	    } catch (boost::system::system_error const& e) {
109  	      cerr << "Error sending graylog message: " << e.what() << std::endl;
110  	    }
111  	  }
112  	}
113  	
114  	void Graylog::log_log_entry(LogEntry const * const e)
115  	{
116  	  if (m_log_dst_valid) {
117  	    m_formatter->open_object_section("");
118  	    m_formatter->dump_string("version", "1.1");
119  	    m_formatter->dump_string("host", m_hostname);
120  	    m_formatter->dump_string("short_message", e->msg);
121  	    m_formatter->dump_float("timestamp", e->stamp.sec() + (e->stamp.usec() / 1000000.0));
122  	    m_formatter->dump_string("_app", "ceph");
123  	
124  	    m_formatter->dump_string("name", e->name.to_str());
125  	
126  	    m_formatter_section->open_object_section("rank");
127  	    e->rank.dump(m_formatter_section.get());
128  	    m_formatter_section->close_section();
129  	
130  	    m_formatter_section->open_object_section("addrs");
131  	    e->addrs.dump(m_formatter_section.get());
132  	    m_formatter_section->close_section();
133  	
134  	    m_ostream_section.clear();
135  	    m_ostream_section.str("");
136  	    m_formatter_section->flush(m_ostream_section);
137  	    m_formatter->dump_string("_who", m_ostream_section.str());
138  	
139  	    m_formatter->dump_int("_seq", e->seq);
140  	    m_formatter->dump_string("_prio", clog_type_to_string(e->prio));
141  	    m_formatter->dump_string("_channel", e->channel);
142  	    m_formatter->dump_string("_fsid", m_fsid);
143  	    m_formatter->dump_string("_logger", m_logger);
144  	    m_formatter->close_section();
145  	
146  	    m_ostream_compressed.clear();
147  	    m_ostream_compressed.str("");
148  	
149  	    m_ostream.reset();
150  	
151  	    m_ostream.push(m_compressor);
152  	    m_ostream.push(m_ostream_compressed);
153  	
154  	    m_formatter->flush(m_ostream);
155  	    m_ostream << std::endl;
156  	
157  	    m_ostream.reset();
158  	
159  	    try {
160  	      boost::asio::ip::udp::socket socket(m_io_service);
161  	      socket.open(m_endpoint.protocol());
162  	      socket.send_to(boost::asio::buffer(m_ostream_compressed.str()), m_endpoint);
(1) Event parameter_hidden: declaration hides parameter "e" (declared at line 114)
(2) Event caretline: ^
163  	    } catch (boost::system::system_error const& e) {
164  	      cerr << "Error sending graylog message: " << e.what() << std::endl;
165  	    }
166  	  }
167  	}
168  	
169  	} // ceph::logging::
170  	} // ceph::
171