Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add scribe logger support

  • Loading branch information...
commit 29369fb7151b3a7bbda2bbb947005eff148e9bcc 1 parent 9308cf6
@fakechris fakechris authored
Showing with 128 additions and 0 deletions.
  1. +18 −0 cpplog.hpp
  2. +110 −0 scribestream.hpp
View
18 cpplog.hpp
@@ -16,6 +16,10 @@
#include "outputdebugstream.hpp"
#endif
+#ifdef WITH_SCRIBE_LOGGER
+#include "scribestream.hpp"
+#endif
+
// The following #define's will change the behaviour of this library.
// #define CPPLOG_FILTER_LEVEL <level>
// Prevents all log messages with level less than <level> from being emitted.
@@ -418,6 +422,20 @@ namespace cpplog
}
};
+#ifdef WITH_SCRIBE_LOGGER
+ class ScribeLogger : public OstreamLogger
+ {
+ private:
+ scribe_stream m_outStream;
+ public:
+ ScribeLogger(std::string host, unsigned short port, std::string category, int timeout)
+ : OstreamLogger(m_outStream)
+ {
+ m_outStream.open(host, port, category, timeout);
+ }
+ };
+#endif
+
// Tee logger - given two loggers, will forward a message to both.
class TeeLogger : public BaseLogger
{
View
110 scribestream.hpp
@@ -0,0 +1,110 @@
+#pragma once
+#ifndef _SCRIBE_STREAM_H
+#define _SCRIBE_STREAM_H
+
+#include <protocol/TBinaryProtocol.h>
+#include <transport/TSocket.h>
+#include <transport/TTransportUtils.h>
+
+#include "gen-cpp/scribe.h"
+
+class scribe_buf : public std::basic_stringbuf<char>
+{
+private:
+ scribe::thrift::scribeClient* m_client;
+ boost::shared_ptr<apache::thrift::transport::TTransport> m_transport;
+
+ std::string m_host;
+ unsigned short m_port;
+ std::string m_category;
+
+public:
+ scribe_buf() : m_client(NULL)
+ {}
+
+ virtual ~scribe_buf()
+ {
+ close();
+ }
+
+ bool open(std::string& host, unsigned short port, std::string& category, int timeout)
+ {
+ m_host = host;
+ m_port = port;
+ m_category = category;
+ try
+ {
+ boost::shared_ptr<apache::thrift::transport::TSocket> socket(new apache::thrift::transport::TSocket(m_host, m_port));
+ socket->setConnTimeout(timeout);
+ socket->setRecvTimeout(timeout);
+ socket->setSendTimeout(timeout);
+ boost::shared_ptr<apache::thrift::transport::TTransport> transport(new apache::thrift::transport::TFramedTransport(socket));
+ m_transport = transport;
+ boost::shared_ptr<apache::thrift::protocol::TProtocol> proto(new apache::thrift::protocol::TBinaryProtocol(transport));
+ m_client = new scribe::thrift::scribeClient(proto);
+ transport->open();
+ }
+ catch (apache::thrift::TException& tx)
+ {
+ std::cerr << "Open scribe transport failed " << tx.what() << std::endl;
+ return false;
+ }
+ return true;
+ }
+
+ virtual int sync()
+ {
+ if (m_client != NULL && m_transport && m_transport->isOpen())
+ {
+ scribe::thrift::LogEntry entry;
+ entry.category = m_category.c_str();
+ entry.message = str().c_str();
+ std::vector<scribe::thrift::LogEntry> messages;
+ messages.push_back(entry);
+ try
+ {
+ int result = m_client->Log(messages);
+ if (result != scribe::thrift::OK)
+ {
+ std::cerr << "Log to scribe failed: " << result << " " << str().c_str() << std::endl;
+ }
+ }
+ catch (apache::thrift::TException& e)
+ {
+ std::cerr << "Log to scribe exception: " << e.what() << " " << str().c_str() << std::endl;
+ }
+ }
+ return 0;
+ }
+
+private:
+ void close()
+ {
+ if (m_client != NULL)
+ {
+ try
+ {
+ m_transport->close();
+ }
+ catch (apache::thrift::TException& e) {}
+
+ delete m_client;
+ m_client = NULL;
+ }
+ }
+};
+
+class scribe_stream : public std::basic_ostream<char>
+{
+public:
+ scribe_stream() : std::basic_ostream<char>(new scribe_buf()) { }
+ ~scribe_stream() { delete rdbuf(); }
+
+ void open(std::string host, unsigned short port, std::string category, int timeout)
+ {
+ scribe_buf * buf = (scribe_buf*)rdbuf();
+ buf->open(host, port, category, timeout);
+ }
+};
+
+#endif

0 comments on commit 29369fb

Please sign in to comment.
Something went wrong with that request. Please try again.