Oolite
Loading...
Searching...
No Matches
OOLogOutputHandler.m File Reference
import "OOLogOutputHandler.h"
import "NSFileManagerOOExtensions.h"
import "NSThreadOOExtensions.h"
import "OOAsyncQueue.h"
import "OOLogging.h"
#include <SDL3/SDL_stdinc.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
Include dependency graph for OOLogOutputHandler.m:

Go to the source code of this file.

Classes

class  OOAsyncLogger

Macros

#define OOLOG_POISON_NSLOG   0
#define SET_CRASH_REPORTER_INFO   1
#define kFlushInterval   2.0

Typedefs

typedef void(* LogCStringFunctionProc) (const char *string, unsigned length, BOOL withSyslogBanner)
typedef LogCStringFunctionProc(* LogCStringFunctionGetterProc) (void)
typedef void(* LogCStringFunctionSetterProc) (LogCStringFunctionProc)

Enumerations

enum  { kConditionReadyToDealloc = 1 , kConditionWorking }

Functions

static void InitCrashReporterInfo (void)
static void SetCrashReporterInfo (const char *info)
static void LoadLogCStringFunctions (void)
static void OONSLogCStringFunction (const char *string, unsigned length, BOOL withSyslogBanner)
static NSString * GetAppName (void)
static BOOL DirectoryExistCreatingIfNecessary (NSString *path)
void OOLogOutputHandlerInit (void)
void OOLogOutputHandlerClose (void)
void OOLogOutputHandlerStartLoggingToStdout ()
void OOLogOutputHandlerStopLoggingToStdout ()
void OOLogOutputHandlerPrint (NSString *string)
NSString * OOLogHandlerGetLogPath (void)
void OOLogOutputHandlerChangeLogFile (NSString *newLogName)
static void ExcludeFromTimeMachine (NSString *path)
NSString * OOLogHandlerGetLogBasePath (void)

Variables

static BOOL sCrashReporterInfoAvailable = NO
static LogCStringFunctionGetterProc _NSLogCStringFunction = NULL
static LogCStringFunctionSetterProc _NSSetLogCStringFunction = NULL
static LogCStringFunctionProc sDefaultLogCStringFunction = NULL
static BOOL sInited = NO
static BOOL sWriteToStderr = YES
static BOOL sWriteToStdout = NO
static BOOL sSaturated = NO
static OOAsyncLogger * sLogger = nil
static NSString * sLogFileName = @"@"Latest.log"
static char ** sCrashReporterInfo = NULL
static char * sOldCrashReporterInfo = NULL
static NSLock * sCrashReporterInfoLock = nil

Macro Definition Documentation

◆ kFlushInterval

#define kFlushInterval   2.0

Definition at line 82 of file OOLogOutputHandler.m.

◆ OOLOG_POISON_NSLOG

#define OOLOG_POISON_NSLOG   0

Definition at line 29 of file OOLogOutputHandler.m.

◆ SET_CRASH_REPORTER_INFO

#define SET_CRASH_REPORTER_INFO   1

Definition at line 48 of file OOLogOutputHandler.m.

Typedef Documentation

◆ LogCStringFunctionGetterProc

typedef LogCStringFunctionProc(* LogCStringFunctionGetterProc) (void)

Definition at line 59 of file OOLogOutputHandler.m.

◆ LogCStringFunctionProc

typedef void(* LogCStringFunctionProc) (const char *string, unsigned length, BOOL withSyslogBanner)

Definition at line 58 of file OOLogOutputHandler.m.

◆ LogCStringFunctionSetterProc

typedef void(* LogCStringFunctionSetterProc) (LogCStringFunctionProc)

Definition at line 60 of file OOLogOutputHandler.m.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kConditionReadyToDealloc 
kConditionWorking 

Definition at line 216 of file OOLogOutputHandler.m.

216 {
219};
@ kConditionReadyToDealloc
@ kConditionWorking

Function Documentation

◆ DirectoryExistCreatingIfNecessary()

BOOL DirectoryExistCreatingIfNecessary ( NSString * path)
static

Definition at line 499 of file OOLogOutputHandler.m.

500{
501 BOOL exists, directory;
502 NSFileManager* fmgr = [NSFileManager defaultManager];
503
504 exists = [fmgr fileExistsAtPath:path isDirectory:&directory];
505
506 if (exists && !directory) {
507 NSLog(@"Log setup: expected %@ to be a folder, but it is a file.", path);
508 return NO;
509 }
510 if (!exists) {
511 if (![fmgr oo_createDirectoryAtPath:path attributes:nil]) {
512 NSLog(@"Log setup: could not create folder %@.", path);
513 return NO;
514 }
515 }
516
517 return YES;
518}
#define NSLog(format,...)
Definition OOLogging.h:148
return nil

