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 df031bee Support -escape_log
df031bee is described below
commit df031bee2c5370fb471744b220259ca1a6164c4a
Author: gejun.0 <[email protected]>
AuthorDate: Tue Aug 30 16:57:54 2022 +0800
Support -escape_log
---
src/butil/logging.cc | 29 +++++++++++++++++++++++++++--
1 file changed, 27 insertions(+), 2 deletions(-)
diff --git a/src/butil/logging.cc b/src/butil/logging.cc
index 09b3ddaf..fa0dbae8 100644
--- a/src/butil/logging.cc
+++ b/src/butil/logging.cc
@@ -21,6 +21,7 @@
#include <gflags/gflags.h>
DEFINE_bool(log_as_json, false, "Print log as a valid JSON");
+DEFINE_bool(escape_log, false, "Escape log content before printing");
#if !BRPC_WITH_GLOG
@@ -565,12 +566,36 @@ static void PrintLogPrefixAsJSON(
os << "\"C\":\"" << file << ':' << line << "\"";
}
+void EscapeJson(std::ostream& os, const butil::StringPiece& s) {
+ for (auto it = s.begin(); it != s.end(); it++) {
+ auto c = *it;
+ switch (c) {
+ case '"': os << "\\\""; break;
+ case '\\': os << "\\\\"; break;
+ case '\b': os << "\\b"; break;
+ case '\f': os << "\\f"; break;
+ case '\n': os << "\\n"; break;
+ case '\r': os << "\\r"; break;
+ case '\t': os << "\\t"; break;
+ default: os << c;
+ }
+ }
+}
+
+inline void OutputLog(std::ostream& os, const butil::StringPiece& s) {
+ if (FLAGS_escape_log) {
+ EscapeJson(os, s);
+ } else {
+ os.write(s.data(), s.length());
+ }
+}
+
void PrintLog(std::ostream& os,
int severity, const char* file, int line,
const butil::StringPiece& content) {
if (!FLAGS_log_as_json) {
PrintLogPrefix(os, severity, file, line);
- os.write(content.data(), content.size());
+ OutputLog(os, content);
} else {
os << '{';
PrintLogPrefixAsJSON(os, severity, file, line);
@@ -582,7 +607,7 @@ void PrintLog(std::ostream& os,
} else {
os << ',';
}
- os.write(content.data(), content.size());
+ OutputLog(os, content);
if (pair_quote) {
os << '"';
} else if (!content.empty() && content[content.size()-1] != '"') {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]