Oolite 1.91.0.7645-241119-222d325
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 <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 86 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 50 of file OOLogOutputHandler.m.

Typedef Documentation

◆ LogCStringFunctionGetterProc

typedef LogCStringFunctionProc(* LogCStringFunctionGetterProc) (void)

Definition at line 62 of file OOLogOutputHandler.m.

◆ LogCStringFunctionProc

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

Definition at line 61 of file OOLogOutputHandler.m.

◆ LogCStringFunctionSetterProc

typedef void(* LogCStringFunctionSetterProc) (LogCStringFunctionProc)

Definition at line 63 of file OOLogOutputHandler.m.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kConditionReadyToDealloc 
kConditionWorking 

Definition at line 236 of file OOLogOutputHandler.m.

237{
240};
@ kConditionReadyToDealloc
@ kConditionWorking

Function Documentation

◆ DirectoryExistCreatingIfNecessary()

static BOOL DirectoryExistCreatingIfNecessary ( NSString * path)
static

Definition at line 549 of file OOLogOutputHandler.m.

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

static void ExcludeFromTimeMachine ( NSString * path)
static

Definition at line 604 of file OOLogOutputHandler.m.

605{
606 OSStatus (*CSBackupSetItemExcluded)(NSURL *item, Boolean exclude, Boolean excludeByPath) = NULL;
607 CFBundleRef carbonCoreBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.CoreServices.CarbonCore"));
608 if (carbonCoreBundle)
609 {
610 CSBackupSetItemExcluded = CFBundleGetFunctionPointerForName(carbonCoreBundle, CFSTR("CSBackupSetItemExcluded"));
611 if (CSBackupSetItemExcluded != NULL)
612 {
613 (void)CSBackupSetItemExcluded([NSURL fileURLWithPath:path], YES, NO);
614 }
615 }
616}

Referenced by OOLogHandlerGetLogBasePath().

+ Here is the caller graph for this function:

◆ GetAppName()

static NSString * GetAppName ( void )
static

Definition at line 619 of file OOLogOutputHandler.m.

620{
621 static NSString *appName = nil;
622 NSBundle *bundle = nil;
623
624 if (appName == nil)
625 {
626 bundle = [NSBundle mainBundle];
627 appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"];
628 if (appName == nil) appName = [bundle bundleIdentifier];
629 if (appName == nil) appName = @"<unknown application>";
630 [appName retain];
631 }
632
633 return appName;
634}

References nil.

◆ InitCrashReporterInfo()

static void InitCrashReporterInfo ( void )
static

Definition at line 692 of file OOLogOutputHandler.m.

693{
694 sCrashReporterInfo = dlsym(RTLD_DEFAULT, "__crashreporter_info__");
695 if (sCrashReporterInfo != NULL)
696 {
697 sCrashReporterInfoLock = [[NSLock alloc] init];
699 {
701 }
702 else
703 {
704 sCrashReporterInfo = NULL;
705 }
706 }
707}
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()

static void LoadLogCStringFunctions ( void )
static

Definition at line 507 of file OOLogOutputHandler.m.

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

580{
581 static NSString *basePath = nil;
582
583 if (basePath == nil)
584 {
585 // ~/Library
586 basePath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
587
588 // ~/Library/Logs
589 basePath = [basePath stringByAppendingPathComponent:@"Logs"];
590 if (!DirectoryExistCreatingIfNecessary(basePath)) return nil;
591
592 // ~/Library/Logs/Oolite
593 basePath = [basePath stringByAppendingPathComponent:GetAppName()];
594 if (!DirectoryExistCreatingIfNecessary(basePath)) return nil;
595 ExcludeFromTimeMachine(basePath);
596
597 [basePath retain];
598 }
599
600 return basePath;
601}
static NSString * GetAppName(void)
static BOOL DirectoryExistCreatingIfNecessary(NSString *path)
static void ExcludeFromTimeMachine(NSString *path)

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

+ Here is the call graph for this function:

◆ OOLogHandlerGetLogPath()

NSString * OOLogHandlerGetLogPath ( void )

Definition at line 220 of file OOLogOutputHandler.m.

221{
222 return [OOLogHandlerGetLogBasePath() stringByAppendingPathComponent:sLogFileName];
223}
static NSString * sLogFileName
NSString * OOLogHandlerGetLogBasePath(void)

