29#define OOLOG_POISON_NSLOG 0
36#include <SDL3/SDL_stdinc.h>
48#define SET_CRASH_REPORTER_INFO 1
74static void OONSLogPrintfHandler(NSString* message);
77#error Unknown platform!
82#define kFlushInterval 2.0
92- (void)asyncLogMessage:(NSString*)message;
116#if SET_CRASH_REPORTER_INFO
124 sWriteToStderr = [[NSUserDefaults standardUserDefaults] boolForKey:@"logging-echo-to-stderr"];
135 OOLog(
@"logging.nsLogFilter.install.failed",
@"Failed to install NSLog() filter; system messages will not be logged in log file.");
137#elif GNUSTEP_BASE_LIBRARY
138 NSRecursiveLock* lock = GSLogLock();
140 _NSLog_printf_handler = OONSLogPrintfHandler;
161#elif GNUSTEP_BASE_LIBRARY
162 NSRecursiveLock* lock = GSLogLock();
164 _NSLog_printf_handler = NULL;
185#if SET_CRASH_REPORTER_INFO
189 if (doCStringStuff) {
190 const char* cStr = [[string stringByAppendingString:@"\n"] UTF8String];
196#if SET_CRASH_REPORTER_INFO
205 return [OOLogHandlerGetLogBasePath() stringByAppendingPathComponent:sLogFileName];
226 NSString* logPath =
nil;
227 NSString* oldPath =
nil;
228 NSFileManager* fmgr =
nil;
239 if ([fmgr fileExistsAtPath:logPath]) {
240 oldPath = [OOLogHandlerGetLogBasePath() stringByAppendingPathComponent:@"Previous.log"];
241 [
fmgr oo_removeItemAtPath:oldPath];
242 if (![fmgr oo_moveItemAtPath:logPath toPath:oldPath]) {
243 if (![fmgr oo_removeItemAtPath:logPath]) {
244 NSLog(
@"Log setup: could not move or delete existing log at %@, will log to stdout instead.", logPath);
273 NSString* logPath =
nil;
274 NSFileManager* fmgr =
nil;
289 [threadStateMonitor setName:@"OOLogOutputHandler.stateMonitor"];
295 [
NSThread detachNewThreadSelector:@selector(loggerThread) toTarget:
self withObject:nil];
302 [threadStateMonitor release];
306 [threadStateMonitor unlockWithCondition:kConditionWorking];
311 OK = (logPath !=
nil);
316 OK = [
fmgr createFileAtPath:logPath contents:nil attributes:nil];
322 NSLog(
@"Log setup: could not open log at %@, will log to stdout instead.", logPath);
332 NSString* postamble =
nil;
336 postamble = [
NSString stringWithFormat:@"\nClosing log at %@.", [
NSDate date]];
339 [threadStateMonitor lockWhenCondition:kConditionReadyToDealloc];
340 [threadStateMonitor unlock];
353- (void)asyncLogMessage:(NSString*)message
359 if (message !=
nil) {
360 message = [
message stringByAppendingString:@"\n"];
364 NSArray* messageComponents = [
message componentsSeparatedByString:@"\n"];
372 flushTimer = [
NSTimer scheduledTimerWithTimeInterval:kFlushInterval target:
self selector:@selector(flushLog) userInfo:nil repeats:NO];
386 NSAutoreleasePool *rootPool =
nil, *pool =
nil;
390 [
NSThread ooSetCurrentThreadName:@"loggerThread"];
394 [threadStateMonitor lock];
395 [threadStateMonitor unlockWithCondition:kConditionWorking];
403 if (!
sSaturated && [message isKindOfClass:[NSData
class]]) {
409 message =
@"\r\n\r\n\r\n***** LOG TRUNCATED DUE TO EXCESSIVE LENGTH *****\r\n";
411 message =
@"\n\n\n***** LOG TRUNCATED DUE TO EXCESSIVE LENGTH *****\n";
413 message = [
message dataUsingEncoding:NSUTF8StringEncoding];
416 [logFile writeData:message];
417 }
else if ([message isEqual:
@"flush"]) {
418 [logFile synchronizeFile];
419 }
else if ([message isEqual:
@"die"]) {
426 @catch (NSException* exception) {
432 [threadStateMonitor lock];
433 [threadStateMonitor unlockWithCondition:kConditionReadyToDealloc];
465 CFBundleRef foundationBundle = NULL;
469 foundationBundle = CFBundleGetBundleWithIdentifier(CFSTR(
"com.apple.Foundation"));
470 if (foundationBundle != NULL) {
471 getter = CFBundleGetFunctionPointerForName(foundationBundle, CFSTR(
"_NSLogCStringFunction"));
472 setter = CFBundleGetFunctionPointerForName(foundationBundle, CFSTR(
"_NSSetLogCStringFunction"));
474 if (getter != NULL && setter != NULL) {
490static void OONSLogPrintfHandler(NSString* message)
501 BOOL exists, directory;
502 NSFileManager* fmgr = [NSFileManager defaultManager];
504 exists = [fmgr fileExistsAtPath:path isDirectory:&directory];
506 if (exists && !directory) {
507 NSLog(
@"Log setup: expected %@ to be a folder, but it is a file.", path);
511 if (![fmgr oo_createDirectoryAtPath:path attributes:
nil]) {
512 NSLog(
@"Log setup: could not create folder %@.", path);
523 OSStatus (*CSBackupSetItemExcluded)(NSURL* item, Boolean exclude, Boolean excludeByPath) = NULL;
524 CFBundleRef carbonCoreBundle = CFBundleGetBundleWithIdentifier(CFSTR(
"com.apple.CoreServices.CarbonCore"));
525 if (carbonCoreBundle) {
526 CSBackupSetItemExcluded = CFBundleGetFunctionPointerForName(carbonCoreBundle, CFSTR(
"CSBackupSetItemExcluded"));
527 if (CSBackupSetItemExcluded != NULL) {
528 (void)CSBackupSetItemExcluded([NSURL fileURLWithPath:path], YES, NO);
535 static NSString* appName =
nil;
536 NSBundle* bundle =
nil;
538 if (appName ==
nil) {
539 bundle = [NSBundle mainBundle];
540 appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"];
542 appName = [bundle bundleIdentifier];
544 appName =
@"<unknown application>";
554 static NSString* basePath =
nil;
556 if (basePath ==
nil) {
557 const char* logdirEnv = SDL_getenv(
"OO_LOGSDIR");
560 basePath = [NSString stringWithUTF8String:logdirEnv];
564 basePath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
567 basePath = NSHomeDirectory();
570 basePath = [basePath stringByAppendingPathComponent:@".Oolite"];
575 basePath = NSHomeDirectory();
579 basePath = [basePath stringByAppendingPathComponent:@"Logs"];
585 basePath = [basePath stringByAppendingPathComponent:GetAppName()];
599#if SET_CRASH_REPORTER_INFO
621 char *copy = NULL, *old = NULL;
638 [sCrashReporterInfoLock lock];
642 [sCrashReporterInfoLock unlock];
void OOLogOutputHandlerClose(void)
static void SetCrashReporterInfo(const char *info)
static LogCStringFunctionSetterProc _NSSetLogCStringFunction
@ kConditionReadyToDealloc
void OOLogOutputHandlerPrint(NSString *string)
static NSString * sLogFileName
static NSString * GetAppName(void)
static char * sOldCrashReporterInfo
void OOLogOutputHandlerStartLoggingToStdout()
NSString * OOLogHandlerGetLogPath(void)
void OOLogOutputHandlerClose(void)
static NSLock * sCrashReporterInfoLock
void OOLogOutputHandlerChangeLogFile(NSString *newLogName)
static LogCStringFunctionGetterProc _NSLogCStringFunction
static LogCStringFunctionProc sDefaultLogCStringFunction
static OOAsyncLogger * sLogger
LogCStringFunctionProc(* LogCStringFunctionGetterProc)(void)
NSString * OOLogHandlerGetLogBasePath(void)
static char ** sCrashReporterInfo
void OOLogOutputHandlerInit(void)
static BOOL sCrashReporterInfoAvailable
static void InitCrashReporterInfo(void)
static BOOL sWriteToStderr
void(* LogCStringFunctionSetterProc)(LogCStringFunctionProc)
static void LoadLogCStringFunctions(void)
static BOOL DirectoryExistCreatingIfNecessary(NSString *path)
static BOOL sWriteToStdout
static void ExcludeFromTimeMachine(NSString *path)
void(* LogCStringFunctionProc)(const char *string, unsigned length, BOOL withSyslogBanner)
void OOLogOutputHandlerStopLoggingToStdout()
static void OONSLogCStringFunction(const char *string, unsigned length, BOOL withSyslogBanner)
#define NSLog(format,...)
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
#define OOLog(class, format,...)
void void OOLogWithFunctionFileAndLine(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inFormat,...) OO_TAKES_FORMAT_STRING(5
OOAsyncQueue * messageQueue
void asyncLogMessage:(NSString *message)
NSConditionLock * threadStateMonitor