summaryrefslogtreecommitdiffstats
path: root/src/common/Graylog.h
blob: c8c50131999d7b5d4b427d40e178ee0ad06b8b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef __CEPH_LOG_GRAYLOG_H
#define __CEPH_LOG_GRAYLOG_H

#include <boost/asio.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/zlib.hpp>

#include "include/ceph_assert.h"  // boost clobbers this

struct uuid_d;
class LogEntry;

namespace ceph {

class Formatter;

namespace logging {

class Entry;
class SubsystemMap;

// Graylog logging backend: Convert log datastructures (LogEntry, Entry) to
// GELF (http://www.graylog2.org/resources/gelf/specification) and send it
// to a GELF UDP receiver

class Graylog
{
 public:

  /**
   * Create Graylog with SubsystemMap. log_entry will resolve the subsystem
   * id to string. Logging will not be ready until set_destination is called
   * @param s SubsystemMap
   * @param logger Value for key "_logger" in GELF
   */
  Graylog(const SubsystemMap * const s, const std::string &logger);

  /**
   * Create Graylog without SubsystemMap. Logging will not be ready
   * until set_destination is called
   * @param logger Value for key "_logger" in GELF
   */
  explicit Graylog(const std::string &logger);
  virtual ~Graylog();

  void set_hostname(const std::string& host);
  void set_fsid(const uuid_d& fsid);

  void set_destination(const std::string& host, int port);

  void log_entry(const Entry& e);
  void log_log_entry(LogEntry const * const e);

  typedef std::shared_ptr<Graylog> Ref;

 private:
  SubsystemMap const * const m_subs;

  bool m_log_dst_valid = false;

  std::string m_hostname;
  std::string m_fsid;
  std::string m_logger;

  boost::asio::ip::udp::endpoint m_endpoint;
  boost::asio::io_service m_io_service;

  std::unique_ptr<Formatter> m_formatter;
  std::unique_ptr<Formatter> m_formatter_section;
  std::stringstream m_ostream_section;
  std::stringstream m_ostream_compressed;
  boost::iostreams::filtering_ostream m_ostream;
  boost::iostreams::zlib_compressor m_compressor;

};

}
}

#endif