Skip to content

Commit

Permalink
add example with glog
Browse files Browse the repository at this point in the history
  • Loading branch information
oathdruid committed Jul 25, 2024
1 parent c4bf16d commit ff4904c
Show file tree
Hide file tree
Showing 21 changed files with 230 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Babylon也支持使用[CMake](https://cmake.org)进行构建,并支持通过[f
- [:future](docs/future.md)
- [:logging](docs/logging/index.md)
- [Use async logger](example/use-async-logger)
- [Use with glog](example/use-with-glog)
- [:reusable](docs/reusable/index.md)
- [:serialization](docs/serialization.md)
- [:time](docs/time.md)
Expand Down
2 changes: 1 addition & 1 deletion example/anyflow/MODULE.bazel
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')
2 changes: 1 addition & 1 deletion example/depend-use-bzlmod/MODULE.bazel
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')
2 changes: 1 addition & 1 deletion example/depend-use-bzlmod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ common --registry=https://raw.githubusercontent.com/bazelboost/registry/main
- 增加依赖项
```
# in MODULE.bazel
bazel_dep(name = 'babylon', version = '1.2.2')
bazel_dep(name = 'babylon', version = '1.3.0')
```

- 添加依赖的子模块到编译目标,全部可用子模块可以参照[模块功能文档](../../README.md#模块功能文档),或者[BUILD](../../BUILD)文件,也可以直接添加All in One依赖目标`@babylon`
Expand Down
4 changes: 2 additions & 2 deletions example/depend-use-cmake-fetch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ set(BUILD_DEPS ON)
include(FetchContent)
FetchContent_Declare(
babylon
URL "https://github.com/baidu/babylon/archive/refs/tags/v1.2.2.tar.gz"
URL_HASH SHA256=241d0d3e8bf89c9b47765c794aa5153f73b32a7f419c48d8aeeeb4461cf8aec7
URL "https://github.com/baidu/babylon/archive/refs/tags/v1.3.0.tar.gz"
URL_HASH SHA256=4a8582db91e1931942555400096371586d0cf06ecaac0841aca652ef6d77aef0
)
FetchContent_MakeAvailable(babylon)

Expand Down
4 changes: 2 additions & 2 deletions example/depend-use-cmake-fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ set(BUILD_DEPS ON)
include(FetchContent)
FetchContent_Declare(
babylon
URL "https://github.com/baidu/babylon/archive/refs/tags/v1.2.2.tar.gz"
URL_HASH SHA256=241d0d3e8bf89c9b47765c794aa5153f73b32a7f419c48d8aeeeb4461cf8aec7
URL "https://github.com/baidu/babylon/archive/refs/tags/v1.3.0.tar.gz"
URL_HASH SHA256=4a8582db91e1931942555400096371586d0cf06ecaac0841aca652ef6d77aef0
)
FetchContent_MakeAvailable(babylon)
```
Expand Down
6 changes: 3 additions & 3 deletions example/depend-use-cmake-find/build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/sh
set -ex

URL=https://github.com/baidu/babylon/archive/refs/tags/v1.2.2.tar.gz
NAME=babylon-1.2.2
SHA256=241d0d3e8bf89c9b47765c794aa5153f73b32a7f419c48d8aeeeb4461cf8aec7
URL=https://github.com/baidu/babylon/archive/refs/tags/v1.3.0.tar.gz
NAME=babylon-1.3.0
SHA256=4a8582db91e1931942555400096371586d0cf06ecaac0841aca652ef6d77aef0
if ! echo "$SHA256 $NAME.tar.gz" | sha256sum -c; then
wget $URL --continue -O $NAME.tar.gz
fi
Expand Down
6 changes: 3 additions & 3 deletions example/depend-use-cmake-subdir/build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/sh
set -ex

URL=https://github.com/baidu/babylon/archive/refs/tags/v1.2.2.tar.gz
NAME=babylon-1.2.2
SHA256=241d0d3e8bf89c9b47765c794aa5153f73b32a7f419c48d8aeeeb4461cf8aec7
URL=https://github.com/baidu/babylon/archive/refs/tags/v1.3.0.tar.gz
NAME=babylon-1.3.0
SHA256=4a8582db91e1931942555400096371586d0cf06ecaac0841aca652ef6d77aef0
if ! echo "$SHA256 $NAME.tar.gz" | sha256sum -c; then
wget $URL --continue -O $NAME.tar.gz
fi
Expand Down
2 changes: 1 addition & 1 deletion example/use-arena-with-brpc/MODULE.bazel
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')
1 change: 0 additions & 1 deletion example/use-async-logger/.bazelrc
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
2 changes: 1 addition & 1 deletion example/use-async-logger/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Use concurrent counter optimize bvar
# Use async logger

## 示例构成

Expand Down
23 changes: 14 additions & 9 deletions example/use-async-logger/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "brpc/server.h"
#include "gflags/gflags.h"

#include "spdlog/async.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/spdlog.h"
Expand Down Expand Up @@ -73,10 +72,14 @@ void setup_babylon() {
return ::bvar::Stat {summary.sum, static_cast<ssize_t>(summary.num)};
}
};
static ::bvar::PassiveStatus<size_t> pending_size {"test-babylon-pending", S::get_pending_size, nullptr};
static ::bvar::PassiveStatus<size_t> free_page_num {"test-babylon-free", S::get_free_page_num, nullptr};
static ::bvar::PassiveStatus<::bvar::Stat> hit_summary {S::get_hit_summary, nullptr};
static ::bvar::Window<::bvar::PassiveStatus<::bvar::Stat>> hit_summary_win {"test-babylon-hit", &hit_summary, -1};
static ::bvar::PassiveStatus<size_t> pending_size {
"test-babylon-pending", S::get_pending_size, nullptr};
static ::bvar::PassiveStatus<size_t> free_page_num {
"test-babylon-free", S::get_free_page_num, nullptr};
static ::bvar::PassiveStatus<::bvar::Stat> hit_summary {S::get_hit_summary,
nullptr};
static ::bvar::Window<::bvar::PassiveStatus<::bvar::Stat>> hit_summary_win {
"test-babylon-hit", &hit_summary, -1};
}

void setup_brpc() {
Expand All @@ -94,7 +97,8 @@ void setup_brpc() {
void setup_spdlog() {
::spdlog::set_pattern("%l %Y-%m-%d %H:%M:%S.%f %t %s:%#] %v");
::spdlog::init_thread_pool(262144, 1);
auto async_file = ::spdlog::basic_logger_mt<::spdlog::async_factory>("async_file_logger", FLAGS_benchmark ? "/dev/null" : "log/name.log");
auto async_file = ::spdlog::basic_logger_mt<::spdlog::async_factory>(
"async_file_logger", FLAGS_benchmark ? "/dev/null" : "log/name.log");
::spdlog::set_default_logger(async_file);
}

Expand All @@ -115,14 +119,15 @@ void run_once_spdlog(size_t round) {
void run_loop() {
::bvar::LatencyRecorder latency {"test-" + FLAGS_mode};

int64_t expect_us = 1000.0 * 1000 / FLAGS_qps * FLAGS_batch * FLAGS_concurrency;
int64_t expect_us =
1000.0 * 1000 / FLAGS_qps * FLAGS_batch * FLAGS_concurrency;

void (*run_once)(size_t);
if (FLAGS_mode == "babylon") {
run_once = run_once_babylon;
} else if (FLAGS_mode == "brpc") {
} else if (FLAGS_mode == "brpc") {
run_once = run_once_brpc;
} else if (FLAGS_mode == "spdlog") {
} else if (FLAGS_mode == "spdlog") {
run_once = run_once_spdlog;
}
::std::vector<::std::thread> threads;
Expand Down
2 changes: 1 addition & 1 deletion example/use-counter-with-bvar/MODULE.bazel
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')
2 changes: 1 addition & 1 deletion example/use-with-bthread/MODULE.bazel
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')
5 changes: 5 additions & 0 deletions example/use-with-glog/.bazelrc
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
1 change: 1 addition & 0 deletions example/use-with-glog/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.2.1
8 changes: 8 additions & 0 deletions example/use-with-glog/BUILD
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',
],
)
2 changes: 2 additions & 0 deletions example/use-with-glog/MODULE.bazel
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')
82 changes: 82 additions & 0 deletions example/use-with-glog/README.md
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();
```
4 changes: 4 additions & 0 deletions example/use-with-glog/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -ex

bazel build example
96 changes: 96 additions & 0 deletions example/use-with-glog/example.cpp
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;
}

0 comments on commit ff4904c

Please sign in to comment.