References nil, and NSLog.

Referenced by OOLogHandlerGetLogBasePath().

Here is the caller graph for this function:

◆ ExcludeFromTimeMachine()

void ExcludeFromTimeMachine ( NSString * path)
static

Definition at line 521 of file OOLogOutputHandler.m.

522{
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);
529 }
530 }
531}

Referenced by OOLogHandlerGetLogBasePath().

Here is the caller graph for this function:

◆ GetAppName()

NSString * GetAppName ( void )
static

Definition at line 533 of file OOLogOutputHandler.m.

534{
535 static NSString* appName = nil;
536 NSBundle* bundle = nil;
537
538 if (appName == nil) {
539 bundle = [NSBundle mainBundle];
540 appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"];
541 if (appName == nil)
542 appName = [bundle bundleIdentifier];
543 if (appName == nil)
544 appName = @"<unknown application>";
545 [appName retain];
546 }
547
548 return appName;
549}

References nil.

◆ InitCrashReporterInfo()

void InitCrashReporterInfo ( void )
static

Definition at line 606 of file OOLogOutputHandler.m.

607{
608 sCrashReporterInfo = dlsym(RTLD_DEFAULT, "__crashreporter_info__");
609 if (sCrashReporterInfo != NULL) {
610 sCrashReporterInfoLock = [[NSLock alloc] init];
613 } else {
614 sCrashReporterInfo = NULL;
615 }
616 }
617}
static NSLock * sCrashReporterInfoLock
static char ** sCrashReporterInfo
static BOOL sCrashReporterInfoAvailable

References nil, sCrashReporterInfo, sCrashReporterInfoAvailable, and sCrashReporterInfoLock.

Referenced by OOLogOutputHandlerInit().

Here is the caller graph for this function:

◆ LoadLogCStringFunctions()

void LoadLogCStringFunctions ( void )
static

Definition at line 463 of file OOLogOutputHandler.m.

464{
465 CFBundleRef foundationBundle = NULL;
466 LogCStringFunctionGetterProc getter = NULL;
467 LogCStringFunctionSetterProc setter = NULL;
468
469 foundationBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Foundation"));
470 if (foundationBundle != NULL) {
471 getter = CFBundleGetFunctionPointerForName(foundationBundle, CFSTR("_NSLogCStringFunction"));
472 setter = CFBundleGetFunctionPointerForName(foundationBundle, CFSTR("_NSSetLogCStringFunction"));
473
474 if (getter != NULL && setter != NULL) {
475 _NSLogCStringFunction = getter;
477 }
478 }
479}
static LogCStringFunctionSetterProc _NSSetLogCStringFunction
static LogCStringFunctionGetterProc _NSLogCStringFunction
LogCStringFunctionProc(* LogCStringFunctionGetterProc)(void)
void(* LogCStringFunctionSetterProc)(LogCStringFunctionProc)

References _NSLogCStringFunction, and _NSSetLogCStringFunction.

Referenced by OOLogOutputHandlerInit().

Here is the caller graph for this function:

◆ OOLogHandlerGetLogBasePath()

NSString * OOLogHandlerGetLogBasePath ( void )

Definition at line 552 of file OOLogOutputHandler.m.

553{
554 static NSString* basePath = nil;
555
556 if (basePath == nil) {
557 const char* logdirEnv = SDL_getenv("OO_LOGSDIR");
558
559 if (logdirEnv) {
560 basePath = [NSString stringWithUTF8String:logdirEnv];
561 } else {
562#if OOLITE_MAC_OS_X
563 // ~/Library
564 basePath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
565#elif OOLITE_LINUX
566 // ~
567 basePath = NSHomeDirectory();
568
569 // ~/.Oolite
570 basePath = [basePath stringByAppendingPathComponent:@".Oolite"];
572 return nil;
573#elif OOLITE_WINDOWS
574 // <Install path>\Oolite
575 basePath = NSHomeDirectory();
576#endif
577
578 // .../Logs
579 basePath = [basePath stringByAppendingPathComponent:@"Logs"];
581 return nil;
582
583#if OOLITE_MAC_OS_X
584 // ~/Library/Logs/Oolite
585 basePath = [basePath stringByAppendingPathComponent:GetAppName()];
587 return nil;
588#endif
589 }
590#if OOLITE_MAC_OS_X
591 ExcludeFromTimeMachine(basePath);
592#endif
593 [basePath retain];
594 }
595
596 return basePath;
597}
static NSString * GetAppName(void)
static BOOL DirectoryExistCreatingIfNecessary(NSString *path)
static void ExcludeFromTimeMachine(NSString *path)

References DirectoryExistCreatingIfNecessary(), ExcludeFromTimeMachine(), and nil.

