Skip to content

Commit a7fd3de

Browse files
committed
[FIX] Route parser notices through the message target
1 parent 31264a1 commit a7fd3de

1 file changed

Lines changed: 105 additions & 36 deletions

File tree

‎src/rust/src/parser.rs‎

Lines changed: 105 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use lib_ccxr::util::log::{DebugMessageFlag, DebugMessageMask, ExitCause, OutputT
88
use num_integer::Integer;
99
use std::cell::Cell;
1010
use std::convert::TryInto;
11+
use std::fmt::Arguments;
1112
use std::fs::File;
12-
use std::io::{prelude::*, BufReader};
13+
use std::io::{self, prelude::*, BufReader};
1314
use std::path::PathBuf;
1415
use std::str::FromStr;
1516
use std::string::String;
@@ -101,7 +102,29 @@ where
101102
}
102103
}
103104

104-
fn process_word_file(filename: &str, list: &mut Vec<String>) -> Result<(), std::io::Error> {
105+
fn print_notice(target: OutputTarget, message: Arguments<'_>) {
106+
// The global logger gets its target after option parsing returns.
107+
let _ = write_notice(target, message, &mut io::stdout(), &mut io::stderr());
108+
}
109+
110+
fn write_notice(
111+
target: OutputTarget,
112+
message: Arguments<'_>,
113+
stdout: &mut impl Write,
114+
stderr: &mut impl Write,
115+
) -> io::Result<()> {
116+
match target {
117+
OutputTarget::Stdout => writeln!(stdout, "{message}"),
118+
OutputTarget::Stderr => writeln!(stderr, "{message}"),
119+
OutputTarget::Quiet => Ok(()),
120+
}
121+
}
122+
123+
fn process_word_file(
124+
filename: &str,
125+
list: &mut Vec<String>,
126+
messages_target: OutputTarget,
127+
) -> io::Result<()> {
105128
let file = File::open(filename)?;
106129
let reader = BufReader::new(file);
107130
let mut num = 0;
@@ -115,8 +138,11 @@ fn process_word_file(filename: &str, list: &mut Vec<String>) -> Result<(), std::
115138

116139
let new_len = line.trim().len();
117140
if new_len > CCX_DECODER_608_SCREEN_WIDTH {
118-
println!(
119-
"Word in line {num} too long, max = {CCX_DECODER_608_SCREEN_WIDTH} characters."
141+
print_notice(
142+
messages_target,
143+
format_args!(
144+
"Word in line {num} too long, max = {CCX_DECODER_608_SCREEN_WIDTH} characters."
145+
),
120146
);
121147
continue;
122148
}
@@ -218,7 +244,10 @@ impl OptionsExt for Options {
218244
self.write_format_rewritten = true;
219245

220246
if self.send_to_srv && args.out.unwrap_or(OutFormat::Null) != OutFormat::Bin {
221-
println!("Output format is changed to bin\n");
247+
print_notice(
248+
self.messages_target,
249+
format_args!("Output format is changed to bin\n"),
250+
);
222251
self.set_output_format_type(OutFormat::Bin);
223252
return;
224253
}
@@ -265,7 +294,10 @@ impl OptionsExt for Options {
265294

266295
fn set_input_format(&mut self, args: &Args) {
267296
if self.input_source == DataSource::Tcp {
268-
println!("Input format is changed to bin\n");
297+
print_notice(
298+
self.messages_target,
299+
format_args!("Input format is changed to bin\n"),
300+
);
269301
self.set_input_format_type(InFormat::Bin);
270302
return;
271303
}
@@ -473,6 +505,16 @@ impl OptionsExt for Options {
473505
capitalization_list: &mut Vec<String>,
474506
profane: &mut Vec<String>,
475507
) {
508+
if args.stdout {
509+
self.cc_to_stdout = true;
510+
if self.messages_target == OutputTarget::Stdout {
511+
self.messages_target = OutputTarget::Stderr;
512+
}
513+
}
514+
if args.quiet {
515+
self.messages_target = OutputTarget::Quiet;
516+
}
517+
476518
if args.stdin {
477519
unsafe {
478520
set_binary_mode();
@@ -672,19 +714,6 @@ impl OptionsExt for Options {
672714
self.timestamp_map = true;
673715
}
674716

675-
if args.es
676-
|| args.ts
677-
|| args.ps
678-
|| args.asf
679-
|| args.wtv
680-
|| args.mp4
681-
|| args.mkv
682-
|| args.dvr_ms
683-
|| args.input.is_some()
684-
{
685-
self.set_input_format(args);
686-
}
687-
688717
if let Some(ref codec) = args.codec {
689718
match codec {
690719
CCXCodec::Teletext => {
@@ -787,6 +816,20 @@ impl OptionsExt for Options {
787816
self.set_output_format(args);
788817
}
789818

819+
// Report output can make diagnostics quiet before input format selection.
820+
if args.es
821+
|| args.ts
822+
|| args.ps
823+
|| args.asf
824+
|| args.wtv
825+
|| args.mp4
826+
|| args.mkv
827+
|| args.dvr_ms
828+
|| args.input.is_some()
829+
{
830+
self.set_input_format(args);
831+
}
832+
790833
// --- report-format (used by -out=report) ---
791834
if let Some(ref fmt) = args.report_format {
792835
self.report_format = Some(fmt.to_lowercase());
@@ -1001,13 +1044,6 @@ impl OptionsExt for Options {
10011044
self.is_608_enabled = true;
10021045
}
10031046

1004-
if args.stdout {
1005-
if self.messages_target == OutputTarget::Stdout {
1006-
self.messages_target = OutputTarget::Stderr;
1007-
}
1008-
self.cc_to_stdout = true;
1009-
}
1010-
10111047
if args.pesheader {
10121048
self.pes_header_to_stdout = true;
10131049
}
@@ -1025,10 +1061,6 @@ impl OptionsExt for Options {
10251061
self.ignore_pts_jumps = false;
10261062
}
10271063

1028-
if args.quiet {
1029-
self.messages_target = OutputTarget::Quiet;
1030-
}
1031-
10321064
if args.debug {
10331065
self.debug_mask =
10341066
DebugMessageMask::new(DebugMessageFlag::VERBOSE, DebugMessageFlag::VERBOSE);
@@ -1433,9 +1465,14 @@ impl OptionsExt for Options {
14331465
}
14341466

14351467
if self.extract_chapters {
1436-
println!("Request to extract chapters recieved.");
1437-
println!("Note that this must only be used with MP4 files,");
1438-
println!("for other files it will simply generate subtitles file.\n");
1468+
print_notice(
1469+
self.messages_target,
1470+
format_args!(
1471+
"Request to extract chapters recieved.\n\
1472+
Note that this must only be used with MP4 files,\n\
1473+
for other files it will simply generate subtitles file.\n"
1474+
),
1475+
);
14391476
}
14401477

14411478
if self.gui_mode_reports {
@@ -1449,7 +1486,13 @@ impl OptionsExt for Options {
14491486

14501487
if self.sentence_cap_file.exists() {
14511488
if let Some(sentence_cap_file) = self.sentence_cap_file.to_str() {
1452-
if process_word_file(sentence_cap_file, capitalization_list).is_err() {
1489+
if process_word_file(
1490+
sentence_cap_file,
1491+
capitalization_list,
1492+
self.messages_target,
1493+
)
1494+
.is_err()
1495+
{
14531496
fatal!(
14541497
cause = ExitCause::ErrorInCapitalizationFile;
14551498
"There was an error processing the capitalization file.\n"
@@ -1462,7 +1505,9 @@ impl OptionsExt for Options {
14621505
add_builtin_profane(profane);
14631506
if self.filter_profanity_file.exists() {
14641507
if let Some(filter_profanity_file) = self.filter_profanity_file.to_str() {
1465-
if process_word_file(filter_profanity_file, profane).is_err() {
1508+
if process_word_file(filter_profanity_file, profane, self.messages_target)
1509+
.is_err()
1510+
{
14661511
fatal!(
14671512
cause = ExitCause::ErrorInCapitalizationFile;
14681513
"There was an error processing the profanity file.\n"
@@ -1576,7 +1621,10 @@ impl OptionsExt for Options {
15761621

15771622
if self.write_format == OutputFormat::WebVtt && self.enc_cfg.encoding != Encoding::UTF8 {
15781623
self.enc_cfg.encoding = Encoding::UTF8;
1579-
println!("Note: Output format is WebVTT, forcing UTF-8");
1624+
print_notice(
1625+
self.messages_target,
1626+
format_args!("Note: Output format is WebVTT, forcing UTF-8"),
1627+
);
15801628
}
15811629

15821630
// Check WITH_LIBCURL
@@ -2564,6 +2612,27 @@ pub mod tests {
25642612
// OUTPUT OPTION TESTS
25652613
// =========================================================================
25662614

2615+
#[test]
2616+
fn test_notice_follows_messages_target() {
2617+
for (target, expected_stdout, expected_stderr) in [
2618+
(OutputTarget::Stdout, "parser notice\n", ""),
2619+
(OutputTarget::Stderr, "", "parser notice\n"),
2620+
(OutputTarget::Quiet, "", ""),
2621+
] {
2622+
let mut stdout = Vec::new();
2623+
let mut stderr = Vec::new();
2624+
write_notice(
2625+
target,
2626+
format_args!("parser notice"),
2627+
&mut stdout,
2628+
&mut stderr,
2629+
)
2630+
.unwrap();
2631+
assert_eq!(stdout, expected_stdout.as_bytes());
2632+
assert_eq!(stderr, expected_stderr.as_bytes());
2633+
}
2634+
}
2635+
25672636
#[test]
25682637
fn test_stdout_redirects_to_stdout() {
25692638
let (options, _) = parse_args(&["--stdout"]);

0 commit comments

Comments
 (0)