Oolite 1.91.0.7745-260117-205bce7
Loading...
Searching...
No Matches
OOLogOutputHandler.m File Reference
import "OOLogOutputHandler.h"
import "OOLogging.h"
import "OOAsyncQueue.h"
#include <stdlib.h>
#include <stdio.h>
import "NSThreadOOExtensions.h"
import "NSFileManagerOOExtensions.h"
#include <SDL.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 OOAsyncLoggersLogger = 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 87 of file OOLogOutputHandler.m.

◆ OOLOG_POISON_NSLOG

#define OOLOG_POISON_NSLOG   0

Definition at line 30 of file OOLogOutputHandler.m.

◆ SET_CRASH_REPORTER_INFO

#define SET_CRASH_REPORTER_INFO   1

Definition at line 51 of file OOLogOutputHandler.m.

Typedef Documentation

◆ LogCStringFunctionGetterProc

typedef LogCStringFunctionProc(* LogCStringFunctionGetterProc) (void)

Definition at line 63 of file OOLogOutputHandler.m.

◆ LogCStringFunctionProc

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

Definition at line 62 of file OOLogOutputHandler.m.

◆ LogCStringFunctionSetterProc

typedef void(* LogCStringFunctionSetterProc) (LogCStringFunctionProc)

Definition at line 64 of file OOLogOutputHandler.m.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kConditionReadyToDealloc 
kConditionWorking 

Definition at line 237 of file OOLogOutputHandler.m.

238{
241};
@ kConditionReadyToDealloc
@ kConditionWorking

Function Documentation

◆ DirectoryExistCreatingIfNecessary()

BOOL DirectoryExistCreatingIfNecessary ( NSString * path)
static

Definition at line 550 of file OOLogOutputHandler.m.

551{
552 BOOL exists, directory;
553 NSFileManager *fmgr = [NSFileManager defaultManager];
554
555 exists = [fmgr fileExistsAtPath:path isDirectory:&directory];
556
557 if (exists && !directory)
558 {
559 NSLog(@"Log setup: expected %@ to be a folder, but it is a file.", path);
560 return NO;
561 }
562 if (!exists)
563 {
564 if (![fmgr oo_createDirectoryAtPath:path attributes:nil])
565 {
566 NSLog(@"Log setup: could not create folder %@.", path);
567 return NO;
568 }
569 }
570
571 return YES;
572}
#define NSLog(format,...)
Definition OOLogging.h:137
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 576 of file OOLogOutputHandler.m.

577{
578 OSStatus (*CSBackupSetItemExcluded)(NSURL *item, Boolean exclude, Boolean excludeByPath) = NULL;
579 CFBundleRef carbonCoreBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.CoreServices.CarbonCore"));
580 if (carbonCoreBundle)
581 {
582 CSBackupSetItemExcluded = CFBundleGetFunctionPointerForName(carbonCoreBundle, CFSTR("CSBackupSetItemExcluded"));
583 if (CSBackupSetItemExcluded != NULL)
584 {
585 (void)CSBackupSetItemExcluded([NSURL fileURLWithPath:path], YES, NO);
586 }
587 }
588}

Referenced by OOLogHandlerGetLogBasePath().

Here is the caller graph for this function:

◆ GetAppName()

NSString * GetAppName ( void )
static

Definition at line 590 of file OOLogOutputHandler.m.

591{
592 static NSString *appName = nil;
593 NSBundle *bundle = nil;
594
595 if (appName == nil)
596 {
597 bundle = [NSBundle mainBundle];
598 appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"];
599 if (appName == nil) appName = [bundle bundleIdentifier];
600 if (appName == nil) appName = @"<unknown application>";
601 [appName retain];
602 }
603
604 return appName;
605}

References nil.

◆ InitCrashReporterInfo()

void InitCrashReporterInfo ( void )
static

Definition at line 663 of file OOLogOutputHandler.m.

664{
665 sCrashReporterInfo = dlsym(RTLD_DEFAULT, "__crashreporter_info__");
666 if (sCrashReporterInfo != NULL)
667 {
668 sCrashReporterInfoLock = [[NSLock alloc] init];
670 {
672 }
673 else
674 {
675 sCrashReporterInfo = NULL;
676 }
677 }
678}
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 508 of file OOLogOutputHandler.m.

