Code archives/File Utilities/Save debugging messages to a debuglog file

This code has been declared by its author to be Public Domain code.

Download source code

Save debugging messages to a debuglog file by Zethrax2015
*** Blitz3D Code ***


------------------------------------------------------------------------------------------------------------------------
The version below actively saves the debuglog message data to a file.
This may slow down your program due to the file accesses (depending on file caching). Or it may not. Ideally the file save would be done in a thread but that option doesn't exist in vanilla Blitz3D code.
This version is useful if you want to save data as it's aquired - potentially before a program crash that may prevent stored data from not being explicitely saved.
------------------------------------------------------------------------------------------------------------------------


You can use the 'DebugMsg' function below to save debug messages to a debuglog file while your program is running.

An optional 'name' string and separator string can be added before the value.


== FUNCTIONS ==

DebugMsg( value$, name$ = "" )
* Writes the specified 'value$' to the debuglog file. If the optional 'name$' is also specified then it will precede the 'value$' with a separator string between the pair.
* If the debuglog file is not already open then a new file will be created automatically. If the file creation fails then the program will end with an error message.
* By default the debuglog file will be a relative file named "debuglog.txt", but you can change this using the 'SetDebuglogFilepath' function.
* By default the name-value separator will be a " = " string, but you can change this using the 'SetDebuglogSeparator' function.


SetDebuglogFilepath( filepath$ )
* Sets a different filename and relative or absolute filepath for the debuglog file.
* This function is optional. By default "debuglog.txt" is used for the filepath.


SetDebuglogSeparator( separator$ )
* Sets a different separator string to use between name and value pairs.
* This function is optional. By default " = " is used for the separator.


CloseDebuglog()
* If an open debuglog file exists then this function will close it.
* You can also use it to close an existing debuglog file and then start writing to a new one (the old one will be overwritten if you don't use 'SetDebuglogFilepath' to change the filepath).
* This function can be safely called even if no debuglog file exists.
* Note that all files are closed automatically when a Blitz program ends so this is somewhat redundant, but it's here if you need it.







------------------------------------------------------------------------------------------------------------------------
The version below stores the data in a type list and then saves it to a file when 'SaveDebuglog' is called.
This is the most efficient way to handle the data, but the downside to this is that the data won't be saved if a crash prevents 'SaveDebuglog' from being executed.
If you want to actively save the data then see the version above this one.
------------------------------------------------------------------------------------------------------------------------


You can use the 'DebugMsg' function below to save debug messages to a debuglog file while your program is running.

An optional 'name' string and separator string can be added before the value.

The data is temporarily stored in a type list. Use 'SaveDebuglog' at the end of your program to save the data to a file.

If there are no debug messages to save then nothing is saved and no files are created or updated.


== FUNCTIONS ==

DebugMsg( value$, name$ = "" )
* Writes the specified 'value$' to the debuglog list.
* If the optional 'name$' is also specified then it will precede the 'value$' in the saved file with a separator string between the pair.


ClearDebugList()
* Clears the list of debuglog messages.
* You can use this command to clear out the list to allow you to start a new list mid-program.


SaveDebuglog( filepath$ = "", separator$ = "", do_append = False )
* Saves the debuglog data to the specified 'filepath$'. Normally you'll want to add this at the end of your program.
* If there are no debug messages to save then nothing is saved and no files are created or updated.
* The 'filepath$' parameter is optional. If it is omitted or a blank string supplied then the file will be saved as a relative file named "debuglog.txt".
* A 'separator$' string can optionally be specified to separate the name and value pairs (assuming the 'name' string was supplied via 'DebugMsg').
* If the 'separator$' string is omitted or a blank string supplied then the default " = " separator string will be used if needed.
* If the 'do_append' flag is set to True (defaults to False) then the data will be appended to any existing file at the specified 'filepath$'. If no file exists at that filepath then a new file will be created.


The code can be found at: http://www.blitzbasic.com/codearcs/codearcs.php?code=3181

Comments

degac2015
Hi

technically a debuglog *should* write immediately any message (so default is append mode in your function), because in case of break-up you are not sure if your application can save the info before 'closing'...

I have implemented (for Bmax) my own Debullog function: each message I 'write' (PrintLog) is immediately saved on a file.


Matty2015
Yep. A debug log that writes nothing on program crash is not too useful. Having an append mode is pretty important.


Zethrax2015
Ok I've swapped the two versions so that the version that saves the debuglog messages directly to the debuglog file appears first. This version will effectively append the messages to the file as soon as they are received.

Since Blitz3D in debug mode closes all files on a soft crash, the file-system's file cache should get saved correctly in the event of a crash. That's my understanding anyway (I did some quick testing that seemed to confirm this).

Any hard crashes that happen outside the protection of the debugger are outside the scope of my little code-lib, of course.


Code Archives Forum