Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

PB/CC: Redirect 'stderr' output to file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PB/CC: Redirect 'stderr' output to file

    When launching a PB/CC program with redirection of STDERR, STDERR output goes nowhere, the documentation stating "unless it has been redirected" nothwithstanding.

    I was recently informed by PB Support that the error is in fact in the documentation, as the compiler was not designed to support redirection of the STDERR function (or perhaps not designed to support redirection of STDERR to somewhere other than where STDOUT is redirected?).

    PB has added a New Feature Suggestion to support independent redirection of the STDERR function.

    Until such time as that feature might be implemented, here is a program demonstrating both the problem and the solution

    Instructions: Compile the program; then execute from the command line or command-file using
    Code:
    CMD /C stderr1.exe 1>my_stdout.txt 2>my_stderr.txt
    DEMO PROGRAM SOURCE CODE
    Code:
    ' STDERR1.BAS
    ' 11.09.08
    ' Demonstration of how to redirect 'stderr' output to a file; 
    ' PB/CC STDERR function does not support this (help files 4x, 5.0 notwithstanding) 
    ' Author: MIchael Mattias Racine WI
    ' Date:  Nov 9 2008
    ' To run demo:
    ' Compile program,
    ' Run from command line or command file using:
    ' CMD /C stderr1.exe 1>my_stdout.txt 2>my_stderr.txt
    
    #COMPILE  EXE
    #COMPILER PBCC
    #DIM      ALL
    
    #INCLUDE "WIN32API.INC"
    
    FUNCTION STDERR2 (s AS STRING) AS LONG
     LOCAL hStdErr AS LONG, hFile AS LONG
    
      hStdErr =  GetStdHandle (%STD_ERROR_HANDLE)
      hFile   =  FREEFILE
      ' NOTE: can open for OUTPUT if desired; however, this results in only the
      ' LAST "STDERR2" command being stored in the file.
      ' i.e., when using OPEN FOR APPEND you must remember to delete the output file
      ' before writing to STDERR file or you will get outputs from prior runs.
      OPEN HANDLE hStdErr FOR APPEND AS hFile
      PRINT #hFile, S
      CLOSE hFile
    
    END FUNCTION
    
    
    FUNCTION PBMAIN () AS LONG
    
         LOCAL Z  AS LONG
    
         FOR Z = 1 TO 10
             STDOUT  USING$ ("STDOUT  line #", Z)
         NEXT
         FOR Z = 1 TO 10
             STDERR  USING$ ("STDERR  line #", Z)
         NEXT
         
      ' COMMENT NEXT THREE LINES OUT TO PROVE TO YOURSELF STDERR function
      ' produces no output when redirected to file.
         FOR Z = 1 TO 10
             STDERR2 USING$ ("STDERR2 line #", Z)
         NEXT
    
         MessageBeep %MB_OK
         
         PRINT "End of job"    '
     ' ****************************************
     ' NEW BUG I FOUND CREATING THIS DEMO
     ' DOC for PRINT SAYS...
     ' " Output of the PRINT statement cannot be redirected to a file or device,
     ' it always goes to the current active console screen."
     ' Demo shows PRINT output goes nowhere, either.
     ' Compiler is not creating a console when STDOUT is redirected,
     ' even when needed to support PRINT verb?
     ' Will be reported.
     ' ********************************************
       
         WAITKEY$
    
    END FUNCTION
    MCM
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

  • #2
    Re the problem with PRINT noted above, we have an official response from PowerBASIC support:

    I see. If standard output and standard error are both re-directed then
    the print statement is not being displayed on the console screen.
    Re-directing standard error is not supported and if you would like to
    print to the console, then I would recommend that this not be done.
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment

    Working...
    X