Jump to content

Recommended Posts

Hi.

While programming I often use ConsoleWrite() for debugging. If the script isn’t to big I often don’t do extra logging but let my ConsoleWrite()’s inside. Sometimes difficulties appear later when @compiled and weeks are gone. So my first thought often is let’s run the script and catch the console outs of my script. So I coded a console reader.

There are two ways to start the buggy script. First via $cmdline send to the reader and second with drag’n’drop onto the readers gui.

#include <AutoItConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <ColorConstants.au3>
#include <StaticConstants.au3>
#include <array.au3>
#include <WinAPIProc.au3>
#include <GuiEdit.au3>
#include <GuiRichEdit.au3>

Opt("GUIOnEventMode", 1)

Global $data = ""
Global $g_aPID = [0]
Global $g_bFreeze = False
Global $g_iZaehler = 0

Global $g_hGUI = GUICreate("Console: StdoutRead" , 800, 800, -1, -1, $WS_OVERLAPPEDWINDOW + $WS_CLIPCHILDREN, $WS_EX_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

Global $text = GUICtrlCreateEdit("",10,30,780,760, $ES_AUTOVSCROLL + $WS_VSCROLL + $ES_READONLY + $ES_NOHIDESEL)
GUICtrlSetState(-1, $GUI_DROPACCEPTED); + $GUI_FOCUS)
GUICtrlSetFont(-1, 9, -1, -1, "Lucida Console")
GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
GUISetOnEvent($GUI_EVENT_DROPPED, "_Dropped")
_GUICtrlEdit_SetLimitText($text, 8388608) ; a filesize about 1mb

Global $g_LaZeilen = GUICtrlCreateLabel("Zeilen: " & StringFormat("% 5d", $g_iZaehler), 680, 10, 100, 9, $SS_LEFTNOWORDWRAP, $WS_EX_LAYERED)
GUICtrlSetFont(-1, 9, -1, -1, "Lucida Console")
GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKSIZE)

Global $g_hCbFreeze = GUICtrlCreateCheckbox("&Freeze", 13, 5, 90)
GUICtrlSetFont(-1, 9, -1, -1, "Lucida Console")
GUICtrlSetResizing(-1, $GUI_DOCKALL)
GUICtrlSetOnEvent($g_hCbFreeze, "_Freeze")

Global $g_hBuCopy = GUICtrlCreateButton("&Copy All", 125, 5, 70, 20)
GUICtrlSetFont(-1, 9, -1, -1, "Lucida Console")
GUICtrlSetResizing(-1, $GUI_DOCKALL)
GUICtrlSetOnEvent($g_hBuCopy, "_Copy")

GUISetState(@SW_SHOW)
If $CmdLine[0] > 0 Then
    _ViaCmdline()
    _GUICtrlEdit_AppendText($text, $CmdLine[1] & @CRLF)
EndIf

Global $sText = StringFormat("% 5d", $g_iZaehler) & @TAB
_GUICtrlEdit_AppendText($text, $sText)

Local $nextline
While 1
    If $g_aPID[0] > 0 Then
        $nextline = _ConsoleReadLine()
        $nextline = StringReplace($nextline, @CRLF, @CRLF & StringFormat("% 5d", $g_iZaehler) & @TAB)
        $sText = $nextline
        If $g_bFreeze = False Then
            _GUICtrlEdit_AppendText($text, $sText)
        EndIf
        GUICtrlSetData($g_LaZeilen, "Zeilen: " & StringFormat("% 5d", $g_iZaehler))
    EndIf
    _ProcessExist()
WEnd

#region - Funcs
Func _ConsoleReadLine()
    Local $Result,$crPos
    While True
        _ProcessExist()
        For $i = 1 To $g_aPID[0]
            $data &= StdoutRead($g_aPID[$i])
            If @error Then ExitLoop
        Next
        $crPos = StringInStr($data, @CRLF)
        If $crPos Then
            $Result = StringLeft($data, $crPos) & @CRLF
            $data = StringRight($data, StringLen($data) - $crPos)
            $g_iZaehler += 1
            Return $Result
        EndIf
    WEnd
    Return SetError(1, 1, $data)
EndFunc

Func _Dropped()
    Local $hPID = Run(@GUI_DragFile, "", Default, $STDERR_MERGED)
    ConsoleWrite("DROP: " & $hPID & " " & @GUI_DragFile & @CRLF)
    _ArrayAdd($g_aPID, $hPID)
    $g_aPID[0] = UBound($g_aPID) - 1
EndFunc

