Skip to content

Commit 705a80d

Browse files
authored
Gel captive log file path (#528)
1 parent 2f03ff1 commit 705a80d

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

gel-captive/src/lib.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
mod server;
22
mod utils;
33

4+
use std::path;
5+
46
pub use server::{ServerInfo, ServerProcess};
57

68
pub struct ServerBuilder {
7-
// TODO
9+
log_file_path: Option<path::PathBuf>,
810
}
911

1012
impl ServerBuilder {
1113
pub fn new() -> Self {
12-
Self {}
14+
Self {
15+
log_file_path: None,
16+
}
1317
}
1418

1519
pub fn start(self) -> ServerProcess {
16-
ServerProcess::start()
20+
ServerProcess::start(self)
21+
}
22+
23+
/// Sets filename of the server output log.
24+
/// Defaults to a random temp file.
25+
pub fn log_file_path(mut self, path: Option<path::PathBuf>) -> Self {
26+
self.log_file_path = path;
27+
self
1728
}
1829
}
1930

gel-captive/src/server.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::env;
21
use std::fs::File;
32
use std::io::{BufRead, BufReader};
43
use std::path::Path;
54
use std::process;
65
use std::sync::Mutex;
6+
use std::{env, path};
77

88
use crate::utils::execute_and_print_errors;
99

@@ -24,7 +24,7 @@ pub struct ServerInfo {
2424
}
2525

2626
impl ServerProcess {
27-
pub(crate) fn start() -> ServerProcess {
27+
pub(crate) fn start(config: crate::ServerBuilder) -> ServerProcess {
2828
let bin_name = if let Ok(ver) = env::var("GEL_MAJOR_VERSION") {
2929
format!("gel-server-{ver}")
3030
} else {
@@ -93,7 +93,7 @@ impl ServerProcess {
9393

9494
// write log file
9595
let stdout = process.stderr.take().unwrap();
96-
std::thread::spawn(move || write_log_into_file(stdout));
96+
std::thread::spawn(move || write_log_into_file(stdout, config.log_file_path));
9797

9898
// wait for server to start
9999
let info = wait_for_server_status(get_status_file).unwrap();
@@ -251,19 +251,21 @@ fn wait_for_server_status(get_status_file: impl FnOnce() -> File) -> Result<Serv
251251
}
252252

253253
/// Writes a stream to a log file in a temporary directory.
254-
fn write_log_into_file(stream: impl std::io::Read) {
255-
let log_dir = env::temp_dir();
254+
fn write_log_into_file(stream: impl std::io::Read, log_path: Option<path::PathBuf>) {
255+
let log_path = if let Some(f) = log_path {
256+
f
257+
} else {
258+
let log_dir = env::temp_dir();
259+
std::fs::create_dir_all(&log_dir).unwrap();
256260

257-
let id = unique_test_run_identifier();
261+
let mut log_path = log_dir;
262+
log_path.push(format!("gel-server-{}.log", unique_test_run_identifier()));
263+
log_path
264+
};
258265

259-
let mut log_file = log_dir.clone();
260-
let file_name = format!("gel-server-{id}.log").to_string();
261-
log_file.push(file_name);
266+
eprintln!("Writing server logs into {:?}", &log_path);
262267

263-
eprintln!("Writing server logs into {:?}", &log_file);
264-
265-
std::fs::create_dir_all(&log_dir).unwrap();
266-
let mut log_file = File::create(log_file).unwrap();
268+
let mut log_file = File::create(log_path).unwrap();
267269

268270
let mut reader = BufReader::new(stream);
269271
std::io::copy(&mut reader, &mut log_file).unwrap();

0 commit comments

Comments
 (0)