-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
230 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
bazel_dep(name = 'babylon', version = '1.2.2') | ||
bazel_dep(name = 'babylon', version = '1.3.0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
bazel_dep(name = 'babylon', version = '1.2.2') | ||
bazel_dep(name = 'babylon', version = '1.3.0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
bazel_dep(name = 'babylon', version = '1.2.2') | ||
bazel_dep(name = 'babylon', version = '1.3.0') | ||
bazel_dep(name = 'brpc', version = '1.9.0') | ||
bazel_dep(name = 'tcmalloc', version = '0.0.0-20240411-5ed309d') | ||
single_version_override(module_name = 'protobuf', version = '25.3.arenastring') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
common --registry=https://bcr.bazel.build | ||
common --registry=https://baidu.github.io/babylon/registry | ||
common --registry=file:///home/oathdruid/src/babylon/registry | ||
common --registry=https://raw.githubusercontent.com/bazelboost/registry/main | ||
|
||
build --compilation_mode opt --copt=-O3 --cxxopt=-std=c++17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Use concurrent counter optimize bvar | ||
# Use async logger | ||
|
||
## 示例构成 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
bazel_dep(name = 'babylon', version = '1.2.2') | ||
bazel_dep(name = 'babylon', version = '1.3.0') | ||
bazel_dep(name = 'brpc', version = '1.9.0') | ||
bazel_dep(name = 'tcmalloc', version = '0.0.0-20240411-5ed309d') | ||
single_version_override(module_name = 'protobuf', version = '3.19.6') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
bazel_dep(name = 'babylon', version = '1.2.2') | ||
bazel_dep(name = 'babylon', version = '1.3.0') | ||
bazel_dep(name = 'brpc', version = '1.9.0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
common --registry=https://bcr.bazel.build | ||
common --registry=https://baidu.github.io/babylon/registry | ||
common --registry=https://raw.githubusercontent.com/bazelboost/registry/main | ||
|
||
build --cxxopt=-std=c++17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7.2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cc_binary( | ||
name = 'example', | ||
srcs = ['example.cpp'], | ||
deps = [ | ||
'@babylon//:logging', | ||
'@glog', | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bazel_dep(name = 'babylon', version = '1.3.0') | ||
bazel_dep(name = 'glog', version = '0.7.1') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Use with glog | ||
|
||
## glog to babylon | ||
|
||
程序整体日志系统使用babylon::LoggerManager,但部分不方便修改的子模块采用glog作为日志接口,可以采用定制google::LogSink的方法将日志重定向到babylon::LoggerManager日志框架 | ||
|
||
```c++ | ||
class BabylonLogSink : public ::google::LogSink { | ||
public: | ||
virtual void send(::google::LogSeverity glog_severity, | ||
const char* full_filename, const char*, | ||
int line, const ::google::LogMessageTime&, | ||
const char* message, size_t message_len) noexcept override { | ||
// severity mapping | ||
static ::babylon::LogSeverity mapping[] = { | ||
[::google::INFO] = ::babylon::LogSeverity::INFO, | ||
[::google::WARNING] = ::babylon::LogSeverity::WARNING, | ||
[::google::ERROR] = ::babylon::LogSeverity::FATAL, | ||
[::google::FATAL] = ::babylon::LogSeverity::FATAL, | ||
}; | ||
|
||
// root logger severity pre-check | ||
auto severity = mapping[glog_severity]; | ||
auto& logger = ::babylon::LoggerManager::instance().get_root_logger(); | ||
if (logger.min_severity() > severity) { | ||
return; | ||
} | ||
|
||
// write to root logger | ||
auto& stream = logger.stream(severity, full_filename, line); | ||
stream.begin(); | ||
stream.write(message, message_len); | ||
stream.end(); | ||
} | ||
}; | ||
|
||
// glog -> babylon sink -> babylon | ||
BabylonLogSink sink; | ||
::google::AddLogSink(&sink); | ||
``` | ||
## babylon to glog | ||
程序整体日志系统使用glog,可以采用定制babylon::LogStream的方法将babylon内部日志重定向到glog | ||
```c++ | ||
class GLogStream : public ::babylon::LogStream { | ||
public: | ||
GLogStream() noexcept : LogStream(*reinterpret_cast<std::streambuf*>(0)) {} | ||
virtual void do_begin() noexcept override { | ||
// severity mapping | ||
static ::google::LogSeverity mapping[] = { | ||
[::babylon::LogSeverity::DEBUG] = ::google::INFO, | ||
[::babylon::LogSeverity::INFO] = ::google::INFO, | ||
[::babylon::LogSeverity::WARNING] = ::google::WARNING, | ||
[::babylon::LogSeverity::FATAL] = ::google::FATAL, | ||
}; | ||
// get underlying std::streambuf from glog. set to babylon::LogStream | ||
auto& message = reinterpret_cast<::google::LogMessage&>(_message_storage); | ||
new (&message) ::google::LogMessage(file().data(), line(), mapping[severity()]); | ||
rdbuf(message.stream().rdbuf()); | ||
} | ||
virtual void do_end() noexcept override { | ||
// destruct google::LogMessage to flush | ||
auto& message = reinterpret_cast<::google::LogMessage&>(_message_storage); | ||
message.~LogMessage(); | ||
} | ||
::std::aligned_storage<sizeof(::google::LogMessage), alignof(::google::LogMessage)>::type _message_storage; | ||
}; | ||
// babylon -> logger -> glog stream -> glog | ||
::babylon::LoggerBuilder builder; | ||
builder.set_log_stream_creator([] { | ||
return ::std::make_unique<GLogStream>(); | ||
}); | ||
::babylon::LoggerManager::instance().set_root_builder(::std::move(builder)); | ||
::babylon::LoggerManager::instance().apply(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
set -ex | ||
|
||
bazel build example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include "babylon/logging/logger.h" | ||
#include "gflags/gflags.h" | ||
#include "glog/logging.h" | ||
|
||
class BabylonLogSink : public ::google::LogSink { | ||
public: | ||
virtual void send(::google::LogSeverity glog_severity, | ||
const char* full_filename, const char*, | ||
int line, const ::google::LogMessageTime&, | ||
const char* message, size_t message_len) noexcept override { | ||
// severity mapping | ||
static ::babylon::LogSeverity mapping[] = { | ||
[::google::INFO] = ::babylon::LogSeverity::INFO, | ||
[::google::WARNING] = ::babylon::LogSeverity::WARNING, | ||
[::google::ERROR] = ::babylon::LogSeverity::FATAL, | ||
[::google::FATAL] = ::babylon::LogSeverity::FATAL, | ||
}; | ||
|
||
// root logger severity pre-check | ||
auto severity = mapping[glog_severity]; | ||
auto& logger = ::babylon::LoggerManager::instance().get_root_logger(); | ||
if (logger.min_severity() > severity) { | ||
return; | ||
} | ||
|
||
// write to root logger | ||
auto& stream = logger.stream(severity, full_filename, line); | ||
stream.begin(); | ||
stream.write(message, message_len); | ||
stream.end(); | ||
} | ||
}; | ||
|
||
class GLogStream : public ::babylon::LogStream { | ||
public: | ||
GLogStream() noexcept : LogStream(*reinterpret_cast<std::streambuf*>(0)) {} | ||
|
||
virtual void do_begin() noexcept override { | ||
// severity mapping | ||
static ::google::LogSeverity mapping[] = { | ||
[::babylon::LogSeverity::DEBUG] = ::google::INFO, | ||
[::babylon::LogSeverity::INFO] = ::google::INFO, | ||
[::babylon::LogSeverity::WARNING] = ::google::WARNING, | ||
[::babylon::LogSeverity::FATAL] = ::google::FATAL, | ||
}; | ||
|
||
// get underlying std::streambuf from glog. set to babylon::LogStream | ||
auto& message = reinterpret_cast<::google::LogMessage&>(_message_storage); | ||
new (&message) ::google::LogMessage(file().data(), line(), mapping[severity()]); | ||
rdbuf(message.stream().rdbuf()); | ||
} | ||
|
||
virtual void do_end() noexcept override { | ||
// destruct google::LogMessage to flush | ||
auto& message = reinterpret_cast<::google::LogMessage&>(_message_storage); | ||
message.~LogMessage(); | ||
} | ||
|
||
::std::aligned_storage<sizeof(::google::LogMessage), alignof(::google::LogMessage)>::type _message_storage; | ||
}; | ||
|
||
int main(int argc, char* argv[]) { | ||
::gflags::ParseCommandLineFlags(&argc, &argv, true); | ||
|
||
::gflags::SetCommandLineOption( | ||
"stderrthreshold", ::std::to_string(::google::NUM_SEVERITIES).c_str()); | ||
::google::InitGoogleLogging(argv[0]); | ||
|
||
// glog -> babylon sink -> babylon | ||
BabylonLogSink sink; | ||
::google::AddLogSink(&sink); | ||
|
||
LOG(INFO) << "1 glog to babylon"; | ||
LOG(WARNING) << "2 glog to babylon"; | ||
LOG(ERROR) << "3 glog to babylon"; | ||
//LOG(FATAL) << "4 glog to babylon"; | ||
|
||
::google::RemoveLogSink(&sink); | ||
|
||
::gflags::SetCommandLineOption("alsologtostderr", "true"); | ||
|
||
// babylon -> logger -> glog stream -> glog | ||
::babylon::LoggerBuilder builder; | ||
builder.set_log_stream_creator([] { | ||
return ::std::make_unique<GLogStream>(); | ||
}); | ||
::babylon::LoggerManager::instance().set_root_builder(::std::move(builder)); | ||
::babylon::LoggerManager::instance().apply(); | ||
|
||
BABYLON_LOG(DEBUG) << "1 babylon to glog"; | ||
BABYLON_LOG(INFO) << "2 babylon to glog"; | ||
BABYLON_LOG(WARNING) << "3 babylon to glog"; | ||
//BABYLON_LOG(FATAL) << "4 babylon to glog"; | ||
|
||
return 0; | ||
} |