Referenced by ResourceManager::diagnosticFileLocation, and GameController::validateMenuItem:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOLogHandlerGetLogPath()

NSString * OOLogHandlerGetLogPath ( void )

Definition at line 203 of file OOLogOutputHandler.m.

204{
205 return [OOLogHandlerGetLogBasePath() stringByAppendingPathComponent:sLogFileName];
206}
static NSString * sLogFileName
NSString * OOLogHandlerGetLogBasePath(void)

Referenced by OOAsyncLogger::init, OpenLogFile(), and OOAsyncLogger::startLogging.

Here is the caller graph for this function:

◆ OOLogOutputHandlerChangeLogFile()

void OOLogOutputHandlerChangeLogFile ( NSString * newLogName)

Definition at line 208 of file OOLogOutputHandler.m.

209{
210 if (![sLogFileName isEqual:newLogName]) {
211 sLogFileName = [newLogName copy];
212 [sLogger changeFile];
213 }
214}
static OOAsyncLogger * sLogger

References OOAsyncLogger::changeFile, and sLogFileName.

Referenced by SwitchLogFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOLogOutputHandlerClose()

void OOLogOutputHandlerClose ( void )

Definition at line 147 of file OOLogOutputHandler.m.

148{
149 if (sInited) {
150 sWriteToStderr = YES;
151 sInited = NO;
152
153 [sLogger endLogging];
155
156#if OOLITE_MAC_OS_X
160 }
161#elif GNUSTEP_BASE_LIBRARY
162 NSRecursiveLock* lock = GSLogLock();
163 [lock lock];
164 _NSLog_printf_handler = NULL;
165 [lock unlock];
166#endif
167 }
168}
static BOOL sInited
Definition OOCPUInfo.m:56
#define DESTROY(x)
static LogCStringFunctionProc sDefaultLogCStringFunction
static BOOL sWriteToStderr

References _NSSetLogCStringFunction, DESTROY, OOAsyncLogger::endLogging, sDefaultLogCStringFunction, sInited, sLogger, and sWriteToStderr.

Referenced by OOLoggingTerminate(), and OOLogOutputHandlerInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOLogOutputHandlerInit()

void OOLogOutputHandlerInit ( void )

Definition at line 111 of file OOLogOutputHandler.m.

112{
113 if (sInited)
114 return;
115
116#if SET_CRASH_REPORTER_INFO
118#endif
119
120 sLogger = [[OOAsyncLogger alloc] init];
121 sInited = YES;
122
123 if (sLogger != nil) {
124 sWriteToStderr = [[NSUserDefaults standardUserDefaults] boolForKey:@"logging-echo-to-stderr"];
125 } else {
126 sWriteToStderr = YES;
127 }
128
129#if OOLITE_MAC_OS_X
131 if (_NSSetLogCStringFunction != NULL) {
134 } else {
135 OOLog(@"logging.nsLogFilter.install.failed", @"Failed to install NSLog() filter; system messages will not be logged in log file.");
136 }
137#elif GNUSTEP_BASE_LIBRARY
138 NSRecursiveLock* lock = GSLogLock();
139 [lock lock];
140 _NSLog_printf_handler = OONSLogPrintfHandler;
141 [lock unlock];
142#endif
143
145}
void OOLogOutputHandlerClose(void)
static void InitCrashReporterInfo(void)
static void LoadLogCStringFunctions(void)
static void OONSLogCStringFunction(const char *string, unsigned length, BOOL withSyslogBanner)
#define OOLog(class, format,...)
Definition OOLogging.h:84

References _NSLogCStringFunction, _NSSetLogCStringFunction, InitCrashReporterInfo(), LoadLogCStringFunctions(), nil, OOLog, OOLogOutputHandlerClose(), OONSLogCStringFunction(), sDefaultLogCStringFunction, sInited, sLogger, and sWriteToStderr.

Referenced by OOLoggingInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOLogOutputHandlerPrint()

void OOLogOutputHandlerPrint ( NSString * string)

Definition at line 179 of file OOLogOutputHandler.m.

180{
181 if (sInited && sLogger != nil && !sWriteToStdout)
182 [sLogger asyncLogMessage:string];
183
184 BOOL doCStringStuff = sWriteToStderr || sWriteToStdout;
185#if SET_CRASH_REPORTER_INFO
186 doCStringStuff = doCStringStuff || sCrashReporterInfoAvailable;
187#endif
188
189 if (doCStringStuff) {
190 const char* cStr = [[string stringByAppendingString:@"\n"] UTF8String];
191 if (sWriteToStdout)
192 fputs(cStr, stdout);
193 else if (sWriteToStderr)
194 fputs(cStr, stderr);
195
196#if SET_CRASH_REPORTER_INFO
199#endif
200 }
201}
static void SetCrashReporterInfo(const char *info)
static BOOL sWriteToStdout

