1

I have a windows (VS2013) c++ application. throughout the application I have calls to printf to output information. In order to better look through some of that information I wanted to try using findstr on it. while debugging this I am unable to tell where the information is being output to. The application has multiple threads.

myApplication.exe > foo.txt 2>&1
# here is a bunch of stuff that is printed to the console when I don't think it should be.

will create an empty file named foo.txt with no output and the messages will be written to the console afterwards, this confuses me as I think the above command should pipe both stderr and stdout to the file foo.txt. running type foo.txt shows an empty file.

similarly I am unable to pipe the output to findstr or similar or even tell where it is being output (it looks like normal stdout)

running the command from a cygwin shell (which I tried as I assume there is some problem with stdout or stderr and the text is being piped to something else) does not show any output, which supports my idea that there may be a problem with that. where is the text being output to?

EDIT: it seems to have become relevant that I am using chromium embedded framework which, upon searching the code seems to have some stuff like:

// Add console so that 'logging' (really printf) is captured
// TODO: drop this once we no longer need it?
if(AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole()){
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);
}

which seems pretty culpable, thanks to @frymode for pointing me in the right direction, I'll update with a solution once I've found one.

Mike H-R
  • 7,726
  • 5
  • 43
  • 65
  • @dlf: not at all, this seems to be asking *about* console applications (the linked question asks about non-console applications). The asker here is wondering why piping doesn't seem to work as expected. – EyasSH Jan 20 '15 at 15:42
  • I'm confused, why would you need to run a windows app through cygwin? Isn't that for running Linux apps on Windows? I thought printf just always outputted to stdout or stderr? If you want these outputted to a file, you could try using freopen to redirect the streams to files. – Rubix Rechvin Jan 20 '15 at 15:48
  • @RubixRechvin you're right, I have no wish (or need) to run this through cygwin. I just thought something weird was going on with the stdout/stderr streams and printf seems to be printing to something else (no idea what, I can't seem to figure this out, which is what I'm trying to understand). Because (I thought) something weird was going on with stdout/stderr I thought I'd see what happened on cygwin (as I assume it handles stdout and stderr differently). my evidence for the printf printing somewhere other than stdout/stderr is that `myApplication.exe > foo.txt 2>&1` still produces output. – Mike H-R Jan 20 '15 at 15:53
  • @RubixRechvin: No, Cygwin is a POSIX-like subsystem that runs under Windows. It can't run programs compiled for Linux, though many (not all) Linux programs can be recompiled to run under Cygwin. Programs compiled under Cygwin are Windows programs that (usually) depend on `cygwin1.dll`. – Keith Thompson Jan 20 '15 at 16:00
  • `printf` goes to `stdout`. The question is, where is `stdout` going? Can you reproduce the problem with a small program that you can include in your question (say, the classic "Hello, world" program)? – Keith Thompson Jan 20 '15 at 16:01
  • @KeithThompson, that's what I'm afraid of, I'll try to get to work on a SSCE to show this but my memory has always been that programs can/should only output to stdout or stderr and that normal `printf` does that. I'm worried some weird dependency I can't find is changing this, I guess I'm looking for insight on how it could be changed in order to find how this is happening. – Mike H-R Jan 20 '15 at 16:03
  • The only thing I can think of is someone might have redirected the streams somewhere with [`SetStdHandle`](http://msdn.microsoft.com/en-gb/library/windows/desktop/ms686244%28v=vs.120%29.aspx) or the like. – sjdowling Jan 20 '15 at 16:24
  • 2
    Is your application console one or windows? (Is console windows opened when it is started from explorer?) Maybe someone tried [these](http://stackoverflow.com/a/27651806/4074081) kind of ticks. – dewaffled Jan 20 '15 at 16:25
  • @frymode, hmm... that's a good idea, my application is a window application (as in creates a window) and creates a console if there is one that doesn't exist. – Mike H-R Jan 20 '15 at 16:27

0 Answers0