[Spice-devel] [PATCH] Add utility to help redirecting output from remote-viewer/virt-viewer
Victor Toso
lists at victortoso.com
Tue Mar 1 13:42:59 UTC 2016
Hi,
On Tue, Mar 01, 2016 at 01:29:13PM +0000, Frediano Ziglio wrote:
> These utility use some trick to use console even if they are GUI
> application (not supposed to have a console by default).
> This make output redirection harder as output by default will tend
> to go to console of parent process instead of using redirected output.
> This utility avoid remote-viewer/virt-viewer to see the calling process
> console keeping the wanted redirection.
>
And this is how you get free beers :)
Not sure what is better, this or having some handling to redirect logs
to file. What works is good for me!
I'll be testing this very soon! Thanks!
> Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
> ---
> src/Makefile.am | 4 ++
> src/output-redirect-wrapper.c | 92 +++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 96 insertions(+)
> create mode 100644 src/output-redirect-wrapper.c
>
> diff --git a/src/Makefile.am b/src/Makefile.am
> index e76f5bc..01b9bc8 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -155,6 +155,10 @@ bin_PROGRAMS += windows-cmdline-wrapper
> windows_cmdline_wrapper_SOURCES = windows-cmdline-wrapper.c
> windows_cmdline_wrapper_LDFLAGS = -lpsapi
>
> +bin_PROGRAMS += output-redirect-wrapper
> +output_redirect_wrapper_SOURCES = output-redirect-wrapper.c
> +output_redirect_wrapper_CPPFLAGS = -DUNICODE -D_UNICODE
> +
> virt-viewer_rc.$(OBJEXT): $(VIRT_VIEWER_RES) $(ICONDIR)/virt-viewer.ico
> $(AM_V_GEN)$(WINDRES) \
> -DICONDIR='\"$(ICONDIR)\"' \
> diff --git a/src/output-redirect-wrapper.c b/src/output-redirect-wrapper.c
> new file mode 100644
> index 0000000..ed807bb
> --- /dev/null
> +++ b/src/output-redirect-wrapper.c
> @@ -0,0 +1,92 @@
> +/*
> + * Windows cmd: a command line wrapper for GUI applications
> + *
> + * Copyright (C) 2012-2016 Red Hat, Inc.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
> + *
> + * Usage:
> + * This wrapper allows to redirect output even if your application
> + * tries to redirect its output to console.
> + *
> + * For instance you can use from console with a syntax like
> + *
> + * output-redirect-wrapper remote-viewer.exe --debug > log.txt
> + *
> + * Based on Marc-André Lureau wrappers
> + */
> +
> +#include <windows.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <tchar.h>
> +
> +int main(void)
> +{
> + STARTUPINFO si = { 0, };
> + PROCESS_INFORMATION pi = { 0, };
> +
> + // pass same input/output handles
> + si.cb = sizeof(si);
> + si.dwFlags = STARTF_USESTDHANDLES;
> + si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
> + // redirect all to single output
> + si.hStdError = si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
> + SetHandleInformation(si.hStdInput, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
> + SetHandleInformation(si.hStdOutput, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
> +
> + // parse command line, strip executable
> + TCHAR *cmdline = GetCommandLine();
> + cmdline += _tcsspn(cmdline, _T(" \t\v"));
> + TCHAR *end = _tcsstr(cmdline, cmdline[0] == _T('\"') ? _T("\" ") : _T(" "));
> + if (!end) {
> + fprintf(stderr, "Please pass executable to call\n");
> + return 1;
> + }
> + cmdline = end + 1;
> + cmdline += _tcsspn(cmdline, _T(" \t\v"));
> +
> + // release console so program launched do not redirect to it
> + FreeConsole();
> +
> + if (!CreateProcess(NULL,
> + cmdline,
> + NULL, // Process handle not inheritable
> + NULL, // Thread handle not inheritable
> + TRUE, // Handle inheritance
> + 0, // No creation flags
> + NULL, // Use parent's environment block
> + NULL, // Use parent's starting directory
> + &si,
> + &pi)) {
> + fprintf(stderr, "CreateProcess failed (%ld).\n", (long) GetLastError());
> + return 1;
> + }
> + // Close unneeded handle
> + CloseHandle(pi.hThread);
> +
> + // Wait until child process exits.
> + WaitForSingleObject(pi.hProcess, INFINITE);
> +
> + DWORD exit_code;
> + GetExitCodeProcess(pi.hProcess, &exit_code);
> +
> + // Close process handle
> + CloseHandle(pi.hProcess);
> +
> + // return same output of application
> + ExitProcess(exit_code);
> + return 0;
> +}
> --
> 2.5.0
>
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/spice-devel
More information about the Spice-devel
mailing list