509{
510 CFBundleRef foundationBundle = NULL;
511 LogCStringFunctionGetterProc getter = NULL;
512 LogCStringFunctionSetterProc setter = NULL;
513
514 foundationBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Foundation"));
515 if (foundationBundle != NULL)
516 {
517 getter = CFBundleGetFunctionPointerForName(foundationBundle, CFSTR("_NSLogCStringFunction"));
518 setter = CFBundleGetFunctionPointerForName(foundationBundle, CFSTR("_NSSetLogCStringFunction"));
519
520 if (getter != NULL && setter != NULL)
521 {
522 _NSLogCStringFunction = getter;
524 }
525 }
526}
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 608 of file OOLogOutputHandler.m.

609{
610 static NSString *basePath = nil;
611
612 if (basePath == nil)
613 {
614 const char *logdirEnv = SDL_getenv("OO_LOGSDIR");
615
616 if (logdirEnv)
617 {
618 basePath = [NSString stringWithUTF8String:logdirEnv];
619 }
620 else
621 {
622#if OOLITE_MAC_OS_X
623 // ~/Library
624 basePath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
625#elif OOLITE_LINUX
626 // ~
627 basePath = NSHomeDirectory();
628
629 // ~/.Oolite
630 basePath = [basePath stringByAppendingPathComponent:@".Oolite"];
631 if (!DirectoryExistCreatingIfNecessary(basePath)) return nil;
632#elif OOLITE_WINDOWS
633 // <Install path>\Oolite
634 basePath = NSHomeDirectory();
635#endif
636
637 // .../Logs
638 basePath = [basePath stringByAppendingPathComponent:@"Logs"];
639 if (!DirectoryExistCreatingIfNecessary(basePath)) return nil;
640
641#if OOLITE_MAC_OS_X
642 // ~/Library/Logs/Oolite
643 basePath = [basePath stringByAppendingPathComponent:GetAppName()];
644 if (!DirectoryExistCreatingIfNecessary(basePath)) return nil;
645#endif
646 }
647#if OOLITE_MAC_OS_X
648 ExcludeFromTimeMachine(basePath);
649#endif
650 [basePath retain];
651 }
652
653 return basePath;
654}
static NSString * GetAppName(void)
static BOOL DirectoryExistCreatingIfNecessary(NSString *path)
static void ExcludeFromTimeMachine(NSString *path)

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

Referenced by ResourceManager::diagnosticFileLocation, OODockTilePlugIn::logFolderPath, 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 221 of file OOLogOutputHandler.m.

222{
223 return [OOLogHandlerGetLogBasePath() stringByAppendingPathComponent:sLogFileName];
224}
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 227 of file OOLogOutputHandler.m.

228{
229 if (![sLogFileName isEqual:newLogName])
230 {
231 sLogFileName = [newLogName copy];
232 [sLogger changeFile];
233 }
234}
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 162 of file OOLogOutputHandler.m.

163{
164 if (sInited)
165 {
166 sWriteToStderr = YES;
167 sInited = NO;
168
169 [sLogger endLogging];
171
172#if OOLITE_MAC_OS_X
174 {
177 }
178#elif GNUSTEP
179 NSRecursiveLock *lock = GSLogLock();
180 [lock lock];
181 _NSLog_printf_handler = NULL;
182 [lock unlock];
183#endif
184 }
185}
#define DESTROY(x)
Definition OOCocoa.h:85
static LogCStringFunctionProc sDefaultLogCStringFunction
static BOOL sWriteToStderr
static BOOL sInited

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 120 of file OOLogOutputHandler.m.

121{
122 if (sInited) return;
123
124#if SET_CRASH_REPORTER_INFO
126#endif
127
128 sLogger = [[OOAsyncLogger alloc] init];
129 sInited = YES;
130
131 if (sLogger != nil)
132 {
133 sWriteToStderr = [[NSUserDefaults standardUserDefaults] boolForKey:@"logging-echo-to-stderr"];
134 }
135 else
136 {
137 sWriteToStderr = YES;
138 }
139
140#if OOLITE_MAC_OS_X
142 if (_NSSetLogCStringFunction != NULL)
143 {
146 }
147 else
148 {
149 OOLog(@"logging.nsLogFilter.install.failed", @"Failed to install NSLog() filter; system messages will not be logged in log file.");
150 }
151#elif GNUSTEP
152 NSRecursiveLock *lock = GSLogLock();
153 [lock lock];
154 _NSLog_printf_handler = OONSLogPrintfHandler;
155 [lock unlock];
156#endif
157
159}
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:88

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 196 of file OOLogOutputHandler.m.

