veilid_core/logging/veilid_tracing/
log_output.rs1use tracing_subscriber::Registry;
2
3use super::*;
4
5use std::path::{Path, PathBuf};
6
7#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
8pub enum LogOutputKind {
9 StdOut,
10 StdErr,
11 File,
12 Api,
13 Layer(String),
14}
15
16#[must_use]
36pub struct LogOutput {
37 pub(super) kind: LogOutputKind,
38 pub(super) color: bool,
39 pub(super) path: PathBuf,
40 pub(super) append: bool,
41 pub(super) directives: Vec<VeilidLogDirective>,
42 pub(super) layer: Option<LogOutputLayer>,
43}
44
45pub type LogOutputLayer =
46 Box<dyn tracing_subscriber::layer::Layer<Registry> + Send + Sync + 'static>;
47
48impl LogOutput {
49 pub fn stdout(color: bool) -> Self {
51 Self {
52 kind: LogOutputKind::StdOut,
53 color,
54 path: PathBuf::new(),
55 append: false,
56 directives: vec![],
57 layer: None,
58 }
59 }
60
61 pub fn stderr(color: bool) -> Self {
63 Self {
64 kind: LogOutputKind::StdErr,
65 color,
66 path: PathBuf::new(),
67 append: false,
68 directives: vec![],
69 layer: None,
70 }
71 }
72
73 pub fn file<P: AsRef<Path>>(path: P, append: bool) -> Self {
75 Self {
76 kind: LogOutputKind::File,
77 color: false,
78 path: path.as_ref().to_owned(),
79 append,
80 directives: vec![],
81 layer: None,
82 }
83 }
84
85 pub fn api() -> Self {
87 Self {
88 kind: LogOutputKind::Api,
89 color: false,
90 path: PathBuf::new(),
91 append: false,
92 directives: vec![],
93 layer: None,
94 }
95 }
96
97 pub fn layer<L>(name: String, layer: LogOutputLayer) -> Self {
99 Self {
100 kind: LogOutputKind::Layer(name),
101 color: false,
102 path: PathBuf::new(),
103 append: false,
104 directives: vec![],
105 layer: Some(layer),
106 }
107 }
108
109 pub fn with_common_log_level(mut self, level: VeilidConfigLogLevel) -> Self {
111 self.directives
112 .push(VeilidLogDirective::try_facility_level("veilid::common", Some(level)).unwrap());
113 self
114 }
115
116 pub fn try_with_directives<C: TryIntoIterVeilidLogDirective>(
119 mut self,
120 directives: C,
121 ) -> VeilidAPIResult<Self> {
122 let mut directives = directives.try_into_iter()?.collect();
123 self.directives.append(&mut directives);
124 Ok(self)
125 }
126}