Referenced by OOOXPVerifier(OOPrivate)::buildDependencyGraph.

+ Here is the caller graph for this function:

◆ OOLogOutputHandlerChangeLogFile()

void OOLogOutputHandlerChangeLogFile ( NSString * newLogName)

Definition at line 226 of file OOLogOutputHandler.m.

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

162{
163 if (sInited)
164 {
165 sWriteToStderr = YES;
166 sInited = NO;
167
168 [sLogger endLogging];
170
171#if OOLITE_MAC_OS_X
173 {
176 }
177#elif GNUSTEP
178 NSRecursiveLock *lock = GSLogLock();
179 [lock lock];
180 _NSLog_printf_handler = NULL;
181 [lock unlock];
182#endif
183 }
184}
#define DESTROY(x)
Definition OOCocoa.h:77
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 119 of file OOLogOutputHandler.m.

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

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

187{
188 sWriteToStdout = true;
189}

References sWriteToStdout.

◆ OOLogOutputHandlerStopLoggingToStdout()

void OOLogOutputHandlerStopLoggingToStdout ( void )

Definition at line 190 of file OOLogOutputHandler.m.

191{
192 sWriteToStdout = false;
193}

References sWriteToStdout.

◆ OONSLogCStringFunction()

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

Definition at line 528 of file OOLogOutputHandler.m.

529{
530 if (OOLogWillDisplayMessagesInClass(@"system"))
531 {
532 OOLogWithFunctionFileAndLine(@"system", NULL, NULL, 0, @"%s", string);
533 }
534}
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()

static void SetCrashReporterInfo ( const char * info)
static

Definition at line 709 of file OOLogOutputHandler.m.

710{
711 char *copy = NULL, *old = NULL;
712
713 /* Don't do anything if setup failed or the string is NULL or empty.
714 (The NULL and empty checks may not be desirable in other uses.)
715 */
716 if (!sCrashReporterInfoAvailable || info == NULL || *info == '\0') return;
717
718 // Copy the string, which we assume to be dynamic...
719 copy = strdup(info);
720 if (copy == NULL) return;
721
722 /* ...and swap it in.
723 Note that we keep a separate pointer to the old value, in case
724 something else overwrites __crashreporter_info__.
725 */
726 [sCrashReporterInfoLock lock];
727 *sCrashReporterInfo = copy;
730 [sCrashReporterInfoLock unlock];
731
732 // Delete our old string.
733 if (old != NULL) free(old);
734}
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 65 of file OOLogOutputHandler.m.

Referenced by LoadLogCStringFunctions(), and OOLogOutputHandlerInit().

◆ _NSSetLogCStringFunction

LogCStringFunctionSetterProc _NSSetLogCStringFunction = NULL
static

◆ sCrashReporterInfo

char** sCrashReporterInfo = NULL
static

Definition at line 687 of file OOLogOutputHandler.m.

Referenced by InitCrashReporterInfo(), and SetCrashReporterInfo().

◆ sCrashReporterInfoAvailable

BOOL sCrashReporterInfoAvailable = NO
static

◆ sCrashReporterInfoLock

NSLock* sCrashReporterInfoLock = nil
static

Definition at line 689 of file OOLogOutputHandler.m.

Referenced by InitCrashReporterInfo().

◆ sDefaultLogCStringFunction

LogCStringFunctionProc sDefaultLogCStringFunction = NULL
static

Definition at line 73 of file OOLogOutputHandler.m.

Referenced by OOLogOutputHandlerClose(), and OOLogOutputHandlerInit().

◆ sInited

BOOL sInited = NO
static

◆ sLogFileName

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

Definition at line 116 of file OOLogOutputHandler.m.

Referenced by OOLogOutputHandlerChangeLogFile().

◆ sLogger

◆ sOldCrashReporterInfo

char* sOldCrashReporterInfo = NULL
static

Definition at line 688 of file OOLogOutputHandler.m.

Referenced by SetCrashReporterInfo().

◆ sSaturated

BOOL sSaturated = NO
static

Definition at line 114 of file OOLogOutputHandler.m.

◆ sWriteToStderr

BOOL sWriteToStderr = YES
static

◆ sWriteToStdout

BOOL sWriteToStdout = NO
static