Flush file buffer immediately - write on HDD

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
ppf
Posts: 88
Joined: Oct 10, 2017 6:41

Flush file buffer immediately - write on HDD

Post by ppf »

Hi

using this code to closer crash point identification, by logging important values, actions, changes;
in case of unknown, relocated errors and crashes etc.Working on linux, sometimes I had only short part of error log
and finding the problem takes a time.
(That's not new, people had posted many version of their error catchers)
So I searched a way to force writing to disk immediately;
after reading manual etc.., seems this code snippet is flushing buffer onto HDD as needed.

Code: Select all

#Include "vbcompat.bi"  	
#include "crt/stdio.bi"		'fflush function
dim shared elf2 as integer: elf2=freefile		'aka Error Log File, must be global 
open file myErrorLogFileName for output as #elf2
'on various places in program...
write #elf2, debugInfoA,....,etc, etc			'with immediate flush
fflush(cast(FILE ptr, Cast( FILE Ptr, FileAttr( elf2, fbFileAttrHandle ))))    
Single thing I don't understand, when using includes from example in manual, is

Code: Select all

(#Include "vbcompat.bi"  	'or file.bi)
#include "crt.bi"
this leads to error "Unsupported platform"
but previously mentioned

Code: Select all

#include "crt/stdio.bi"

compiles Ok & works too Ok
ppf
Posts: 88
Joined: Oct 10, 2017 6:41

Re: Flush file buffer immediately - write on HDD

Post by ppf »

Enhanced flushing code to 'must-have' core routine. Enjoy ;)

To get nice readable source, flushing of record buffer can be simplified with macro

Code: Select all

'A version
'place this in program definitions, declarations, macros ...
#define flushRecordOnDisk(fileID) fflush(cast(FILE ptr, Cast( FILE Ptr, FileAttr( fileID, fbFileAttrHandle ))))     'immediately write record on disk
then

Code: Select all

'...
write #elf2, .......			'write trace & debug info into the errorLogFile
flushRecordOnDisk(elf2)			'flush it on HDD immediate !
'...
eventually even simpler - set elf2 as global constant and open your logFile together

Code: Select all

'B version
'place this in program definitions, declarations, macros ...
Dim Shared As Integer elf2=freefile
Open ...logfile ... as #elf2
#define flushRecordOnDisk() fflush(cast(FILE ptr, Cast( FILE Ptr, FileAttr( elf2, fbFileAttrHandle ))))     'immediately write record on disk


'...
write #elf2, .......			'write trace & debug info into the errorLogFile
flushRecordOnDisk()			 	'flush it on HDD immediate !
'...
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Flush file buffer immediately - write on HDD

Post by badidea »

Is the double cast to "file ptr" a typo or intentional?
cast(FILE ptr, Cast( FILE Ptr, ... ) )
ppf
Posts: 88
Joined: Oct 10, 2017 6:41

Re: Flush file buffer immediately - write on HDD

Post by ppf »

Hi, badidea

no typo, it is substitution of 3 lines into 1
Post Reply