[Spice-devel] [PATCH] Add utility to help redirecting output from remote-viewer/virt-viewer

Frediano Ziglio fziglio at redhat.com
Wed Mar 2 17:13:56 UTC 2016


> 
> Frediano,
> 
> On Tue, Mar 1, 2016 at 6:12 PM, Frediano Ziglio <fziglio at redhat.com> wrote:
> >
> >>
> >> Hi
> >>
> >> ----- Original Message -----
> >> > 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.
> >>
> >> This looks like it should replace windows-cmdline-wrapper altogether.
> >>
> >
> > Really? I though the two utility were written for different purposes.
> > I though your utility was a complement of the AttachConsole in the
> > executable
> > so the executable detect them and can provide some control/feedback using
> > the console.
> >
> > If it's just redirect to console if parent has a console or to file if we
> > want to redirect to file probably we can find a solution in
> > virt-viewer/remote-viewer that satisfy both requirements.
> 
> Tested you program on a Windows machine. Seems to work as expected, so
> I'd remove the current wndows-command-line-wrapper.c in favour of
> yours.
> 
> One question, can you teach you program to do nothing when no redirect
> is needed?
> I mean, if no " > log.txt", then just print everything on the
> stdout/stderr as usual.
> 
> Please, after addressing Marc-André comments about Unicode, can you
> submit a v2, but then to the virt-tools-list? It's a solution for
> virt-viewer, so it's better if we do the review there.
> 

Wrote a new patch that should replace this utility.
Hope to have understood what's the expected result.

Frediano

> >
> > Frediano
> >
> >> >
> >> > 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
> >> >
> >>
> > _______________________________________________
> > Spice-devel mailing list
> > Spice-devel at lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/spice-devel
> 
> Best Regards,
> --
> 


More information about the Spice-devel mailing list