-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlog_config.rs
92 lines (78 loc) · 2.91 KB
/
log_config.rs
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Types and helpers specifying how logs should be formatted and where they
//! should be directed.
use std::{path::Path, process::Stdio, str::FromStr};
use tracing::info;
/// Specifies how a test's logging should be managed.
#[derive(Debug, Clone, Copy)]
pub struct LogConfig {
pub output_mode: OutputMode,
pub log_format: LogFormat,
}
/// Specifies where a output for a test's processes should be written.
#[derive(Debug, Clone, Copy)]
pub enum OutputMode {
/// Write to files in the server's factory's temporary directory.
TmpFile,
/// Write stdout/stderr to the console.
Stdio,
/// Redirect stdout/stderr to /dev/null.
Null,
}
impl FromStr for OutputMode {
type Err = std::io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"file" | "tmpfile" => Ok(OutputMode::TmpFile),
"stdio" => Ok(OutputMode::Stdio),
"null" => Ok(OutputMode::Null),
_ => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
s.to_string(),
)),
}
}
}
impl OutputMode {
/// Returns the stdout/stderr handles to pass to processes using the
/// specified logging mode.
///
/// # Parameters
///
/// - directory: The directory in which to store any files written under
/// the selected discipline.
/// - file_prefix: The prefix to add to the names of any files written
/// under the selected discipline.
pub(crate) fn get_handles(
&self,
directory: &impl AsRef<Path>,
file_prefix: &str,
) -> anyhow::Result<(Stdio, Stdio)> {
match self {
OutputMode::TmpFile => {
let mut stdout_path = directory.as_ref().to_path_buf();
stdout_path.push(format!("{}.stdout.log", file_prefix));
let mut stderr_path = directory.as_ref().to_path_buf();
stderr_path.push(format!("{}.stderr.log", file_prefix));
info!(?stdout_path, ?stderr_path, "Opening server log files");
Ok((
std::fs::File::create(stdout_path)?.into(),
std::fs::File::create(stderr_path)?.into(),
))
}
OutputMode::Stdio => Ok((Stdio::inherit(), Stdio::inherit())),
OutputMode::Null => Ok((Stdio::null(), Stdio::null())),
}
}
}
/// Specifies how output for a test's processes should be structured.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogFormat {
/// Format logs as plain hopefully human-readable output.
Plain,
/// Format logs as Bunyan output, more suitable for machine processing (such
/// as in CI).
Bunyan,
}