Jump to content

Can someone please explain to me how to log my program?


Recommended Posts

I am looking all around for how to log everything that my program does so I can see what was successful, what was not, what it did, what it returned, what went wrong, etc. I've tried some ways to log, but nothing at all worked anything like that. Same for an error function. It just sits in my program and does nothing.

But for now I am asking how do I make my program write its activity in a text file.

Link to comment
Share on other sites

I'd like the logging to always happen for every user and output it to a text file so I can pick it up and use to improve/fix.

If I add trace lines, it will only be visible in SciTE?

The Debugger, is an external program, right? Can't have the user using something like that..

Link to comment
Share on other sites

I'd like the logging to always happen for every user and output it to a text file so I can pick it up and use to improve/fix.

If I add trace lines, it will only be visible in SciTE?

The Debugger, is an external program, right? Can't have the user using something like that..

 

I'd like the logging to always happen for every user and output it to a text file so I can pick it up and use to improve/fix.

If I add trace lines, it will only be visible in SciTE?

The Debugger, is an external program, right? Can't have the user using something like that..

 

i think in this situation you must manually use msgbox , gui text , popup ...

Link to comment
Share on other sites

Am sure there are better was of doing this, but you can add the trace lines and use ConsoleRead

 

I have to do it without SciTE. Users won't have that.

Does the console happen even without SciTE, and then it can be sent to a txt file after all?

Link to comment
Share on other sites

Add tracelines and replace consolewrite in them with filewrite.

Probably not as simple as changing function name but you get the idea.

 

Thanks, that worked!

But can logging or tracing be done without making the code of the program look cluttered like a top-tier mess? Sure, I can add and remove them at will, but is that how it is properly done?

So if I want to keep adding the trace lines continually over a long period of time as people use my program I suppose I make it somehow display time and date besides the line..?

Tracing is one thing, but another is logging - how does a dude do that in a similar output fashion?

Link to comment
Share on other sites

#include <Date.au3>

$hFile = FileOpen(@ScriptDir & "\MyLog.txt", 9)

Local $iValue = 0
Local $iLogScript = 1

_Example()

Func _Example()
    $FunctionName = "_Example"
    For $i = 0 To 10
        $iValue = Random(1.3, 100)
        _MyLog($FunctionName, $iValue)
    Next
EndFunc   ;==>_Example

Func _MyLog($Function, $Data)
    If Not $iLogScript Then
        Return
    EndIf
    FileWrite($hFile, _Now() & " - " & $Function & " - " & $Data & @CRLF)
EndFunc   ;==>_MyLog

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

I have done something similar to J1 for a Helpdesk app I wrote for a hospital, and understand the concern regarding "clutter" in the code. Taking his suggestion a bit farther, you can create a UDF with your logging function(s) and then include it in your script. That way you call only the one line of your included function (e.g. _logfile_loginEvent($user, $success), or _logfile_modifyPropertiesEvent($user, $property, $success)).

This way, if you need to add/change/remove logging functions, you don't have to touch the main script's code. It can be done as something of an "incremental update" where you just deliver the updated logfile script to machines.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

This may not be the most ideal of solutions but in some of my scripting I will use the _FileWriteLog function. It will automatically pre-pend a date time stamp to the lines written to the file of your choice. You could probably further enhance its capabilities within a function similar to what @John has provided. 

From the AutoIt Help file:

; Open the logfile in write mode.

#include <File.au3>

Local $hFile = FileOpen(@ScriptDir & "\Example.log", 1)

_FileWriteLog($hFile, "Text 1") ; Write to the logfile passing the filehandle returned by FileOpen.
FileClose($hFile) ; Close the filehandle to release the file.

The Help file also has a second sample. My two cents. I'd like to know what you end up using...

Link to comment
Share on other sites

I've also added, in my own logging functions, the concept of loglevels.  I log everything, but only output to the log file those which are above the default log level.

That way, if you need to debug something, you can up the default to a high level...but when all is going fine, only log the important stuff.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
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...