Skip to content

Commit 0bc1030

Browse files
committed
Implement private AndroidFormatter for AndroidSink
1 parent 50def56 commit 0bc1030

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// TODO: Remove this file, use `PatternFormatter` instead
2+
//
3+
// Need to keep waiting for conditional space and brackets to be supported in
4+
// pattern template strings (optional fields require these, e.g. `logger_name`)
5+
6+
use std::fmt::{self, Write};
7+
8+
use cfg_if::cfg_if;
9+
10+
use crate::{
11+
formatter::{Formatter, FormatterContext},
12+
Error, Record, StringBuf,
13+
};
14+
15+
#[derive(Clone)]
16+
pub(crate) struct AndroidFormatter {}
17+
18+
impl AndroidFormatter {
19+
#[must_use]
20+
pub(crate) fn new() -> Self {
21+
Self {}
22+
}
23+
24+
fn format_impl(
25+
&self,
26+
record: &Record,
27+
dest: &mut StringBuf,
28+
_ctx: &mut FormatterContext,
29+
) -> Result<(), fmt::Error> {
30+
cfg_if! {
31+
if #[cfg(not(feature = "flexible-string"))] {
32+
dest.reserve(crate::string_buf::RESERVE_SIZE);
33+
}
34+
}
35+
36+
if let Some(logger_name) = record.logger_name() {
37+
dest.write_str("[")?;
38+
dest.write_str(logger_name)?;
39+
dest.write_str("] ")?;
40+
}
41+
42+
if let Some(srcloc) = record.source_location() {
43+
dest.write_str("[")?;
44+
dest.write_str(srcloc.module_path())?;
45+
dest.write_str(", ")?;
46+
dest.write_str(srcloc.file())?;
47+
dest.write_str(":")?;
48+
write!(dest, "{}", srcloc.line())?;
49+
dest.write_str("] ")?;
50+
}
51+
52+
dest.write_str(record.payload())?;
53+
54+
record.key_values().write_to(dest, true)?;
55+
Ok(())
56+
}
57+
}
58+
59+
impl Formatter for AndroidFormatter {
60+
fn format(
61+
&self,
62+
record: &Record,
63+
dest: &mut StringBuf,
64+
ctx: &mut FormatterContext,
65+
) -> crate::Result<()> {
66+
self.format_impl(record, dest, ctx)
67+
.map_err(Error::FormatRecord)
68+
}
69+
}
70+
71+
impl Default for AndroidFormatter {
72+
fn default() -> Self {
73+
Self::new()
74+
}
75+
}

spdlog/src/formatter/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
//! [`SinkPropAccess::set_formatter`]: crate::sink::SinkPropAccess::set_formatter
5252
//! [./examples]: https://github.com/SpriteOvO/spdlog-rs/tree/main/spdlog/examples
5353
54+
#[cfg(any(
55+
all(target_os = "android", feature = "native", feature = "android_log"),
56+
all(doc, not(doctest))
57+
))]
58+
mod android_formatter;
5459
mod full_formatter;
5560
#[cfg(any(
5661
all(target_os = "linux", feature = "native", feature = "libsystemd"),
@@ -65,6 +70,11 @@ mod unreachable_formatter;
6570

6671
use std::ops::Range;
6772

73+
#[cfg(any(
74+
all(target_os = "android", feature = "native", feature = "android_log"),
75+
all(doc, not(doctest))
76+
))]
77+
pub(crate) use android_formatter::*;
6878
use dyn_clone::*;
6979
pub use full_formatter::*;
7080
#[cfg(any(

spdlog/src/sink/android_sink.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{ffi::CString, io, ptr::null, result::Result as StdResult};
33
use libc::EPERM;
44

55
use crate::{
6-
formatter::{Formatter, FormatterContext},
6+
formatter::{AndroidFormatter, Formatter, FormatterContext},
77
prelude::*,
88
sink::{GetSinkProp, Sink, SinkProp},
99
Error, ErrorHandler, Record, Result, StringBuf,
@@ -184,7 +184,7 @@ impl AndroidSink {
184184
/// | Parameter | Default Value |
185185
/// |-----------------|-----------------------------|
186186
/// | [level_filter] | `All` |
187-
/// | [formatter] | `FullFormatter` |
187+
/// | [formatter] | `AndroidFormatter` |
188188
/// | [error_handler] | [`ErrorHandler::default()`] |
189189
/// | | |
190190
/// | [tag] | `AndroidSink::Default` |
@@ -196,8 +196,11 @@ impl AndroidSink {
196196
/// [tag]: AndroidSinkBuilder::tag
197197
#[must_use]
198198
pub fn builder() -> AndroidSinkBuilder {
199+
let prop = SinkProp::default();
200+
prop.set_formatter(AndroidFormatter::new());
201+
199202
AndroidSinkBuilder {
200-
prop: SinkProp::default(),
203+
prop,
201204
tag: AndroidLogTag::Default,
202205
}
203206
}

0 commit comments

Comments
 (0)