Skip to content

Commit 0470069

Browse files
committed
Reuse more code between gcloud and json formatting
1 parent 9508578 commit 0470069

File tree

2 files changed

+20
-97
lines changed

2 files changed

+20
-97
lines changed

src/format/gcloud.rs

Lines changed: 8 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ impl Format for Gcloud {
2828
// Write all parts of the buffer that need formatting.
2929
buf.buf[0] = b'{';
3030
#[cfg(feature = "timestamp")]
31-
write_timestamp(buf);
32-
write_msg(buf, record.args());
33-
write_key_values(buf, record.key_values(), kvs);
31+
json::write_timestamp(buf);
32+
json::write_msg(buf, record.args());
33+
json::write_key_values(buf, record.key_values(), kvs);
3434
if add_loc {
35-
write_line(buf, record.line().unwrap_or(0));
35+
json::write_line(buf, record.line().unwrap_or(0));
3636
}
3737

3838
// Now that we've written the message to our buffer we have to construct it.
3939
// The first part of the message is the timestamp and log level (severity),
4040
// e.g. `{"timestamp":"2020-12-31T12:32:23.906132Z","severity":"INFO`.
4141
// Or without a timestamp, i.e. `{"severity":"INFO`.
42-
bufs[0] = IoSlice::new(timestamp(buf));
42+
bufs[0] = IoSlice::new(json::timestamp(buf));
4343
bufs[1] = IoSlice::new(b"\"severity\":\"");
4444
if record.level() == log::Level::Error && record.target() == PANIC_TARGET {
4545
// If we're panicking we increase the severity to critical.
@@ -49,23 +49,23 @@ impl Format for Gcloud {
4949
}
5050
// The message (and the end of the log level), e.g. `","message":"some message`.
5151
bufs[3] = IoSlice::new(b"\",\"message\":\"");
52-
bufs[4] = IoSlice::new(msg(buf));
52+
bufs[4] = IoSlice::new(json::msg(buf));
5353
// The target, e.g. `","target":"request`.
5454
bufs[5] = IoSlice::new(b"\",\"target\":\"");
5555
bufs[6] = IoSlice::new(record.target().as_bytes());
5656
// The module, e.g. `","module":"stored::http`.
5757
bufs[7] = IoSlice::new(b"\",\"module\":\"");
5858
bufs[8] = IoSlice::new(record.module_path().unwrap_or("").as_bytes());
5959
// Any key value pairs supplied by the user.
60-
bufs[9] = IoSlice::new(key_values(buf));
60+
bufs[9] = IoSlice::new(json::key_values(buf));
6161
// Optional file, e.g.
6262
// `","sourceLocation":{"file":"some_file.rs","line":"123"}}`, and a line
6363
// end.
6464
let n = if add_loc {
6565
bufs[10] = IoSlice::new(b",\"sourceLocation\":{\"file\":\"");
6666
bufs[11] = IoSlice::new(record.file().unwrap_or("??").as_bytes());
6767
bufs[12] = IoSlice::new(b"\",\"line\":\"");
68-
bufs[13] = IoSlice::new(line(buf));
68+
bufs[13] = IoSlice::new(json::line(buf));
6969
bufs[14] = IoSlice::new(b"\"}}\n");
7070
15
7171
} else {
@@ -76,90 +76,9 @@ impl Format for Gcloud {
7676
}
7777
}
7878

79-
/// Index of the end of `{"timestamp":"0000-00-00T00:00:00.000000Z",`.
80-
#[cfg(feature = "timestamp")]
81-
const TS_END_INDEX: usize = 43;
82-
#[cfg(not(feature = "timestamp"))]
83-
const TS_END_INDEX: usize = 1;
84-
85-
#[inline]
86-
#[cfg(feature = "timestamp")]
87-
fn write_timestamp(buf: &mut Buffer) {
88-
let _ = buf.buf[TS_END_INDEX];
89-
buf.buf[1] = b'"';
90-
buf.buf[2] = b't';
91-
buf.buf[3] = b'i';
92-
buf.buf[4] = b'm';
93-
buf.buf[5] = b'e';
94-
buf.buf[6] = b's';
95-
buf.buf[7] = b't';
96-
buf.buf[8] = b'a';
97-
buf.buf[9] = b'm';
98-
buf.buf[10] = b'p';
99-
buf.buf[11] = b'"';
100-
buf.buf[12] = b':';
101-
buf.buf[13] = b'"';
102-
format_timestamp(&mut buf.buf[14..]);
103-
buf.buf[TS_END_INDEX - 2] = b'"';
104-
buf.buf[TS_END_INDEX - 1] = b',';
105-
}
106-
107-
#[inline]
108-
fn timestamp(buf: &Buffer) -> &[u8] {
109-
&buf.buf[..TS_END_INDEX]
110-
}
111-
11279
#[inline]
11380
const fn severity(level: log::Level) -> &'static [u8] {
11481
// NOTE: gcloud doesn't have trace messages so we use debug twice.
11582
const SEVERITIES: [&[u8]; 6] = [b"OFF", b"ERROR", b"WARNING", b"INFO", b"DEBUG", b"DEBUG"];
11683
SEVERITIES[level as usize]
11784
}
118-
119-
#[inline]
120-
fn write_msg(buf: &mut Buffer, args: &fmt::Arguments) {
121-
buf.buf.truncate(TS_END_INDEX);
122-
if let Some(msg) = args.as_str() {
123-
json::Buf(&mut buf.buf)
124-
.write_str(msg)
125-
.unwrap_or_else(|_| unreachable!());
126-
} else {
127-
json::Buf(&mut buf.buf)
128-
.write_fmt(*args)
129-
.unwrap_or_else(|_| unreachable!());
130-
}
131-
buf.indices[0] = buf.buf.len();
132-
}
133-
134-
#[inline]
135-
fn msg(buf: &Buffer) -> &[u8] {
136-
&buf.buf[TS_END_INDEX..buf.indices[0]]
137-
}
138-
139-
#[inline]
140-
fn write_key_values<Kvs: kv::Source>(buf: &mut Buffer, kvs1: &dyn kv::Source, kvs2: Kvs) {
141-
buf.buf.extend_from_slice(b"\"");
142-
// TODO: see if we can add to the slice of `IoSlice` using the keys
143-
// and string values.
144-
let mut visitor = json::KeyValueVisitor(&mut buf.buf);
145-
kvs1.visit(&mut visitor).unwrap_or_else(|_| unreachable!());
146-
kvs2.visit(&mut visitor).unwrap_or_else(|_| unreachable!());
147-
buf.indices[1] = buf.buf.len();
148-
}
149-
150-
#[inline]
151-
fn key_values(buf: &Buffer) -> &[u8] {
152-
&buf.buf[buf.indices[0]..buf.indices[1]]
153-
}
154-
155-
#[inline]
156-
fn write_line(buf: &mut Buffer, line: u32) {
157-
let mut itoa = itoa::Buffer::new();
158-
buf.buf.extend_from_slice(itoa.format(line).as_bytes());
159-
buf.indices[2] = buf.buf.len();
160-
}
161-
162-
#[inline]
163-
fn line(buf: &Buffer) -> &[u8] {
164-
&buf.buf[buf.indices[1]..buf.indices[2]]
165-
}

src/format/json.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const TS_END_INDEX: usize = 1;
7575

7676
#[inline]
7777
#[cfg(feature = "timestamp")]
78-
fn write_timestamp(buf: &mut Buffer) {
78+
pub(crate) fn write_timestamp(buf: &mut Buffer) {
7979
let _ = buf.buf[TS_END_INDEX];
8080
buf.buf[1] = b'"';
8181
buf.buf[2] = b't';
@@ -96,12 +96,12 @@ fn write_timestamp(buf: &mut Buffer) {
9696
}
9797

9898
#[inline]
99-
fn timestamp(buf: &Buffer) -> &[u8] {
99+
pub(crate) fn timestamp(buf: &Buffer) -> &[u8] {
100100
&buf.buf[..TS_END_INDEX]
101101
}
102102

103103
#[inline]
104-
fn write_msg(buf: &mut Buffer, args: &fmt::Arguments) {
104+
pub(crate) fn write_msg(buf: &mut Buffer, args: &fmt::Arguments) {
105105
buf.buf.truncate(TS_END_INDEX);
106106
if let Some(msg) = args.as_str() {
107107
Buf(&mut buf.buf)
@@ -116,12 +116,16 @@ fn write_msg(buf: &mut Buffer, args: &fmt::Arguments) {
116116
}
117117

118118
#[inline]
119-
fn msg(buf: &Buffer) -> &[u8] {
119+
pub(crate) fn msg(buf: &Buffer) -> &[u8] {
120120
&buf.buf[TS_END_INDEX..buf.indices[0]]
121121
}
122122

123123
#[inline]
124-
fn write_key_values<Kvs: kv::Source>(buf: &mut Buffer, kvs1: &dyn kv::Source, kvs2: Kvs) {
124+
pub(crate) fn write_key_values<Kvs: kv::Source>(
125+
buf: &mut Buffer,
126+
kvs1: &dyn kv::Source,
127+
kvs2: Kvs,
128+
) {
125129
buf.buf.extend_from_slice(b"\"");
126130
// TODO: see if we can add to the slice of `IoSlice` using the keys
127131
// and string values.
@@ -132,19 +136,19 @@ fn write_key_values<Kvs: kv::Source>(buf: &mut Buffer, kvs1: &dyn kv::Source, kv
132136
}
133137

134138
#[inline]
135-
fn key_values(buf: &Buffer) -> &[u8] {
139+
pub(crate) fn key_values(buf: &Buffer) -> &[u8] {
136140
&buf.buf[buf.indices[0]..buf.indices[1]]
137141
}
138142

139143
#[inline]
140-
fn write_line(buf: &mut Buffer, line: u32) {
144+
pub(crate) fn write_line(buf: &mut Buffer, line: u32) {
141145
let mut itoa = itoa::Buffer::new();
142146
buf.buf.extend_from_slice(itoa.format(line).as_bytes());
143147
buf.indices[2] = buf.buf.len();
144148
}
145149

146150
#[inline]
147-
fn line(buf: &Buffer) -> &[u8] {
151+
pub(crate) fn line(buf: &Buffer) -> &[u8] {
148152
&buf.buf[buf.indices[1]..buf.indices[2]]
149153
}
150154

0 commit comments

Comments
 (0)