Python Forum
a script i am thinking about creating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a script i am thinking about creating
#1
i am thinking about writing a script that runs a specified command with arguments passed to it while capturing stdout and stderr separately to prefix each line with "out" and "err" according to which that line came from, then merging them together, interleaved in the order the newline was output, with an optional time stamp prefix added to each line. i often can use this kind of logging and just settle for writing stdout and stderr to separate files or merge them using 2>&1 with no way to distinguish each output.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Command 1>> stdout.txt 2>> stderr.txt
Reply
#3
(Feb-27-2018, 06:39 AM)Larz60+ Wrote: Command 1>> stdout.txt 2>> stderr.txt

that's what i'm already doing (though not always doing append).

what i want is one file (or output to stdout) where the original two outputs are merged in time order (the time of a line being when its newline is output) and each line is prefixed to indicate whether it came from stdout or from stderr. so if a program being run under this does:

    ...
    print('foo',file=sys.stdout)
    sys.stdout.flush()
    print('xyzzy',file=sys.stderr)
    sys.stderr.flush()
    print('bar',file=sys.stdout)
    sys.stdout.flush()
    ...
then i would get:
Output:
... out foo err xyzzy out bar ...
and it would be clear that "xyzzy" was written to stderr after "foo" was written to stdout, and before "bar" was written to stdout. there would be 2 pipes from the program being run and my script would have to read both pipes concurrently and unbuffered. i might also need to make those 2 pipe be 2 ptys to run programs that modify how they operate if stdout and/or stderr are not ttys.

options i am thinking about for this script are --prefix=date and --prefix=time and --stdoutprefix=out and --stderrprefix=err where the order of giving the options defines the order the prefixes they specify are placed on the respective output lines.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What WERE They Thinking : Python 3.9x millpond 8 3,727 Jun-28-2021, 01:20 AM
Last Post: millpond
  a project i am thinking about Skaperen 0 1,398 Mar-11-2020, 10:56 PM
Last Post: Skaperen
  Determing packages needed by a script: Creating a Miniconda environment ashevade 1 1,789 Jan-27-2020, 09:25 AM
Last Post: buran
  a program i have been thinking about Skaperen 0 2,547 Jun-25-2017, 04:44 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020