Skip to content

Commit 94e78e3

Browse files
committed
Add a shorthand method build_arc to builder structs (#107)
1 parent e8ebe72 commit 94e78e3

25 files changed

+305
-228
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ jobs:
250250
journalctl --no-pager -o json -t native_linux | jq -e -s $'.[1].PRIORITY == "3" and .[1].CODE_FILE == null and .[1].CODE_LINE == null and .[1].TID != null'
251251
252252
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[0].MESSAGE == "[demo] [info] info message from spdlog-rs\'s JournaldSink\n"'
253-
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[0].PRIORITY == "6" and .[0].CODE_FILE == "linux.rs" and .[0].CODE_LINE == "15" and .[0].TID != null'
253+
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[0].PRIORITY == "6" and .[0].CODE_FILE == "linux.rs" and .[0].CODE_LINE != null and .[0].TID != null'
254254
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[1].MESSAGE == "[demo] [error] error message from spdlog-rs\'s JournaldSink { error_code=114514 }\n"'
255-
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[1].PRIORITY == "3" and .[1].CODE_FILE == "linux.rs" and .[1].CODE_LINE == "16" and .[1].TID != null'
255+
journalctl --no-pager -o json -t native_linux_srcloc | jq -e -s $'.[1].PRIORITY == "3" and .[1].CODE_FILE == "linux.rs" and .[1].CODE_LINE != null and .[1].TID != null'
256256
257257
test-native-windows:
258258
strategy:

spdlog/benches/spdlog-rs/cmp_cpp.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate test;
55
#[path = "../common/mod.rs"]
66
mod common;
77

8-
use std::{env, sync::Arc, thread, time::Instant};
8+
use std::{env, thread, time::Instant};
99

1010
use clap::Parser;
1111
use spdlog::{
@@ -36,38 +36,38 @@ fn bench_threaded_logging(threads: usize, iters: usize) {
3636
info!("**********************************************************************");
3737

3838
let logger = build_test_logger(|b| {
39-
b.sink(Arc::new(
39+
b.sink(
4040
FileSink::builder()
4141
.path(common::BENCH_LOGS_PATH.join("FileSink.log"))
4242
.truncate(true)
43-
.build()
43+
.build_arc()
4444
.unwrap(),
45-
))
45+
)
4646
.name("basic_mt")
4747
});
4848
bench_mt(logger, threads, iters);
4949

5050
let logger = build_test_logger(|b| {
51-
b.sink(Arc::new(
51+
b.sink(
5252
RotatingFileSink::builder()
5353
.base_path(common::BENCH_LOGS_PATH.join("RotatingFileSink_FileSize.log"))
5454
.rotation_policy(RotationPolicy::FileSize(FILE_SIZE))
5555
.max_files(ROTATING_FILES)
56-
.build()
56+
.build_arc()
5757
.unwrap(),
58-
))
58+
)
5959
.name("rotating_mt")
6060
});
6161
bench_mt(logger, threads, iters);
6262

6363
let logger = build_test_logger(|b| {
64-
b.sink(Arc::new(
64+
b.sink(
6565
RotatingFileSink::builder()
6666
.base_path(common::BENCH_LOGS_PATH.join("RotatingFileSink_Daily.log"))
6767
.rotation_policy(RotationPolicy::Daily { hour: 0, minute: 0 })
68-
.build()
68+
.build_arc()
6969
.unwrap(),
70-
))
70+
)
7171
.name("daily_mt")
7272
});
7373
bench_mt(logger, threads, iters);

spdlog/benches/spdlog-rs/cmp_cpp_async.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate test;
55
#[path = "../common/mod.rs"]
66
mod common;
77

8-
use std::{cmp, env, num::NonZeroUsize, sync::Arc, thread, time::Instant};
8+
use std::{cmp, env, num::NonZeroUsize, thread, time::Instant};
99

1010
use clap::Parser;
1111
use spdlog::{
@@ -32,25 +32,24 @@ fn bench(
3232

3333
let queue_size = NonZeroUsize::new(queue_size).unwrap();
3434
for _ in 0..iters {
35-
let thread_pool = Arc::new(ThreadPool::builder().capacity(queue_size).build().unwrap());
36-
37-
let file_sink = Arc::new(
38-
FileSink::builder()
39-
.path(common::BENCH_LOGS_PATH.join(file_name))
40-
.truncate(true)
41-
.build()
42-
.unwrap(),
43-
);
44-
45-
let async_sink = Arc::new(
46-
AsyncPoolSink::builder()
47-
.thread_pool(thread_pool)
48-
.overflow_policy(policy)
49-
.sink(file_sink)
50-
.error_handler(|err| panic!("an error occurred: {err}"))
51-
.build()
52-
.unwrap(),
53-
);
35+
let thread_pool = ThreadPool::builder()
36+
.capacity(queue_size)
37+
.build_arc()
38+
.unwrap();
39+
40+
let file_sink = FileSink::builder()
41+
.path(common::BENCH_LOGS_PATH.join(file_name))
42+
.truncate(true)
43+
.build_arc()
44+
.unwrap();
45+
46+
let async_sink = AsyncPoolSink::builder()
47+
.thread_pool(thread_pool)
48+
.overflow_policy(policy)
49+
.sink(file_sink)
50+
.error_handler(|err| panic!("an error occurred: {err}"))
51+
.build_arc()
52+
.unwrap();
5453

5554
let logger = Logger::builder()
5655
.sink(async_sink)

spdlog/benches/spdlog-rs/kv.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ extern crate test;
55
#[path = "../common/mod.rs"]
66
mod common;
77

8-
use std::sync::Arc;
9-
108
use spdlog::{prelude::*, sink::*};
119
use test::Bencher;
1210

@@ -18,13 +16,11 @@ use test_utils::*;
1816

1917
fn logger(name: &str) -> Logger {
2018
let path = common::BENCH_LOGS_PATH.join(format!("kv_{name}.log"));
21-
let sink = Arc::new(
22-
FileSink::builder()
23-
.path(path)
24-
.truncate(true)
25-
.build()
26-
.unwrap(),
27-
);
19+
let sink = FileSink::builder()
20+
.path(path)
21+
.truncate(true)
22+
.build_arc()
23+
.unwrap();
2824
build_test_logger(|b| b.sink(sink))
2925
}
3026

spdlog/benches/spdlog-rs/spdlog_rs.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@ impl Mode {
3939
match self {
4040
Self::Sync => sink,
4141
Self::Async => {
42-
let thread_pool = Arc::new(ThreadPool::builder().build().unwrap());
43-
44-
Arc::new(
45-
AsyncPoolSink::builder()
46-
.thread_pool(thread_pool)
47-
.overflow_policy(OverflowPolicy::DropIncoming)
48-
.sink(sink)
49-
.build()
50-
.unwrap(),
51-
)
42+
let thread_pool = ThreadPool::builder().build_arc().unwrap();
43+
44+
AsyncPoolSink::builder()
45+
.thread_pool(thread_pool)
46+
.overflow_policy(OverflowPolicy::DropIncoming)
47+
.sink(sink)
48+
.build_arc()
49+
.unwrap()
5250
}
5351
}
5452
}
@@ -82,31 +80,27 @@ fn bench_any(bencher: &mut Bencher, mode: Mode, sink: Arc<dyn Sink>) {
8280
}
8381

8482
fn bench_file_inner(bencher: &mut Bencher, mode: Mode) {
85-
let sink: Arc<dyn Sink> = Arc::new(
86-
FileSink::builder()
87-
.path(mode.path("file"))
88-
.truncate(true)
89-
.build()
90-
.unwrap(),
91-
);
83+
let sink = FileSink::builder()
84+
.path(mode.path("file"))
85+
.truncate(true)
86+
.build_arc()
87+
.unwrap();
9288
bench_any(bencher, mode, sink);
9389
}
9490

9591
fn bench_rotating_inner(bencher: &mut Bencher, rotation_policy: RotationPolicy) {
96-
let sink = Arc::new(
97-
RotatingFileSink::builder()
98-
.base_path(Mode::Sync.path(match rotation_policy {
99-
RotationPolicy::FileSize(_) => "rotating_file_size",
100-
RotationPolicy::Daily { .. } => "rotating_daily",
101-
RotationPolicy::Hourly => "rotating_hourly",
102-
RotationPolicy::Period { .. } => "rotating_period",
103-
}))
104-
.rotation_policy(rotation_policy)
105-
.max_files(common::ROTATING_FILES)
106-
.rotate_on_open(true)
107-
.build()
108-
.unwrap(),
109-
);
92+
let sink = RotatingFileSink::builder()
93+
.base_path(Mode::Sync.path(match rotation_policy {
94+
RotationPolicy::FileSize(_) => "rotating_file_size",
95+
RotationPolicy::Daily { .. } => "rotating_daily",
96+
RotationPolicy::Hourly => "rotating_hourly",
97+
RotationPolicy::Period { .. } => "rotating_period",
98+
}))
99+
.rotation_policy(rotation_policy)
100+
.max_files(common::ROTATING_FILES)
101+
.rotate_on_open(true)
102+
.build_arc()
103+
.unwrap();
110104
bench_any(bencher, Mode::Sync, sink);
111105
}
112106

spdlog/examples/02_file.rs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, sync::Arc, time::Duration};
1+
use std::{env, time::Duration};
22

33
use spdlog::{
44
prelude::*,
@@ -18,8 +18,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1818
fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
1919
let path = env::current_exe()?.with_file_name("file.log");
2020

21-
let file_sink = Arc::new(FileSink::builder().path(path).build()?);
22-
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
21+
let file_sink = FileSink::builder().path(path).build_arc()?;
22+
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
2323
spdlog::set_default_logger(new_logger);
2424

2525
info!("this log will be written to the file `all.log`");
@@ -30,13 +30,11 @@ fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
3030
fn configure_rotating_daily_file_logger() -> Result<(), Box<dyn std::error::Error>> {
3131
let path = env::current_exe()?.with_file_name("rotating_daily.log");
3232

33-
let file_sink = Arc::new(
34-
RotatingFileSink::builder()
35-
.base_path(path)
36-
.rotation_policy(RotationPolicy::Daily { hour: 0, minute: 0 })
37-
.build()?,
38-
);
39-
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
33+
let file_sink = RotatingFileSink::builder()
34+
.base_path(path)
35+
.rotation_policy(RotationPolicy::Daily { hour: 0, minute: 0 })
36+
.build_arc()?;
37+
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
4038
spdlog::set_default_logger(new_logger);
4139

4240
info!("this log will be written to the file `rotating_daily.log`, and the file will be rotated daily at 00:00");
@@ -47,13 +45,11 @@ fn configure_rotating_daily_file_logger() -> Result<(), Box<dyn std::error::Erro
4745
fn configure_rotating_size_file_logger() -> Result<(), Box<dyn std::error::Error>> {
4846
let path = env::current_exe()?.with_file_name("rotating_size.log");
4947

50-
let file_sink = Arc::new(
51-
RotatingFileSink::builder()
52-
.base_path(path)
53-
.rotation_policy(RotationPolicy::FileSize(1024))
54-
.build()?,
55-
);
56-
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
48+
let file_sink = RotatingFileSink::builder()
49+
.base_path(path)
50+
.rotation_policy(RotationPolicy::FileSize(1024))
51+
.build_arc()?;
52+
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
5753
spdlog::set_default_logger(new_logger);
5854

5955
info!("this log will be written to the file `rotating_size.log`, and the file will be rotated when its size reaches 1024 bytes");
@@ -64,13 +60,11 @@ fn configure_rotating_size_file_logger() -> Result<(), Box<dyn std::error::Error
6460
fn configure_rotating_hourly_file_logger() -> Result<(), Box<dyn std::error::Error>> {
6561
let path = env::current_exe()?.with_file_name("rotating_hourly.log");
6662

67-
let file_sink = Arc::new(
68-
RotatingFileSink::builder()
69-
.base_path(path)
70-
.rotation_policy(RotationPolicy::Hourly)
71-
.build()?,
72-
);
73-
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
63+
let file_sink = RotatingFileSink::builder()
64+
.base_path(path)
65+
.rotation_policy(RotationPolicy::Hourly)
66+
.build_arc()?;
67+
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
7468
spdlog::set_default_logger(new_logger);
7569

7670
info!("this log will be written to the file `rotating_hourly.log`, and the file will be rotated every hour");
@@ -81,15 +75,13 @@ fn configure_rotating_hourly_file_logger() -> Result<(), Box<dyn std::error::Err
8175
fn configure_rotating_period_file_logger() -> Result<(), Box<dyn std::error::Error>> {
8276
let path = env::current_exe()?.with_file_name("rotating_period.log");
8377

84-
let file_sink = Arc::new(
85-
RotatingFileSink::builder()
86-
.base_path(path)
87-
.rotation_policy(RotationPolicy::Period(Duration::from_secs(
88-
60 * 90, // 90 minutes
89-
)))
90-
.build()?,
91-
);
92-
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
78+
let file_sink = RotatingFileSink::builder()
79+
.base_path(path)
80+
.rotation_policy(RotationPolicy::Period(Duration::from_secs(
81+
60 * 90, // 90 minutes
82+
)))
83+
.build_arc()?;
84+
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
9385
spdlog::set_default_logger(new_logger);
9486

9587
info!("this log will be written to the file `rotating_period.log`, and the file will be rotated every 1.5 hours");

spdlog/examples/03_logger.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1313

1414
// Or completely replace it with a new one.
1515
let path = env::current_exe()?.with_file_name("all.log");
16-
let file_sink = Arc::new(FileSink::builder().path(path).build()?);
16+
let file_sink = FileSink::builder().path(path).build_arc()?;
1717

18-
let new_logger = Arc::new(
19-
Logger::builder()
20-
.level_filter(LevelFilter::All)
21-
.flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn))
22-
.sink(file_sink.clone())
23-
.build()?,
24-
);
18+
let new_logger = Logger::builder()
19+
.level_filter(LevelFilter::All)
20+
.flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn))
21+
.sink(file_sink.clone())
22+
.build_arc()?;
2523
new_logger.set_flush_period(Some(Duration::from_secs(3)));
2624
spdlog::set_default_logger(new_logger);
2725

@@ -42,7 +40,7 @@ struct AppDatabase {
4240
impl AppDatabase {
4341
fn new(all_log_sink: Arc<dyn Sink>) -> Result<Self, Box<dyn std::error::Error>> {
4442
let path = env::current_exe()?.with_file_name("db.log");
45-
let db_file_sink = Arc::new(FileSink::builder().path(path).build()?);
43+
let db_file_sink = FileSink::builder().path(path).build_arc()?;
4644

4745
let logger = Logger::builder()
4846
.name("database")

spdlog/examples/07_async.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, sync::Arc};
1+
use std::env;
22

33
use spdlog::{
44
prelude::*,
@@ -7,17 +7,15 @@ use spdlog::{
77

88
fn main() -> Result<(), Box<dyn std::error::Error>> {
99
let path = env::current_exe()?.with_file_name("async.log");
10-
let file_sink = Arc::new(FileSink::builder().path(path).build()?);
10+
let file_sink = FileSink::builder().path(path).build_arc()?;
1111

1212
// AsyncPoolSink is a combined sink which wraps other sinks
13-
let async_pool_sink = Arc::new(AsyncPoolSink::builder().sink(file_sink).build()?);
13+
let async_pool_sink = AsyncPoolSink::builder().sink(file_sink).build_arc()?;
1414

15-
let async_logger = Arc::new(
16-
Logger::builder()
17-
.sink(async_pool_sink)
18-
.flush_level_filter(LevelFilter::All)
19-
.build()?,
20-
);
15+
let async_logger = Logger::builder()
16+
.sink(async_pool_sink)
17+
.flush_level_filter(LevelFilter::All)
18+
.build_arc()?;
2119

2220
info!(logger: async_logger, "Hello, async!");
2321

0 commit comments

Comments
 (0)