Func _ViaCmdline()
    Local $hPID = Run($CmdLine[1], "", Default, $STDERR_MERGED)
    ConsoleWrite("CMDLINE: " & $hPID & " " & $CmdLine[1] & @CRLF)
    _ArrayAdd($g_aPID, $hPID)
    $g_aPID[0] = UBound($g_aPID) - 1
EndFunc

Func _Freeze()
    $g_bFreeze = Not $g_bFreeze
    ConsoleWrite("FREEZE: " & $g_bFreeze & @CRLF)
    GUICtrlSetState($text, $GUI_FOCUS)
EndFunc

Func _Copy()
    ConsoleWrite("COPY" & @CRLF)
    ClipPut(GUICtrlRead($text))
EndFunc

Func _ProcessExist()
    For $i = $g_aPID[0] To 1 Step - 1
        If Not ProcessExists($g_aPID[$i]) Then
            ConsoleWrite("GONE: " & $g_aPID[$i] & @CRLF)
            _ArrayDelete($g_aPID, $i)
            $g_aPID[0] = UBound($g_aPID) - 1
        EndIf
    Next
EndFunc

Func _Exit()
    If $CmdLine[0] = 0 Then ; if reader is started by $cmdline then no script will exit but reader
        For $i = 1 To $g_aPID[0]
            ConsoleWrite("KILL: " & $g_aPID[$i] & " " & _WinAPI_GetProcessFileName($g_aPID[$i]) & @CRLF)
            ProcessClose($g_aPID[$i])
        Next
    EndIf
    ConsoleWrite("EXIT" & @CRLF)
    Exit
EndFunc
#endregion Funcs

Maybe someone will find it useful too. 

One last remark. If only one script is given via $cmdline to the reader no scripts will exit if consolereader exits. But otherwise all scripts dropped onto the readers gui will exit too. This is by design. If you want to change this do it inside func _exit().

Regards, Conrad 

P.S. Possibly some #includes are not necessary anymore but have been while scripting.

