1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use opentelemetry_otlp::WithExportConfig;
use serde::{Deserialize, Serialize};
use std::{
    fmt::{self, Display},
    fs, io,
    path::PathBuf,
    str::FromStr,
};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use url::Url;

#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
/// Format of the logger.
pub enum LogFormat {
    Plain,
    Json,
}

impl Default for LogFormat {
    fn default() -> Self {
        Self::Plain
    }
}

impl Display for LogFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            LogFormat::Plain => "plain",
            LogFormat::Json => "json",
        };
        f.write_str(s)
    }
}

impl FromStr for LogFormat {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_lowercase().as_str() {
            "plain" => Ok(LogFormat::Plain),
            "json" => Ok(LogFormat::Json),
            other => Err(format!("unknown log format '{}'", other)),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
/// Output of the logger.
pub enum LogOutput {
    Stdout,
    Stderr,
    File(PathBuf),
}

impl FromStr for LogOutput {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some(stripped) = s.strip_prefix('@') {
            return Ok(Self::File(PathBuf::from(stripped)));
        }

        match s.trim().to_lowercase().as_str() {
            "stdout" => Ok(LogOutput::Stdout),
            "stderr" => Ok(LogOutput::Stderr),
            other => Err(format!("unknown log output '{}'", other)),
        }
    }
}

impl Default for LogOutput {
    fn default() -> Self {
        Self::Stderr
    }
}

pub struct LogGuard {
    _nonblocking_worker_guard: tracing_appender::non_blocking::WorkerGuard,
}

impl Drop for LogGuard {
    fn drop(&mut self) {
        tracing::trace!("shutting down opentelemetry trace provider");
        opentelemetry::global::shutdown_tracer_provider();
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LogSettings {
    pub level: LevelFilter,
    pub format: LogFormat,
    pub output: LogOutput,
    pub trace_collector_endpoint: Option<Url>,
}

impl Default for LogSettings {
    fn default() -> Self {
        Self {
            level: LevelFilter::TRACE,
            format: Default::default(),
            output: Default::default(),
            trace_collector_endpoint: None,
        }
    }
}

impl LogSettings {
    pub fn init_log(self) -> Result<LogGuard, Error> {
        let nonblocking_worker_guard = match self.output {
            LogOutput::Stdout => {
                let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout());
                self.init_subscriber(non_blocking)?;

                guard
            }
            LogOutput::Stderr => {
                let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stderr());
                self.init_subscriber(non_blocking)?;

                guard
            }
            LogOutput::File(ref path) => {
                let file = fs::OpenOptions::new()
                    .create(true)
                    .write(true)
                    .append(true)
                    .open(path)
                    .map_err(|cause| Error::File {
                        path: path.clone(),
                        cause,
                    })?;
                let (non_blocking, guard) = tracing_appender::non_blocking(file);

                self.init_subscriber(non_blocking)?;

                guard
            }
        };

        let default_settings = Self::default();

        if self.output != default_settings.output {
            tracing::info!(
                "log output overriden from command line: {:?} replaced with {:?}",
                default_settings.output,
                self.output
            );
        }
        if self.level != default_settings.level {
            tracing::info!(
                "log level overriden from command line: {:?} replaced with {:?}",
                default_settings.level,
                self.level
            );
        }
        if self.format != default_settings.format {
            tracing::info!(
                "log format overriden from command line: {:?} replaced with {:?}",
                default_settings.format,
                self.format
            );
        }

        Ok(LogGuard {
            _nonblocking_worker_guard: nonblocking_worker_guard,
        })
    }

    fn init_subscriber(
        &self,
        non_blocking: tracing_appender::non_blocking::NonBlocking,
    ) -> Result<(), Error> {
        let otel_layer = if let Some(endpoint) = self.trace_collector_endpoint.as_ref() {
            let otel_tracer = opentelemetry_otlp::new_pipeline()
                .tracing()
                .with_exporter(
                    opentelemetry_otlp::new_exporter()
                        .tonic()
                        .with_endpoint(endpoint.as_str()),
                )
                .with_trace_config(opentelemetry::sdk::trace::config().with_resource(
                    opentelemetry::sdk::Resource::new(vec![opentelemetry::KeyValue::new(
                        opentelemetry_semantic_conventions::resource::SERVICE_NAME,
                        "explorer",
                    )]),
                ))
                .install_batch(opentelemetry::runtime::Tokio)
                .map_err(Error::InstallOpenTelemetryPipeLine)?;

            Some(tracing_opentelemetry::layer().with_tracer(otel_tracer))
        } else {
            None
        };

        let subscriber = tracing_subscriber::registry()
            .with(self.level)
            .with(otel_layer);

        match self.format {
            LogFormat::Plain => {
                let layer = tracing_subscriber::fmt::Layer::new()
                    .with_level(true)
                    .with_writer(non_blocking);

                subscriber
                    .with(layer)
                    .try_init()
                    .map_err(Error::InitSubscriber)
            }
            LogFormat::Json => {
                let layer = tracing_subscriber::fmt::Layer::new()
                    .json()
                    .with_level(true)
                    .with_writer(non_blocking);

                subscriber
                    .with(layer)
                    .try_init()
                    .map_err(Error::InitSubscriber)
            }
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("log format `{specified}` is not supported for this output")]
    FormatNotSupported { specified: LogFormat },
    #[error("failed to open the log file `{}`", .path.to_string_lossy())]
    File {
        path: PathBuf,
        #[source]
        cause: io::Error,
    },
    #[error("failed to install opentelemetry pipeline")]
    InstallOpenTelemetryPipeLine(#[source] opentelemetry::trace::TraceError),
    #[error("failed to init subscriber")]
    InitSubscriber(#[source] tracing_subscriber::util::TryInitError),
}