Jump to content

Redirect console debug to a file


Recommended Posts

Hi all,

I'm building a poker bot that will require some extensive debugging / logging. I find ConsoleWrite debugging very useful, however I will need to implement detailed logging so that the data can be collected while it runs and analyzed at a later stage. For this my ideal solution would be to somehow redirect console debugging into a .txt file.

I was unable to find (an easy?) way to do this - looked briefly at stdout and similar but these appear to deal with child processes only, not something that will fit the purpose as far as I can tell.

I also looked at "Add trace lines" option and, while it may seem like an overkill, it would do a job in the beginning until things stabilize. The trouble is redirecting that to a file.

On my wish list - utility I can run against any .AU3 script which will insert debug lines as appropriate at every function entry/exit, get all variable and array values into a debug file. I hope somone had a dream/need before me and a UDF exists to address this...

Many thanks!

Link to comment
Share on other sites

Look at ConsoleRead () and FileWriteLine

$a=ConsoleRead(1000,true)
$file=Fileopen("path txt file goes here",1);write without erasing the current content
FilewriteLine($file,$a)
Fileclose($file)

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

Look at ConsoleRead () and FileWriteLine

$a=ConsoleRead(1000,true)
$file=Fileopen("path txt file goes here",1);write without erasing the current content
FilewriteLine($file,$a)
Fileclose($file)
Thanks for info. There are couple of issues with this:

- I won't have any idea how much debug will be going into console in terms of characters or lines. The script will be running continuously and the amount of work or code executed depends on too many factors to know this in advance. Reading any predefined number of characters would probably be inefficient if not erroneous in some way. I can't have overlapping (logging same console text twice), and also can't miss anything.

- From AutoIT Help file: If at any time when this function is called (except for "peeking" as described below) there are no characters to be read from the stream, the ConsoleRead function will block (pause) and not return until there are characters to be read from the stream. This means that the AutoIt process will be halted, and there will be no processing of hotkeys, GUI messages, etc. until the parent process writes something to AutoIt's STDIN stream.

This means I would have to do "peek" method to avoid problems. I don't have any idea how to count or track what portion of console debug has been committed to log file.

Cheers

Link to comment
Share on other sites

Look at ConsoleRead () and FileWriteLine

$a=ConsoleRead(1000,true)
$file=Fileopen("path txt file goes here",1);write without erasing the current content
FilewriteLine($file,$a)
Fileclose($file)
That won't work. When using peeking mode (i.e. the second parameter of ConsoleRead) ConsoleRead returns the number of characters available in the string, NOT the actual characters. So it would need to be something like this, open a handle to your file somewhere else:

Func _InitMyApp
   ...
   $sMyDebugFile = "C:\poker_" & @HOUR & @MIN & @SEC & ".txt"
   $hFile=FileOpen($sMyDebugFile,10)
   ...
EndFunc
...
$a=ConsoleRead(default,true)
If $a <> 0 Then $FileWrite($hFile,ConsoleRead($a))
...
Func _ExitMyApp
   ...
   FileClose($hFile)
   ...
EndFunc
Link to comment
Share on other sites

You may or may not want to do this, but usually what I do for some of the programs that I write is create a function which will write to the console and also write to a log file as well. You can also turn this off by setting $debug to false, possibly turning it on using a command line arg.

Example:

$debug = True
$log = @ScriptDir & "\" & @MON & @MDAY & @YEAR & "_" & @HOUR & @MIN & @SEC & ".txt"

;code stuff goes here

;insert this line where you want to debug something
If $debug Then _DebugPrint("debug message goes here")


;debug helper function
Func _DebugPrint($sMsgString)
    _FileWriteLog($log, $sMsgString)
    ConsoleWrite(@MON & "/" & @MDAY & "/" @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & $sMsgString & @CRLF)
EndFunc

[font="Verdana"]People who say it cannot be done should not interrupt those who are doing it. - George Benard Shaw[/font]

Link to comment
Share on other sites

You may or may not want to do this, but usually what I do for some of the programs that I write is create a function which will write to the console and also write to a log file as well. You can also turn this off by setting $debug to false, possibly turning it on using a command line arg.

