This is an automated email from the ASF dual-hosted git repository.
jamesge pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-brpc.git
The following commit(s) were added to refs/heads/master by this push:
new ce7c45f Make code compile for glog
ce7c45f is described below
commit ce7c45f182d2ffa954665d6438ba2b877f5c4758
Author: jamesge <[email protected]>
AuthorDate: Thu Nov 26 19:34:04 2020 +0800
Make code compile for glog
---
src/brpc/controller.cpp | 22 +++++++++++++++++-----
src/brpc/controller.h | 6 +++---
src/butil/logging.cc | 6 +++---
src/butil/logging.h | 1 -
test/brpc_controller_unittest.cpp | 17 ++++++-----------
5 files changed, 29 insertions(+), 23 deletions(-)
diff --git a/src/brpc/controller.cpp b/src/brpc/controller.cpp
index 92f717d..0ef6004 100644
--- a/src/brpc/controller.cpp
+++ b/src/brpc/controller.cpp
@@ -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('"');
diff --git a/src/brpc/controller.h b/src/brpc/controller.h
index 89250a7..8a75c54 100755
--- a/src/brpc/controller.h
+++ b/src/brpc/controller.h
@@ -486,6 +486,9 @@ public:
// Get the object to write key/value which will be flushed into
// 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 {
@@ -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.
diff --git a/src/butil/logging.cc b/src/butil/logging.cc
index 26e6259..c283ce1 100644
--- a/src/butil/logging.cc
+++ b/src/butil/logging.cc
@@ -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;
diff --git a/src/butil/logging.h b/src/butil/logging.h
index 20754b0..f92cd78 100644
--- a/src/butil/logging.h
+++ b/src/butil/logging.h
@@ -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;
diff --git a/test/brpc_controller_unittest.cpp
b/test/brpc_controller_unittest.cpp
index bf79b97..011928c 100644
--- a/test/brpc_controller_unittest.cpp
+++ b/test/brpc_controller_unittest.cpp
@@ -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
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]