Skip to content

Commit 5e20c0e

Browse files
committed
Remove RecordBuilder
1 parent 8030472 commit 5e20c0e

File tree

6 files changed

+35
-88
lines changed

6 files changed

+35
-88
lines changed

spdlog/src/formatter/full_formatter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ mod tests {
130130

131131
#[test]
132132
fn format() {
133-
let record = Record::new(Level::Warn, "test log content");
133+
let record = Record::new(Level::Warn, "test log content", None, None);
134134
let mut buf = StringBuf::new();
135135
let mut ctx = FormatterContext::new();
136136
FullFormatter::new()

spdlog/src/formatter/json_formatter.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ mod tests {
199199
fn should_format_json() {
200200
let mut dest = StringBuf::new();
201201
let formatter = JsonFormatter::new();
202-
let record = Record::builder(Level::Info, "payload").build();
202+
let record = Record::new(Level::Info, "payload", None, None);
203203
let mut ctx = FormatterContext::new();
204204
formatter.format(&record, &mut dest, &mut ctx).unwrap();
205205

@@ -222,9 +222,7 @@ mod tests {
222222
fn should_format_json_with_logger_name() {
223223
let mut dest = StringBuf::new();
224224
let formatter = JsonFormatter::new();
225-
let record = Record::builder(Level::Info, "payload")
226-
.logger_name("my-component")
227-
.build();
225+
let record = Record::new(Level::Info, "payload", None, Some("my-component"));
228226
let mut ctx = FormatterContext::new();
229227
formatter.format(&record, &mut dest, &mut ctx).unwrap();
230228

@@ -247,9 +245,12 @@ mod tests {
247245
fn should_format_json_with_src_loc() {
248246
let mut dest = StringBuf::new();
249247
let formatter = JsonFormatter::new();
250-
let record = Record::builder(Level::Info, "payload")
251-
.source_location(Some(SourceLocation::__new("module", "file.rs", 1, 2)))
252-
.build();
248+
let record = Record::new(
249+
Level::Info,
250+
"payload",
251+
Some(SourceLocation::__new("module", "file.rs", 1, 2)),
252+
None,
253+
);
253254
let mut ctx = FormatterContext::new();
254255
formatter.format(&record, &mut dest, &mut ctx).unwrap();
255256

spdlog/src/formatter/pattern_formatter/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,10 +1210,12 @@ mod tests {
12101210

12111211
#[must_use]
12121212
fn get_mock_record() -> Record<'static> {
1213-
Record::builder(Level::Info, "record_payload")
1214-
.logger_name("logger_name")
1215-
.source_location(Some(SourceLocation::__new("module", "file", 10, 20)))
1216-
.build()
1213+
Record::new(
1214+
Level::Info,
1215+
"record_payload",
1216+
Some(SourceLocation::__new("module", "file", 10, 20)),
1217+
Some("logger_name"),
1218+
)
12171219
}
12181220

12191221
fn test_pattern<P, T>(pattern: P, formatted: T, style_range: Option<Range<usize>>)

spdlog/src/lib.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,10 @@ pub mod prelude {
320320
}
321321

322322
use std::{
323+
borrow::Cow,
323324
env::{self, VarError},
324325
ffi::OsStr,
325-
panic,
326+
fmt, panic,
326327
result::Result as StdResult,
327328
};
328329

@@ -783,19 +784,15 @@ pub fn __log(
783784
logger: &Logger,
784785
level: Level,
785786
srcloc: Option<SourceLocation>,
786-
fmt_args: std::fmt::Arguments,
787+
fmt_args: fmt::Arguments,
787788
) {
788-
// use `Cow` to avoid allocation as much as we can
789-
let payload: std::borrow::Cow<str> = match fmt_args.as_str() {
790-
Some(literal_str) => literal_str.into(), // no format arguments, so it is a `&'static str`
791-
None => fmt_args.to_string().into(),
792-
};
793-
794-
let mut builder = Record::builder(level, payload).source_location(srcloc);
795-
if let Some(logger_name) = logger.name() {
796-
builder = builder.logger_name(logger_name);
797-
}
798-
logger.log(&builder.build());
789+
// Use `Cow` to avoid allocation as much as we can
790+
let payload: Cow<str> = fmt_args
791+
.as_str()
792+
.map(Cow::Borrowed) // No format arguments, so it is a `&'static str`
793+
.unwrap_or_else(|| Cow::Owned(fmt_args.to_string()));
794+
let record = Record::new(level, payload, srcloc, logger.name());
795+
logger.log(&record);
799796
}
800797

801798
#[cfg(test)]

spdlog/src/record.rs

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,24 @@ struct RecordInner {
3737

3838
impl<'a> Record<'a> {
3939
#[must_use]
40-
pub(crate) fn new<S>(level: Level, payload: S) -> Record<'a>
41-
where
42-
S: Into<Cow<'a, str>>,
43-
{
40+
pub(crate) fn new(
41+
level: Level,
42+
payload: impl Into<Cow<'a, str>>,
43+
srcloc: Option<SourceLocation>,
44+
logger_name: Option<&'a str>,
45+
) -> Record<'a> {
4446
Record {
45-
logger_name: None,
47+
logger_name: logger_name.map(Cow::Borrowed),
4648
payload: payload.into(),
4749
inner: Cow::Owned(RecordInner {
4850
level,
49-
source_location: None,
51+
source_location: srcloc,
5052
time: SystemTime::now(),
5153
tid: get_current_tid(),
5254
}),
5355
}
5456
}
5557

56-
#[must_use]
57-
pub(crate) fn builder<S>(level: Level, payload: S) -> RecordBuilder<'a>
58-
where
59-
S: Into<Cow<'a, str>>,
60-
{
61-
RecordBuilder::new(level, payload)
62-
}
63-
6458
/// Creates a [`RecordOwned`] that doesn't have lifetimes.
6559
#[must_use]
6660
pub fn to_owned(&self) -> RecordOwned {
@@ -219,53 +213,6 @@ impl RecordOwned {
219213
// When adding more getters, also add to `Record`
220214
}
221215

222-
#[allow(missing_docs)]
223-
#[derive(Clone, Debug)]
224-
pub(crate) struct RecordBuilder<'a> {
225-
record: Record<'a>,
226-
}
227-
228-
impl<'a> RecordBuilder<'a> {
229-
/// Constructs a `RecordBuilder`.
230-
///
231-
/// The default value of [`Record`] is the same as [`Record::new()`].
232-
///
233-
/// Typically users should only use it for testing [`Sink`].
234-
///
235-
/// [`Sink`]: crate::sink::Sink
236-
#[must_use]
237-
pub(crate) fn new<S>(level: Level, payload: S) -> Self
238-
where
239-
S: Into<Cow<'a, str>>,
240-
{
241-
Self {
242-
record: Record::new(level, payload),
243-
}
244-
}
245-
246-
/// Sets the logger name.
247-
#[must_use]
248-
pub(crate) fn logger_name(mut self, logger_name: &'a str) -> Self {
249-
self.record.logger_name = Some(Cow::Borrowed(logger_name));
250-
self
251-
}
252-
253-
/// Sets the source location.
254-
// `Option` in the parameter is for the convenience of passing the result of
255-
// the macro `source_location_current` directly.
256-
#[must_use]
257-
pub(crate) fn source_location(mut self, srcloc: Option<SourceLocation>) -> Self {
258-
self.record.inner.to_mut().source_location = srcloc;
259-
self
260-
}
261-
262-
/// Builds a [`Record`].
263-
#[must_use]
264-
pub(crate) fn build(self) -> Record<'a> {
265-
self.record
266-
}
267-
}
268-
269216
fn get_current_tid() -> u64 {
270217
#[cfg(target_os = "linux")]
271218
#[must_use]

spdlog/src/sink/rotating_file_sink.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ mod tests {
12201220

12211221
{
12221222
let logger = build(true);
1223-
let mut record = Record::new(Level::Info, "test log message");
1223+
let mut record = Record::new(Level::Info, "test log message", None, None);
12241224
let initial_time = record.time();
12251225

12261226
assert_files_count("hourly", 1);
@@ -1279,7 +1279,7 @@ mod tests {
12791279
};
12801280

12811281
{
1282-
let mut record = Record::new(Level::Info, "test log message");
1282+
let mut record = Record::new(Level::Info, "test log message", None, None);
12831283

12841284
assert_files_count(prefix, 1);
12851285

0 commit comments

Comments
 (0)