[futurebasic] Re: [FB] Implementing Log file in FB

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2011 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Wed, 6 Apr 2011 15:31:08 +1200
Brian S wrote:

>> _pathToLogFile$ = "/Users/rdp/Library/Logs/foo.log" // **change to suit**
>> // special log file in ~/Library/Logs
>> path = _pathToLogFile$
>> fn FBPStr2CStr( path )
>> fd = fn open( @path, O_WRONLY || O_APPEND || O_CREAT )
> 
> I’m wondering if there is a way to avoid a hard-coded path ( in standard file manipulation with constants such as _kUserDomain etc. but possible there is another method ). Clearly, the BSD open takes a pointer to an array of char ( essentially a pointer to a C string ) to return the file descriptor. 

/*
Open ~/Library/Logs/fileName for output/append.
File is created if necessary.
Returns a file descriptor, or negative value if error
*/
local fn OpenLogFile( fileName as Str255 )
'~'1
dim as FSRef   userLibraryRef
dim as Str255  path
dim as long    fd : fd = -2

if ( fn FSFindFolder( _kUserDomain, _kDomainLibraryFolderType, _false, @userLibraryRef ) ) then exit fn
if ( fn FSRefMakePath( @userLibraryRef, @path[0], 255 ) ) then exit fn
fn FBCStr2PStr( @path )
path += "/Logs/" + fileName
fn FBPStr2CStr( @path ) 
fd = fn open( @path, O_WRONLY || O_APPEND || O_CREAT, &O744 )
end fn = fd


// demo
dim as long  fd
fd = fn OpenLogFile( "foo.log" )
if ( fd < 0 ) then stop "OpenLogFile error " + str$( fd )


Robert P.