use core::fmt::Write;
use super::event_data::PropValue;
use super::row::Row;
use super::timestamp;
use crate::telemetry::level::Level;
const FORMAT_VERSION: &str = "1.0";
const FIXED: [&str; 9] = [
"timestamp",
"Level",
"Logger",
"Message",
"CorrelationId",
"Exception",
"Service",
"EventProperties",
"LogFormatVersion",
];
pub(crate) fn line(row: &Row, service: (&str, &str, &str), out: &mut String) {
let (name, version, hostname) = service;
out.push_str("{\"timestamp\":\"");
timestamp::format_into(out, row.moment);
out.push_str("\",\"Level\":\"");
out.push_str(level_name(row.level));
out.push_str("\",\"Logger\":");
string(out, &row.target);
out.push_str(",\"Message\":");
string(out, &row.message);
if let Some(correlation) = &row.correlation_id {
out.push_str(",\"CorrelationId\":");
string(out, correlation);
}
if let Some(exception) = &row.exception {
out.push_str(",\"Exception\":{\"Type\":");
string(out, &exception.kind);
out.push_str(",\"Message\":");
string(out, &exception.message);
if let Some(stack) = &exception.stack {
out.push_str(",\"StackTrace\":");
string(out, stack);
}
if let Some(causes) = &exception.causes {
out.push_str(",\"Inner\":");
string(out, causes);
}
out.push('}');
}
out.push_str(",\"Service\":{\"Name\":");
string(out, name);
out.push_str(",\"Version\":");
string(out, version);
out.push_str(",\"Hostname\":");
string(out, hostname);
out.push_str("},\"EventProperties\":{");
let mut written = false;
if let Some(trace) = &row.trace_id {
out.push_str("\"traceId\":");
string(out, trace);
written = true;
}
if let Some(span) = &row.span_id {
if written {
out.push(',');
}
out.push_str("\"spanId\":");
string(out, span);
written = true;
}
for (key, value) in &row.properties {
if written {
out.push(',');
}
entry(out, key, value);
written = true;
}
out.push_str("},\"LogFormatVersion\":");
string(out, FORMAT_VERSION);
if let Some(trace) = &row.trace_id {
out.push_str(",\"traceId\":");
string(out, trace);
}
if let Some(span) = &row.span_id {
out.push_str(",\"spanId\":");
string(out, span);
}
for (key, value) in &row.properties {
if FIXED.contains(&key.as_str()) {
continue;
}
out.push(',');
entry(out, key, value);
}
out.push('}');
}
fn level_name(level: Level) -> &'static str {
match level {
Level::Trace => "Trace",
Level::Debug => "Debug",
Level::Info => "Info",
Level::Warn => "Warn",
Level::Error => "Error",
Level::Fatal => "Fatal",
Level::Off => "Off",
}
}
fn entry(out: &mut String, key: &str, value: &PropValue) {
string(out, key);
out.push(':');
write_value(out, value);
}
fn write_value(out: &mut String, value: &PropValue) {
match value {
PropValue::Text(text) => string(out, text),
PropValue::Integer(number) => {
let _ = write!(out, "{number}");
}
PropValue::Float(number) => float(out, *number),
PropValue::Flag(flag) => out.push_str(if *flag { "true" } else { "false" }),
}
}
fn float(out: &mut String, number: f64) {
if !number.is_finite() {
out.push_str("null");
return;
}
let start = out.len();
let _ = write!(out, "{number}");
if !out[start..].contains('.') {
out.push_str(".0");
}
}
fn string(out: &mut String, value: &str) {
out.push('"');
for character in value.chars() {
match character {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
'\u{8}' => out.push_str("\\b"),
'\u{c}' => out.push_str("\\f"),
other if (other as u32) < 0x20 => {
let _ = write!(out, "\\u{:04x}", other as u32);
}
other => out.push(other),
}
}
out.push('"');
}