This is exactly how I did it in the current version. Every critical part has log of this kind, function that logs time/date/values. However I'm looking at a poker bot which will likely contain > 5000 lines of code and inserting "debug" line at every place I need it by hand is... the only way to go unless there is a better, automated way of doing it...

Link to comment
Share on other sites

This is exactly how I did it in the current version. Every critical part has log of this kind, function that logs time/date/values. However I'm looking at a poker bot which will likely contain > 5000 lines of code and inserting "debug" line at every place I need it by hand is... the only way to go unless there is a better, automated way of doing it...

Luckily for you there is an automated way using SciTE.

In SciTE, go to Otions -> Open Abbreviations File, this will open the file in the editor.

Look (or do a search) for "#endregion - ctrl+b" (without the double quotes). After that line in the file, I made a custom section for all of the custom abbreviations that I use when making a script. In your case, it can add the line where you want to debug.

Ex. -

(add the line below to the file)

dbg=If $debug Then _DebugPrint("|")

Be sure to keep the pipe character in between the double quotes, this marks where the cursor will be placed when the abbreviation is expanded. You can replace dbg with anything that you want, that is the abbreviation you will use so that when it is expanded, everything on the right side of the equals sign will show up and all you have to do is type in your message, or replace the double quotes with a variable. Save the changes to the file when done.

Next, to make it so that all you have to do it type dbg and then press the space bar to expand it, is go to Options -> Open au3.keywords.abbreviations.properties. After that opens in the editor, after the last entry, type in your abbreviation. In your case it would be dbg after the last entry, then save the file. This will make it so that all you have to do is type in dbg and then hit the space bar and the abbreviation will expand. That should save you some typing.

Hope this makes some sense, write back if you need any help.

Good luck! :)

[font="Verdana"]People who say it cannot be done should not interrupt those who are doing it. - George Benard Shaw[/font]

Link to comment
Share on other sites

Luckily for you there is an automated way using SciTE.

In SciTE, go to Otions -> Open Abbreviations File, this will open the file in the editor.

Look (or do a search) for "#endregion - ctrl+b" (without the double quotes). After that line in the file, I made a custom section for all of the custom abbreviations that I use when making a script. In your case, it can add the line where you want to debug.

Ex. -

(add the line below to the file)

dbg=If $debug Then _DebugPrint("|")

Be sure to keep the pipe character in between the double quotes, this marks where the cursor will be placed when the abbreviation is expanded. You can replace dbg with anything that you want, that is the abbreviation you will use so that when it is expanded, everything on the right side of the equals sign will show up and all you have to do is type in your message, or replace the double quotes with a variable. Save the changes to the file when done.

Next, to make it so that all you have to do it type dbg and then press the space bar to expand it, is go to Options -> Open au3.keywords.abbreviations.properties. After that opens in the editor, after the last entry, type in your abbreviation. In your case it would be dbg after the last entry, then save the file. This will make it so that all you have to do is type in dbg and then hit the space bar and the abbreviation will expand. That should save you some typing.

Hope this makes some sense, write back if you need any help.

Good luck! :)

That's also a possibility, it shortens the time needed to insert the debug lines - I still have to specify what I want to log, like variables, elements of arrays etc.

How about this - you know how you can double click a variable, then press Alt+D to create

"ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Variable = ' & $Variable & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console"

Can I customize Scite to have Alt+somekey which will instead of above line insert "If $Debug Then _DebugPrint(@ScriptLineNumber & ":" & $SelectedVariablehere) ?

This way it would be a lot easier, no need to enter variable name manually after Alt-keying your target?

Cheers

Link to comment
Share on other sites

That's also a possibility, it shortens the time needed to insert the debug lines - I still have to specify what I want to log, like variables, elements of arrays etc.

How about this - you know how you can double click a variable, then press Alt+D to create

"ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Variable = ' & $Variable & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console"

Can I customize Scite to have Alt+somekey which will instead of above line insert "If $Debug Then _DebugPrint(@ScriptLineNumber & ":" & $SelectedVariablehere) ?

This way it would be a lot easier, no need to enter variable name manually after Alt-keying your target?

Cheers

Anyone? This would go under customizing Scite I guess?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...