197{
198 if (sInited && sLogger != nil && !sWriteToStdout) [sLogger asyncLogMessage:string];
199
200 BOOL doCStringStuff = sWriteToStderr || sWriteToStdout;
201#if SET_CRASH_REPORTER_INFO
202 doCStringStuff = doCStringStuff || sCrashReporterInfoAvailable;
203#endif
204
205 if (doCStringStuff)
206 {
207 const char *cStr = [[string stringByAppendingString:@"\n"] UTF8String];
208 if (sWriteToStdout)
209 fputs(cStr, stdout);
210 else if (sWriteToStderr)
211 fputs(cStr, stderr);
212
213#if SET_CRASH_REPORTER_INFO
215#endif
216 }
217
218}
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 187 of file OOLogOutputHandler.m.

188{
189 sWriteToStdout = true;
190}

References sWriteToStdout.

◆ OOLogOutputHandlerStopLoggingToStdout()

void OOLogOutputHandlerStopLoggingToStdout ( void )

Definition at line 191 of file OOLogOutputHandler.m.

192{
193 sWriteToStdout = false;
194}

References sWriteToStdout.

◆ OONSLogCStringFunction()

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

Definition at line 529 of file OOLogOutputHandler.m.

530{
531 if (OOLogWillDisplayMessagesInClass(@"system"))
532 {
533 OOLogWithFunctionFileAndLine(@"system", NULL, NULL, 0, @"%s", string);
534 }
535}
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
Definition OOLogging.m:144
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 680 of file OOLogOutputHandler.m.

681{
682 char *copy = NULL, *old = NULL;
683
684 /* Don't do anything if setup failed or the string is NULL or empty.
685 (The NULL and empty checks may not be desirable in other uses.)
686 */
687 if (!sCrashReporterInfoAvailable || info == NULL || *info == '\0') return;
688
689 // Copy the string, which we assume to be dynamic...
690 copy = strdup(info);
691 if (copy == NULL) return;
692
693 /* ...and swap it in.
694 Note that we keep a separate pointer to the old value, in case
695 something else overwrites __crashreporter_info__.
696 */
697 [sCrashReporterInfoLock lock];
698 *sCrashReporterInfo = copy;
701 [sCrashReporterInfoLock unlock];
702
703 // Delete our old string.
704 if (old != NULL) free(old);
705}
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 66 of file OOLogOutputHandler.m.

Referenced by LoadLogCStringFunctions(), and OOLogOutputHandlerInit().

◆ _NSSetLogCStringFunction

LogCStringFunctionSetterProc _NSSetLogCStringFunction = NULL
static

◆ sCrashReporterInfo

char** sCrashReporterInfo = NULL
static

Definition at line 658 of file OOLogOutputHandler.m.

Referenced by InitCrashReporterInfo(), and SetCrashReporterInfo().

◆ sCrashReporterInfoAvailable

BOOL sCrashReporterInfoAvailable = NO
static

◆ sCrashReporterInfoLock

NSLock* sCrashReporterInfoLock = nil
static

Definition at line 660 of file OOLogOutputHandler.m.

Referenced by InitCrashReporterInfo().

◆ sDefaultLogCStringFunction

LogCStringFunctionProc sDefaultLogCStringFunction = NULL
static

Definition at line 74 of file OOLogOutputHandler.m.

Referenced by OOLogOutputHandlerClose(), and OOLogOutputHandlerInit().

◆ sInited

BOOL sInited = NO
static

Definition at line 112 of file OOLogOutputHandler.m.

◆ sLogFileName

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

Definition at line 117 of file OOLogOutputHandler.m.

Referenced by OOLogOutputHandlerChangeLogFile().

◆ sLogger

◆ sOldCrashReporterInfo

char* sOldCrashReporterInfo = NULL
static

Definition at line 659 of file OOLogOutputHandler.m.

Referenced by SetCrashReporterInfo().

◆ sSaturated

BOOL sSaturated = NO
static

◆ sWriteToStderr

BOOL sWriteToStderr = YES
static

◆ sWriteToStdout

BOOL sWriteToStdout = NO
static