Can pytest-html only shows captured log but no captures stdout or stderr? - pytest

enter image description here
As the picture shows, but I only want the captured log not captured stdout.

Related

redirected STDOUT without ending EOL rests in a buffer and is not shown

I am writing a program (a commandline frontend) which redirects stdout and stdin of arbitrary commandline programs. The problem is platform-independent. To understand the problem, I have written a simple commandline program where the problem occurs:
#include <stdio.h>
main () {
char Expression[200];
printf ("Enter first expression: "); scanf ("%s", Expression);
printf ("You have entered: %s\n", Expression);
printf ("\n");
printf ("(Now query with Stderr)\n");
fprintf (stderr, "Enter second expression: "); scanf ("%s", Expression);
printf ("You have entered: %s\n", Expression);
}
The stdout "Enter the first expression" rests in the output buffer and is not sent via redirected stdout pipe to my program. So the first question is "Enter second expression" because the buffer problem only occurs with stdout, not with stderr. The buffer content (first query) is sent if the user has typed the input and presses RETURN. So stdout only runs through the pipe after EOL, stderr shows the output immediately.
If you run Info-Zip UNZIP and you are unzipping files which already exist, UNZIP sends a query:
replace myfile.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:
and this query is sent via stderr, so the problem does not occur. But other programs run into the problem.
The commandline frontend of Windows 10 has been rewritten. If such a situation occurs (e.g. using the cmd.exe integrated "copy" command when overwriting an existing file), the new commandline frontend waits 3 seconds (!) and then shows the buffered query. So it seems that the Microsoft programmers need to write a "dirty hack" to solve this problem.
My question is: How to force the user program to spit out the stdout buffer if no EOL has been received? I have full access to the session where the user program runs by a special helper program in the same session which enables communication between my graphical frontend program and the text window where cmd.exe runs.
I realized now that the "fflush" command has to be placed in the user program. Stdout is always buffered when redirecting, Stderr is not. That is the reason that stderr output can happen before the stdout output. I wrote a test sample where the stderr output is shown 4 lines before it should be shown.
So that means that all programmers which are writing to stdout and stderr or write user queries have to flush stdout:
//first case: query and user input
char Expression[200];
printf ("Enter an expression: "); fflush (stdout); scanf ("%s", Expression);
printf ("You have entered: %s\n", Expression);
//second case: stderr output
fflush (stdout); fprintf (stderr, "An error has occured.");
I checked Internet postings and it seems that this problem occurs on all platforms.
So if you write programs with stdout and stderr, use fflush(stdout) in these two cases. The reverse case (using stderr and then stdout) is no problem, because stderr does not get buffered. (I hope I am right for all platforms.)
No, I have no option to override the programs the users execute in my commandline frontend program.
Everyone can redirect stdout and stderr to a single text file:
[C:\] userprog >output.txt 2>&1
That means both stdout and stderr is redirected to one file "output.txt". If stdout is not flushed correctly, the incorrect order is shown in the text file.
I have realized that all programs from professional source are flushing stdout in these cases and generate a proper order.

How to redirect ssh->capture2 output to STDOUT without buffering?

I am using Net::OpenSSH module. The capture2 function returns the output to a variable which I can print to get the output but I want to do this directly to STDOUT without buffering.

Output stdin, stdout to file and console using Perl

I am trying a simple questionnaire using perl. I want to record the responses in a log file as and when a user inputs it. I'm having problem in redirecting the stdin to file.
Below is the code I implemented. Refer this.
open my $tee, "|-", "tee some_file.out";
print $tee "DO you want to continue?(y/n)\n";
$var=<STDIN>;
$var =~ s/[\n\r\f\t]//g;
if($var eq "y"){
print $tee "Enter\n";
}
close $tee;
The output I'm getting now is, only after user input is provided the question is printed.
#in console
y
DO you want to continue?(y/n)
Enter
#some_file.out
DO you want to continue?(y/n)
Enter
Below is the expected output:
#in console
DO you want to continue?(y/n)
y
Enter
#some_file.out
DO you want to continue?(y/n)
y
Enter
I also found Duplicate stdin to stdout but really couldn't achieve what I want to.
Am I missing something?!
Is there any cleaner solution available?
First of all, never use the phrase "redirecting the stdin to..." because stdin is input. It doesn't go to anything. It comes from somewhere.
It seems that what you expected is to have a copy of $var appear in your log file. Since you never printed $var to $tee there's no way that could happen.
So why did you think $var would appear in the log file? From the way you have shown us a copy of the log file next to a copy of what you see on the terminal, I guess that your reasoning went something like this:
The tee put all of the output into the log file
The tee also put all of the output on the terminal
My program didn't output anything else besides what went into the tee
The screen contents should match the log file
But there's a hidden assumption that's required to reach the conclusion:
3a. Nothing else was written to the terminal besides my program's output
And that's the part which is incorrect. When you type y into the terminal while your program is running, the terminal itself echoes what you type. It prints a copy in the terminal window, and also sends the character to your program's stdin. The y that you see on screen is not part of your program's output at all.
Since the echoing is done by the terminal and not by your program, you can't instruct it to also send a copy to your log file. You need to explicitly print it there if you want it to be logged.
You can ask the terminal to stop echoing, and then you take responsibility for printing the characters as they are typed so the user can see what they're typing. If you want to try that, see the Term::ReadKey module.
Or if what you really want is a complete record of everything that appeared on the terminal during the run of your program, maybe you should run it in the standard unix tool script, which is made for exactly that purpose.
(Side note: Did you know about the IO::Tee module? you can have teed output without an external process)

Why doesn't the output get redirected into the file?

I have the following command line that outputs vmstat every second with a time stamp on each line via the perl script:
vmstat 15 | /home/Beer/addtimestamp.pl > File_1
the contents of the addtimestamp.pl:
!/usr/bin/perl
while (<>) { print localtime() . ": $_"; }
So why doesn't the output get redirected to the "File_1" file?
It works perfectly when I don't, it prints out the output perfectly every second with no issues at all.
When outputting to the terminal, perl's output is line buffered, so you will see every line as it is output. When its output is a file, it will be block buffered so you will not see any output until a full block is ready to write (4k I think, but variable and system-defined).
You need to set stdout to use line buffering:
$|=1;
Search for [perl line buffered output] and you'll see plenty of results about this.

Redirecting the output of expect to logfile in expect

I am using expect in perl. I want to redirect all the output that appears on the stdout console to a log file so that i can debug it in future. Currently i am using
$exp->log_stdout(0);
Instead of redirecting to this can i do it to a log file? if so how to do it?
if you see the documentation of Expect, you will find information about Log session to a file
$object->log_file("filename" | $filehandle | \&coderef | undef)
All characters send to or received from the spawned process are written to the file. Normally appends to the logfile, but you can pass an additional mode of "w" to truncate the file upon open():
$object->log_file("filename", "w");
That means use log_file method instead of log_stdout.
Your problem will be solved.