References OOAsyncLogger::asyncLogMessage:, nil, sCrashReporterInfoAvailable, SetCrashReporterInfo(), sInited, sLogger, sWriteToStderr, and sWriteToStdout.

Referenced by OOLogInsertMarker(), OOLogInternal_(), and OOLogWithFunctionFileAndLineAndArguments().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OOLogOutputHandlerStartLoggingToStdout()

void OOLogOutputHandlerStartLoggingToStdout ( void )

Definition at line 170 of file OOLogOutputHandler.m.

171{
172 sWriteToStdout = true;
173}

References sWriteToStdout.

◆ OOLogOutputHandlerStopLoggingToStdout()

void OOLogOutputHandlerStopLoggingToStdout ( void )

Definition at line 174 of file OOLogOutputHandler.m.

175{
176 sWriteToStdout = false;
177}

References sWriteToStdout.

◆ OONSLogCStringFunction()

void OONSLogCStringFunction ( const char * string,
unsigned length,
BOOL withSyslogBanner )
static

Definition at line 481 of file OOLogOutputHandler.m.

482{
483 if (OOLogWillDisplayMessagesInClass(@"system")) {
484 OOLogWithFunctionFileAndLine(@"system", NULL, NULL, 0, @"%s", string);
485 }
486}
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
Definition OOLogging.m:136
void void OOLogWithFunctionFileAndLine(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inFormat,...) OO_TAKES_FORMAT_STRING(5

References OOLogWillDisplayMessagesInClass(), and OOLogWithFunctionFileAndLine().

Referenced by OOLogOutputHandlerInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetCrashReporterInfo()

void SetCrashReporterInfo ( const char * info)
static

Definition at line 619 of file OOLogOutputHandler.m.

620{
621 char *copy = NULL, *old = NULL;
622
623 /* Don't do anything if setup failed or the string is NULL or empty.
624 (The NULL and empty checks may not be desirable in other uses.)
625 */
626 if (!sCrashReporterInfoAvailable || info == NULL || *info == '\0')
627 return;
628
629 // Copy the string, which we assume to be dynamic...
630 copy = strdup(info);
631 if (copy == NULL)
632 return;
633
634 /* ...and swap it in.
635 Note that we keep a separate pointer to the old value, in case
636 something else overwrites __crashreporter_info__.
637 */
638 [sCrashReporterInfoLock lock];
639 *sCrashReporterInfo = copy;
642 [sCrashReporterInfoLock unlock];
643
644 // Delete our old string.
645 if (old != NULL)
646 free(old);
647}
static char * sOldCrashReporterInfo

References sCrashReporterInfo, sCrashReporterInfoAvailable, and sOldCrashReporterInfo.

Referenced by OOLogOutputHandlerPrint().

Here is the caller graph for this function:

Variable Documentation

◆ _NSLogCStringFunction

LogCStringFunctionGetterProc _NSLogCStringFunction = NULL
static

Definition at line 62 of file OOLogOutputHandler.m.

Referenced by LoadLogCStringFunctions(), and OOLogOutputHandlerInit().

◆ _NSSetLogCStringFunction

LogCStringFunctionSetterProc _NSSetLogCStringFunction = NULL
static

◆ sCrashReporterInfo

char** sCrashReporterInfo = NULL
static

Definition at line 601 of file OOLogOutputHandler.m.

Referenced by InitCrashReporterInfo(), and SetCrashReporterInfo().

◆ sCrashReporterInfoAvailable

BOOL sCrashReporterInfoAvailable = NO
static

◆ sCrashReporterInfoLock

NSLock* sCrashReporterInfoLock = nil
static

Definition at line 603 of file OOLogOutputHandler.m.

Referenced by InitCrashReporterInfo().

◆ sDefaultLogCStringFunction

LogCStringFunctionProc sDefaultLogCStringFunction = NULL
static

Definition at line 70 of file OOLogOutputHandler.m.

Referenced by OOLogOutputHandlerClose(), and OOLogOutputHandlerInit().

◆ sInited

BOOL sInited = NO
static

Definition at line 104 of file OOLogOutputHandler.m.

◆ sLogFileName

NSString* sLogFileName = @"@"Latest.log"
static

Definition at line 109 of file OOLogOutputHandler.m.

Referenced by OOLogOutputHandlerChangeLogFile().

◆ sLogger

◆ sOldCrashReporterInfo

char* sOldCrashReporterInfo = NULL
static

Definition at line 602 of file OOLogOutputHandler.m.

Referenced by SetCrashReporterInfo().

◆ sSaturated

BOOL sSaturated = NO
static

◆ sWriteToStderr

BOOL sWriteToStderr = YES
static

◆ sWriteToStdout

BOOL sWriteToStdout = NO
static