Edited by Simpel
P.S.

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • By Burgs
      Greetings,
        I would like to be able to write a script to send commands to the console for creation of Gstreamer pipelines.  I was thinking of something similar to this:
      Local $iPID = Run("C:\Windows\System32\cmd", "", @SW_MAXIMIZE) ;THIS OPENS THE CONSOLE...!!! if $iPID == 0 Then ConsoleWrite(@CRLF & "I DID NOT OPEN CMD...error: " & @error & @CRLF) if $iPID <> 0 Then ConsoleWrite(@CRLF & "I OPENED CMD...!!!" & @CRLF) $hCmd = WinGetHandle("C:\WINDOWS\system32\cmd.exe") if $hCmd <> 0 Then WinActivate($hCmd) ;ensure command console is active... $sOutput = Send("cd C:\gstreamer\1.0\x86_64\bin" & @CRLF, $SEND_RAW) $sOutput = Send("gst-launch-1.0 videotestsrc ! autovideosink" & @CRLF, $SEND_RAW) Sleep(3000) ControlSend($hCmd, "", "", "exit" & @CR) EndIf ;$hCmd NOT "0"... I don't really know if this is the best way to open the console and send commands into it.  I'm also not sure about how to best catch any errors that may occur...likely this needs to be accomplished with the STDOUTREAD command however I've not had experience using it before and therefore would appreciate some advice that anybody may offer. 
      Basically I'm seeking guidance on how to best automate the opening of the console, sending lines of commands to be executed, and handling any potential errors in the execution of those commands...I thank you in advance.  Regards.
    • By Jeep
      Here is a small library (UDF) that I use all the time. Not transcendent, it’s just to simplify my life.
      When I use the ConsoleWrite function, more than 99.99 percent, I go to the line (Newline), more than 99.99 %, parameters à the same except information that i exam. _CW is born.
      The second one _CW_Fmt allows to display marks (Fmt for Formatting) and allows to evaluate the format the length of a variable and the position of some characters without counting too much :-).
      For the functions derived from MsgBox () again the same observation, always the same parameters following the context: Info, Error or Warning. Thus were born _MB_Info, _MB_Error and _MB_Warning.
      As for _MB_IsOk, the same observation about the logical answer we are interested in a question with two choices: agree or disagree and not much else (True or False).
      All your remarks, suggestions and constructive criticism are welcome.
      JPD_Simply.au3
      JPD_Simply_Demo.au3
    • By ohaya
      Hi,
      I am trying to use ConsoleWrite() to output some debug to stdout/console on a Windows command (cmd) window, but it seems like nothing is being output.
      I noted that the help says:
      but what does that mean (the "compiled as a console application" part?
      I have also tried adding
      to the beginning of my .au3 and re-compiling, but when I run the .exe in a command window, nothing is being output?
      How do I get the output from ConsoleWrite() to appear?
      Thanks,
      Jim
    • By nacerbaaziz
      Hello, dear.
      I am a newbie in autoit language
      I don't master any other programming language.
      I also don't understand how to manage the DLL files
      In addition to all this I am blind, and as we all know the explanations provided for the blind in programming are very few.
      So I'm trying to rely on myself for learning.
      I work hard to design some simple software and tools that make it easier to use computers, as they perform some tasks that may be difficult for the blind.
      I am also looking for the programs that the blind need and cannot use it because it's haven't the compatibility with the screen reader, and i try to design a simple programs that do the same work.
      In these tools and programs I have to deal directly with screen readers, when i must to forcing these programs to read some processes and tasks that it cannot be read in normal mode.
      To force these programs to read, I have to deal with the API files.
      And as I said, I'm very weak in managing DLLs.
      So I hope you'll try to help me, please.
      I did a lot of research until I found the API file to manage the free screen reader (NVDA).
      It is open source
      It is based on CPP language and Python
      And I don't understand the both languages.
      For this I decided to put you an API file link here which is attached with it examples, source and DLL file
      I hope you can help me by convert these functions to AutoIt include file
      I offer you a very serious apology if the participation is a violation of the laws.
      I wish I could find here someone to help me.
      To download the file please click on this link
      http://www.nvda-project.org/nvdaControllerClient/nvdaControllerClient_20100219.7z
      and To download the free screen reader,  please following this link
      https://www.nvaccess.org/download/

      I repeat my apologies and thanks in advance.
       
    • By boomingranny
      Use this UDF to add a console gui to your script (with log file):
      It uses the Hidden Autoit window (that you probably didn't even know existed)
      Closing Console window will terminate script.
      example of console:

      #include-once #include <GuiEdit.au3> EnableConsoleGui("example.log") ;example: ;------------------------ ConsoleWrite ("Hello World") For $i = 1 To 10 ConsoleWrite (".") Sleep(200) Next ConsoleWrite ("done") ConsoleWrite(@CRLF) ConsoleWrite ("close me to exit"&@CRLF) While 1 Sleep(1000) WEnd ;------------------------ ;end of example code Func EnableConsoleGui($Logfile="") ;EnableConsoleGUI ;by Daniel Barnes 20/04/2018 ;Uses AutoIt's Hidden window as a console (output only) Global $pidChild ;if we don't have a parent (as the parent window would have our script name) If Not WinExists(StringTrimRight(@ScriptName,4)) Then Opt("TrayIconHide",1) ;get Autoit's hidden window handle local $hWnd = WinGetHandle(AutoItWinGetTitle()) ;move the autoit hidden window to the middle of the screen WinMove($hWnd, "", (@DesktopWidth / 2) - 250, (@DesktopHeight / 2) - 250, 500, 500) ;get the Handle of the edit box in Autoit's hidden window $hEditBox = ControlGetHandle($hWnd,"","[CLASS:Edit; INSTANCE:1]") ;show it WinSetState($hWnd, "", @SW_SHOW) ;set its title = our script name WinSetTitle($hWnd,"",StringTrimRight(@ScriptName,4)) ;Spawn a child "copy" of the script, enabling reading of its console... If @Compiled Then ;2 = $STDOUT_CHILD. This avoids requiring the AutoItConstants.au3 in this sample code $pidChild= Run( FileGetShortName(@ScriptFullPath),@ScriptDir,"",2) Else ;2 = $STDOUT_CHILD. This avoids requiring the AutoItConstants.au3 in this sample code $pidChild= Run( FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath),@ScriptDir,"",2) EndIf OnAutoItExitRegister("EnableConsoleGui_CloseChildPID") ;read the console, while the child window exists (and the console window is visible) While ProcessExists($pidChild) $ConsoleRead = StdoutRead($pidChild) If $ConsoleRead then $text = StringLeft(ControlGetText($hWnd,"",$hEditBox),65535) If $Logfile Then FileWrite($Logfile,$ConsoleRead) $text &= $ConsoleRead ControlSetText($hWnd,"",$hEditBox,$text) ConsoleWrite($ConsoleRead) ;scroll to bottom of console edit window _GUICtrlEdit_SetSel($hEditBox, 65535, 65535) endif Sleep(250) WEnd exit endif EndFunc Func EnableConsoleGui_CloseChildPID() ;if this func isn't used ;when you close the console gui ;the child "clone" of your script will keep running ProcessClose($pidChild) EndFunc
×
×
  • Create New...