Logging To Text File


echoi

Recommended Posts

I display the status describing the scripts being run to the command window during run - this way I have a history of what happened in sequence. I would copy the command window and paste to a text file at the end of the run; however the command window keeps only so much I lose what happened at the initial stage.

Is there a way to export the command window to a text file?

Is there a way that I can write directly to a text file during run?

Is there a way to store this history text? -- I see that channels store values only.

Link to comment
Share on other sites

The best thing to do is to create a sequence that logs your debugging info rather than using ?. The sequence would be a function, and would use the File. functions to write to the log. Something like:


function DebugLog(string message)

try
private handle = file.open("c:\mylog.txt",0,1,1,1)
file.write(handle,message)
file.close(handle)
catch()
? strLastError
endcatch

[/CODE]

You can get a lot more sophisticated. You also may want to open the file once outside this sequence, putting the handle in a global, then leave the file open. You also probably should add some protection mechanism if you are going to call this from multiple threads. It all depends on how critical things are. The simplest protection is probably:

[CODE]
function DebugLog(string message)

while(1)
try
private handle = file.open("c:\mylog.txt",0,1,1,1)
file.write(handle,message)
file.close(handle)
break
catch()
? strLastError
delay(0.01)
endcatch
endwhile
[/CODE]

If the file can't be opened or written, it will pause and try again. Depending on the OS, the file.open() in write mode will likely lock any other thread from opening the file, causing it to throw an error.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.