Commit ce7c45f1 authored by jamesge's avatar jamesge

Make code compile for glog

parent 6d3a6e1e
......@@ -77,6 +77,8 @@ BAIDU_REGISTER_ERRNO(brpc::ECLOSE, "Close socket initiatively");
BAIDU_REGISTER_ERRNO(brpc::EITP, "Bad Itp response");
DECLARE_bool(log_as_json);
namespace brpc {
DEFINE_bool(graceful_quit_on_sigterm, false,
......@@ -128,9 +130,19 @@ Controller::Controller() {
ResetPods();
}
struct SessionKVFlusher {
Controller* cntl;
};
static std::ostream& operator<<(std::ostream& os, const SessionKVFlusher& f) {
f.cntl->FlushSessionKV(os);
return os;
}
Controller::~Controller() {
*g_ncontroller << -1;
FlushSessionKV(LOG_STREAM(INFO));
if (_session_kv != nullptr && _session_kv->Count() != 0) {
LOG(INFO) << SessionKVFlusher{ this };
}
ResetNonPods();
}
......@@ -1509,7 +1521,7 @@ void Controller::FlushSessionKV(std::ostream& os) {
pRID = _http_request->GetHeader(FLAGS_request_id_header);
}
if (::logging::FLAGS_log_as_json) {
if (FLAGS_log_as_json) {
os << "\"M\":\"" BRPC_SESSION_END_MSG "\"";
if (pRID) {
os << ",\"" BRPC_REQ_ID "\":\"" << *pRID << '"';
......@@ -1534,7 +1546,7 @@ Controller::LogPostfixDummy::~LogPostfixDummy() {
std::ostream& operator<<(std::ostream& os, const Controller::LogPostfixDummy& p) {
const_cast<brpc::Controller::LogPostfixDummy&>(p).osptr = &os;
if (::logging::FLAGS_log_as_json) {
if (FLAGS_log_as_json) {
os << "\"M\":\"";
}
return os;
......@@ -1544,14 +1556,14 @@ std::ostream& operator<<(std::ostream& os, const Controller::LogPostfixDummy& p)
Controller::LogPostfixDummy Controller::LogPostfix() const {
Controller::LogPostfixDummy result;
std::string& p = result.postfix;
if (::logging::FLAGS_log_as_json) {
if (FLAGS_log_as_json) {
p.push_back('"');
}
const std::string* pRID = nullptr;
if (_http_request) {
pRID = _http_request->GetHeader(FLAGS_request_id_header);
if (pRID) {
if (::logging::FLAGS_log_as_json) {
if (FLAGS_log_as_json) {
p.append(",\"" BRPC_REQ_ID "\":\"");
p.append(*pRID);
p.push_back('"');
......
......@@ -487,6 +487,9 @@ public:
// LOG(INFO) when this controller is deleted.
KVMap& SessionKV();
// Flush SessionKV() into `os'
void FlushSessionKV(std::ostream& os);
// Contextual prefixes for LOGD/LOGI/LOGW/LOGE/LOGF macros
struct LogPostfixDummy {
LogPostfixDummy() : osptr(nullptr) {}
......@@ -674,9 +677,6 @@ private:
std::string& protocol_param() { return _thrift_method_name; }
const std::string& protocol_param() const { return _thrift_method_name; }
// Flush this->SessionKV() into `os'
void FlushSessionKV(std::ostream& os);
private:
// NOTE: align and group fields to make Controller as compact as possible.
......
......@@ -19,6 +19,9 @@
#include "butil/logging.h"
#include <gflags/gflags.h>
DEFINE_bool(log_as_json, false, "Print log as a valid JSON");
#if !BRPC_WITH_GLOG
#if defined(OS_WIN)
......@@ -87,7 +90,6 @@ typedef pthread_mutex_t* MutexHandle;
#include <vector>
#include <deque>
#include <limits>
#include <gflags/gflags.h>
#include "butil/atomicops.h"
#include "butil/thread_local.h"
#include "butil/scoped_lock.h" // BAIDU_SCOPED_LOCK
......@@ -139,8 +141,6 @@ DEFINE_bool(log_hostname, false, "Add host after pid in each log so"
DEFINE_bool(log_year, false, "Log year in datetime part in each log");
DEFINE_bool(log_as_json, false, "Print log as a valid JSON");
namespace {
LoggingDestination logging_destination = LOG_DEFAULT;
......
......@@ -409,7 +409,6 @@ const LogSeverity BLOG_0 = BLOG_ERROR;
#define VLOG_IS_ON(verbose_level) BAIDU_VLOG_IS_ON(verbose_level, __FILE__)
DECLARE_int32(v);
DECLARE_bool(log_as_json);
extern const int VLOG_UNINITIALIZED;
......
......@@ -74,15 +74,7 @@ TEST_F(ControllerTest, notify_on_destruction) {
ASSERT_TRUE(cancel);
}
/*
class MyFormatter : public brpc::SessionLog::Formatter {
void Print(std::ostream& os, const brpc::SessionLog& log) override {
for (auto it = log.Begin(); it != log.End(); ++it) {
os << '"' << it->first << "\":\"" << it->second << "\",";
}
}
};
*/
#if ! BRPC_WITH_GLOG
static bool endsWith(const std::string& s1, const butil::StringPiece& s2) {
if (s1.size() < s2.size()) {
......@@ -97,8 +89,10 @@ static bool startsWith(const std::string& s1, const butil::StringPiece& s2) {
return memcmp(s1.data(), s2.data(), s2.size()) == 0;
}
DECLARE_bool(log_as_json);
TEST_F(ControllerTest, SessionKV) {
logging::FLAGS_log_as_json = false;
FLAGS_log_as_json = false;
logging::StringSink sink1;
auto oldSink = logging::SetLogSink(&sink1);
//brpc::SetGlobalSessionLogFormatter(new MyFormatter);
......@@ -120,10 +114,11 @@ TEST_F(ControllerTest, SessionKV) {
ASSERT_TRUE(startsWith(sink1, "E")) << sink1;
sink1.clear();
logging::FLAGS_log_as_json = true;
FLAGS_log_as_json = true;
}
ASSERT_TRUE(endsWith(sink1, R"(,"M":"Session ends","@rid":"abcdEFG-456","Baidu":"22","Cisco":"33.300000","Apple":"1"})")) << sink1;
ASSERT_TRUE(startsWith(sink1, R"({"L":"I",)")) << sink1;
logging::SetLogSink(oldSink);
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment