From 2defc1af932c136d2c93bf02bc51e8f4515b780a Mon Sep 17 00:00:00 2001 From: Richard Frith-Macdonald Date: Tue, 22 Feb 2005 14:06:28 +0000 Subject: [PATCH] Minor enhancement to improve debugging git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@20775 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 9 ++++++++ Documentation/Base.gsdoc | 9 ++++++++ Source/GSPrivate.h | 1 + Source/NSDebug.m | 2 +- Source/NSLog.m | 46 ++++++++++++++++++++++++++++++++++------ Source/NSUserDefaults.m | 2 ++ 6 files changed, 61 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 82ff8cf28..507270806 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2005-02-22 14:00 Richard Frith-Macdonald + + * Documentation/Base.gsdoc: + * Source/GSPrivate.h: + * Source/NSDebug.m: + * Source/NSLog.m: + * Source/NSUserDefaults.m: + Add GSLogThread to include thread id in NSLog() and debug output. + 2005-02-22 11:12 Richard Frith-Macdonald * Source/GSFFCallInvocation.m: diff --git a/Documentation/Base.gsdoc b/Documentation/Base.gsdoc index 363cf4d58..3faf16575 100644 --- a/Documentation/Base.gsdoc +++ b/Documentation/Base.gsdoc @@ -104,6 +104,15 @@ some reason.

+ GSLogThread + +

+ Setting the user default GSLogThread to + YES will cause NSLog and debug output to + include the current thread in the logged message.
+ This is useful for debugging multi-threaded applications. +

+
GSMacOSXCompatible

diff --git a/Source/GSPrivate.h b/Source/GSPrivate.h index 73ce8cfc2..b8cfb9428 100644 --- a/Source/GSPrivate.h +++ b/Source/GSPrivate.h @@ -167,6 +167,7 @@ typedef enum { GSMacOSXCompatible, // General behavior flag. GSOldStyleGeometry, // Control geometry string output. GSLogSyslog, // Force logging to go to syslog. + GSLogThread, // Include thread ID in log message. NSWriteOldStylePropertyLists, // Control PList output. GSUserDefaultMaxFlag // End marker. } GSUserDefaultFlagType; diff --git a/Source/NSDebug.m b/Source/NSDebug.m index 906eb9d5a..28762c23d 100644 --- a/Source/NSDebug.m +++ b/Source/NSDebug.m @@ -621,7 +621,7 @@ _GSDebugAllocationListAll(void) void GSDebugAllocationRemove(Class c, id o) { - (*_GSDebugAllocationRemoveFunc)(c,o); + (*_GSDebugAllocationRemoveFunc)(c,o); } void diff --git a/Source/NSLog.m b/Source/NSLog.m index 526cac709..f369d20b3 100644 --- a/Source/NSLog.m +++ b/Source/NSLog.m @@ -35,6 +35,7 @@ #include "Foundation/NSLock.h" #include "Foundation/NSAutoreleasePool.h" #include "Foundation/NSData.h" +#include "Foundation/NSThread.h" #ifdef HAVE_SYSLOG_H #include @@ -46,6 +47,8 @@ #include "GSPrivate.h" +extern NSThread *GSCurrentThread(); + /** * A variable holding the file descriptor to which NSLogv() messages are * written by default. GNUstep initialises this to stderr.
@@ -211,6 +214,12 @@ NSLog (NSString* format, ...) * ensuring that a newline is present at the end of the message. *

*

+ * In GNUstep, the GSLogThread user default may be set to YES in + * order to instruct this function to include the internal ID of + * the mcurrent thread after the process ID. This can help you + * to track the behavior of a multi-threaded program. + *

+ *

* The resulting message is then passed to a handler function to * perform actual output. Locking is performed around the call to * the function actually writing the message out, to ensure that @@ -242,15 +251,38 @@ NSLogv (NSString* format, va_list args) #ifdef HAVE_SYSLOG if (GSUserDefaultsFlag(GSLogSyslog) == YES) - prefix = @""; + { + if (GSUserDefaultsFlag(GSLogThread) == YES) + { + prefix = [NSString stringWithFormat: @"[%08x] ", GSCurrentThread()]; + } + else + { + prefix = @""; + } + } else #endif - prefix = [NSString - stringWithFormat: @"%@ %@[%d] ", - [[NSCalendarDate calendarDate] - descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%F"], - [[NSProcessInfo processInfo] processName], - pid]; + { + if (GSUserDefaultsFlag(GSLogThread) == YES) + { + prefix = [NSString + stringWithFormat: @"%@ %@[%d,%08x] ", + [[NSCalendarDate calendarDate] + descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%F"], + [[NSProcessInfo processInfo] processName], + pid, GSCurrentThread()]; + } + else + { + prefix = [NSString + stringWithFormat: @"%@ %@[%d] ", + [[NSCalendarDate calendarDate] + descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%F"], + [[NSProcessInfo processInfo] processName], + pid]; + } + } /* Check if there is already a newline at the end of the format */ if (![format hasSuffix: @"\n"]) diff --git a/Source/NSUserDefaults.m b/Source/NSUserDefaults.m index 23e217a6e..4cf5ac032 100644 --- a/Source/NSUserDefaults.m +++ b/Source/NSUserDefaults.m @@ -121,6 +121,8 @@ static void updateCache(NSUserDefaults *self) = [self boolForKey: @"GSOldStyleGeometry"]; flags[GSLogSyslog] = [self boolForKey: @"GSLogSyslog"]; + flags[GSLogThread] + = [self boolForKey: @"GSLogThread"]; flags[NSWriteOldStylePropertyLists] = [self boolForKey: @"NSWriteOldStylePropertyLists"]; }