Skip to content

Commit 4e51e7f

Browse files
committed
[Logging]: Added conditional logging support for stdout
1 parent a23abf6 commit 4e51e7f

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/muxer.rs

+26-16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod ffi {
1818

1919
extern "Rust" {
2020
fn start(pairing_file: String, log_path: String) -> Result<(), Errors>;
21+
fn startWithLogger(pairing_file: String, log_path: String, is_console_logging_enabled: bool) -> Result<(), Errors>;
2122
fn target_minimuxer_address();
2223
}
2324
}
@@ -257,9 +258,13 @@ pub static STARTED: AtomicBool = AtomicBool::new(false);
257258
pub static STARTED: AtomicBool = AtomicBool::new(true); // minimuxer won't start in tests
258259

259260
/// Starts the muxer and heartbeat client
260-
/// # Arguments
261+
/// # Arguments\
261262
/// Pairing file contents as a string and log path as a string
262263
pub fn start(pairing_file: String, log_path: String) -> crate::Res<()> {
264+
startWithLogger(pairing_file, log_path, true) // logging is enabled by default as before
265+
}
266+
267+
pub fn startWithLogger(pairing_file: String, log_path: String, is_console_logging_enabled: bool) -> crate::Res<()> {
263268
use fern::Dispatch;
264269
use log::LevelFilter;
265270

@@ -272,7 +277,7 @@ pub fn start(pairing_file: String, log_path: String) -> crate::Res<()> {
272277
}
273278

274279
// the logger failing to initialize isn't a problem since it will only fail if it has already been initialized
275-
if Dispatch::new()
280+
let mut logger = Dispatch::new()
276281
.format(|out, message, record| {
277282
out.finish(format_args!(
278283
"{} [{}] {}: {}",
@@ -281,9 +286,11 @@ pub fn start(pairing_file: String, log_path: String) -> crate::Res<()> {
281286
record.target(),
282287
message
283288
))
284-
})
285-
.chain(
286-
// stdout
289+
});
290+
291+
// conditionally enable stdout logging only if requested
292+
if is_console_logging_enabled {
293+
logger = logger.chain(
287294
Dispatch::new()
288295
.level(LevelFilter::Trace)
289296
.level_for("plist_plus", LevelFilter::Off) // plist_plus spams logs
@@ -295,17 +302,20 @@ pub fn start(pairing_file: String, log_path: String) -> crate::Res<()> {
295302
.level_for("hyper", LevelFilter::Off)
296303
.level_for("tracing", LevelFilter::Off) // maybe we shouldn't do this?
297304
.chain(std::io::stdout()),
298-
)
299-
.chain(
300-
// minimuxer.log
301-
Dispatch::new()
302-
.level(LevelFilter::Off)
303-
.level_for("minimuxer", LevelFilter::Info)
304-
.level_for("rusty_libimobiledevice", LevelFilter::Error)
305-
.chain(File::create(&log_path).unwrap()),
306-
)
307-
.apply()
308-
.is_ok()
305+
);
306+
}
307+
308+
logger = logger.chain(
309+
// minimuxer.log
310+
Dispatch::new()
311+
.level(LevelFilter::Off)
312+
.level_for("minimuxer", LevelFilter::Info)
313+
.level_for("rusty_libimobiledevice", LevelFilter::Error)
314+
.chain(File::create(&log_path).unwrap()),
315+
);
316+
317+
// apply logger
318+
if logger.apply().is_ok()
309319
{
310320
info!("Logger initialized!!");
311321
}

0 commit comments

Comments
 (0)