From 46f68fdc936dc631728fbb2dd743a315218bf228 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Mon, 11 Jan 2016 16:33:15 +0100 Subject: [PATCH] main: provide -E option to redirect stderr to file --- src/iohelpers.cpp | 9 +++++++++ src/iohelpers.h | 1 + src/main.cpp | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/iohelpers.cpp b/src/iohelpers.cpp index dd47dbc..ef7f999 100644 --- a/src/iohelpers.cpp +++ b/src/iohelpers.cpp @@ -36,6 +36,15 @@ bool redirect_cout (const std::string& fn) return true; } +bool redirect_cerr (const std::string& fn) +{ + static std::ofstream alt_cerr; + alt_cerr.open (fn.c_str(), std::ios::out | std::ios::binary); + if (alt_cerr.fail()) return false; + std::cerr.rdbuf (alt_cerr.rdbuf()); + return true; +} + std::string escape_output (const std::string&s) { std::string r; diff --git a/src/iohelpers.h b/src/iohelpers.h index 079d8f6..7d46451 100644 --- a/src/iohelpers.h +++ b/src/iohelpers.h @@ -43,6 +43,7 @@ bool redirect_cin (const std::string& fn); bool redirect_cout (const std::string& fn); +bool redirect_cerr (const std::string& fn); #define readall_bufsize 8192 template diff --git a/src/main.cpp b/src/main.cpp index 80fd1f6..80d9cbd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,7 @@ void print_help (char*pname) out ("Global options:"); out (" -R, --in input file, default is stdin"); out (" -o, --out output file, default is stdout"); + out (" -E, --err the same for stderr"); out (" -a, --armor use ascii-armored I/O"); out (" -y, --yes assume that answer is `yes' everytime"); outeol; @@ -120,7 +121,7 @@ int main (int argc, char**argv) opt_import_no_action = false; std::string recipient, user, - input, output, + input, output, err_output, name, filter, action_param, detach_sign, @@ -144,6 +145,7 @@ int main (int argc, char**argv) //I/O redirection from default stdin/out {"in", 1, 0, 'R' }, {"out", 1, 0, 'o' }, + {"err", 1, 0, 'E' }, //keyring management {"list", 0, 0, 'k' }, @@ -183,7 +185,7 @@ int main (int argc, char**argv) option_index = -1; c = getopt_long (argc, argv, - "hVTayr:u:R:o:kipx:m:KIPX:M:g:N:F:fnsvedCb:S:", + "hVTayr:u:R:o:E:kipx:m:KIPX:M:g:N:F:fnsvedCb:S:", long_opts, &option_index); if (c == -1) break; @@ -231,6 +233,8 @@ int main (int argc, char**argv) "cannot accept multiple inputs") read_single_opt ('o', output, "cannot accept multiple outputs") + read_single_opt ('E', err_output, + "cannot accept multiple error outputs") read_action ('k') read_action ('i') @@ -323,6 +327,12 @@ int main (int argc, char**argv) goto exit; } + if (err_output.length() && !redirect_cerr (err_output)) { + progerr ("could not redirect to error output file"); + exitval = 1; + goto exit; + } + /* * check the option flags and do whatever was requested */