Game Development Community

A question of coding style

by Sherman Pendley · in Torque Game Builder · 04/23/2009 (12:48 pm) · 6 replies

I've patched TGB to store the console log in the correct place on Macs, in $HOME/Library/Logs/(app bundle identifier).log. It works fine, but I want to make sure it's put together in the preferred way, or whether it should be arranged differently.

In platform.h, I added a declaration for a new osGetConsoleLogPath() function:

extern StringTableEntry osGetConsoleLogPath();

Then, in console.cc, I modified the Con::init() method to call it:

logFileName                   = osGetConsoleLogPath();

I also replaced defLogFileName with logFileName in the Con::log() and Con::setLogMode() methods:

consoleLogFile.open(logFileName, FileStream::Write);

Finally, in implemented the osGetConsoleLogPath() function in macCocoaPlatform.mm:

StringTableEntry osGetConsoleLogPath()
{
    FSRef logDirFS;
    OSErr err;
    UInt8 logDirPath[PATH_MAX];

    // Get the path to the user's log folder
    err = FSFindFolder(kUserDomain, kLogsFolderType, kDontCreateFolder, &logDirFS);
    if (err != 0) return "console.log";

    err = FSRefMakePath(&logDirFS, logDirPath, PATH_MAX-1);
    if (err != 0) return "console.log";

    // Get the identifier of the main bundle and append it to the log dir
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
    NSString *logPath = [NSString stringWithUTF8String:(char*)logDirPath];
    NSString *logFilePath = [logPath stringByAppendingPathComponent:[bundleID stringByAppendingPathExtension:@"log"]];

    return [logFilePath fileSystemRepresentation];
}

As I said, this works. My question is one of style: Is there a better way to arrange this? Should the function be a method on Platform:: instead?

#1
05/14/2009 (6:11 am)
I can get it working like that (need to specify line numbers against specific versions of TGB for users to get it right straight away ;).

I think it belongs in the Platform class, yes.

You can get the current user's home directory with NSHomeDirectory(), then add Library/Logs to the string.

Apple recommend a more complicated solution using NSSearchPathForDirectoriesInDomains.

(FSFindFolder is Carbon, and Apple is deprecating more of it each day.)

Either method of getting to ~/Library is fine, but note that they also recommend making a directory for the application. This would probably be useful if the app makes datestamped logs.
#2
01/19/2010 (6:31 pm)
In case people are still interested in this, here's the version of the function that I'm using (with TGE 1.5)

This is probably horrible style, since it's the only piece of Objective-C I've strung together in my life, but it appears to work fine.

StringTableEntry dOSGetConsoleLogPath()  
{  
	NSString *LOG_DIRECTORY = @"FreeBuild";
    NSString *ald = nil;
	OSErr err = 0;
	 UInt8 logDirPath[PATH_MAX];
	
    if (ald == nil) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains
		(NSLibraryDirectory, NSUserDomainMask, YES);
        if ([paths count] == 1) {
            ald = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Logs"];
            ald = [ald stringByAppendingPathComponent:LOG_DIRECTORY];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            BOOL isDirectory = NO;
            if (![fileManager fileExistsAtPath:ald isDirectory:&isDirectory]) {
                if (![fileManager createDirectoryAtPath:ald attributes:nil]) {
                    ald = nil;
					err = 1;
                }
            }
            else {
                if (!isDirectory) {
                    ald = nil;
					err = 1;
                }
            }
        }
    } 
	
	if (ald == nil || err != 0){
		return "console.log";
	} else{
		NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];  
		ald = [ald stringByAppendingPathComponent:[bundleID stringByAppendingPathExtension:@"log"]];
		return [ald fileSystemRepresentation];  
	}
	
}
#3
01/20/2010 (2:55 pm)
You shoulda seen the Python I wrote today…

It seems like it should work. Maybe analyse it with Xcode's new tools/leak check it, just in case :)
#4
01/20/2010 (3:01 pm)
well, I'm using it, and it seems to run ok, but good idea. I actually haven't upgraded to Snow Leopard yet though, so I don't have the newest XCode.
#5
01/20/2010 (3:24 pm)
Heartily recommended. It catches some unbalanced object allocations, and shows me arrows along the code to give hints.
#6
01/20/2010 (4:39 pm)
A man could get spoiled on that :D