Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
ce7c45f1
Commit
ce7c45f1
authored
Nov 26, 2020
by
jamesge
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make code compile for glog
parent
6d3a6e1e
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
29 additions
and
23 deletions
+29
-23
controller.cpp
src/brpc/controller.cpp
+17
-5
controller.h
src/brpc/controller.h
+3
-3
logging.cc
src/butil/logging.cc
+3
-3
logging.h
src/butil/logging.h
+0
-1
brpc_controller_unittest.cpp
test/brpc_controller_unittest.cpp
+6
-11
No files found.
src/brpc/controller.cpp
View file @
ce7c45f1
...
...
@@ -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
(
'"'
);
...
...
src/brpc/controller.h
View file @
ce7c45f1
...
...
@@ -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.
...
...
src/butil/logging.cc
View file @
ce7c45f1
...
...
@@ -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
;
...
...
src/butil/logging.h
View file @
ce7c45f1
...
...
@@ -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
;
...
...
test/brpc_controller_unittest.cpp
View file @
ce7c45f1
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment