NSLog updates for consistency with latest OSX

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38370 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2015-03-01 19:37:28 +00:00
parent 58087334b8
commit 86ada074dd
7 changed files with 184 additions and 39 deletions

View file

@ -1,3 +1,17 @@
2015-02-28 Richard Frith-Macdonald <rfm@gnu.org>
* configure.ac: Check for gettid availability
* configure: Regenerate
* Headers/GNUstepBase/config.h.in: Regenerate
* Source/GSPrivate.h: Update comment on GSLogThread
* Documentation/Base.gsdoc: Update doc of GSLogThread
* Source/NSLog.m: Change to log thread ID (to match OSX which now
logs the thread ID along with the process ID). Change the meaning
of the GSLogThread user default (which used to turn on logging of
the thread ID) so that it now turns on logging of the thread name
(where a thread name has been set) and the NSThread address if no
name has been set.
2015-02-16 Wolfgang Lux <wolfgang.lux@gmail.com>
* Source/NSData.m (writeToFile:options:error:): Fix leak of open

View file

@ -165,8 +165,8 @@ notice and this notice are preserved.
<p>
Setting the user default <code>GSLogThread</code> to
<code>YES</code> will cause NSLog and debug output to
include the current thread in the logged message.<br />
This is useful for debugging multi-threaded applications.
include the current thread name in the logged message.<br />
This may be useful for debugging multi-threaded applications.
</p>
</desc>
<term>GSMacOSXCompatible</term>

View file

@ -293,6 +293,9 @@
/* Define to 1 if you have the `getpwuid_r' function. */
#undef HAVE_GETPWUID_R
/* Define if you have gettid() */
#undef HAVE_GETTID
/* Define if you have this function */
#undef HAVE_GICONV
@ -455,6 +458,9 @@
/* Define to 1 if you have the <pthread.h> header file. */
#undef HAVE_PTHREAD_H
/* Define to 1 if you have the `pthread_set_name_np' function. */
#undef HAVE_PTHREAD_SET_NAME_NP
/* Define this if you work on sysv */
#undef HAVE_PTS_STREAM_MODULES
@ -775,7 +781,7 @@
/* Define as the link to exe of process in /proc filesystem. */
#undef PROCFS_EXE_LINK
/* Description: Define set name function for pthread */
/* Description: Define setname function for pthread with three args */
#undef PTHREAD_SETNAME
/* Define to 1 if the `setpgrp' function takes no argument. */

View file

@ -235,7 +235,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.
GSLogThread, // Include thread name in log message.
GSLogOffset, // Include time zone offset in message.
NSWriteOldStylePropertyLists, // Control PList output.
GSUserDefaultMaxFlag // End marker.

View file

@ -37,6 +37,33 @@
#import "Foundation/NSThread.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#if defined(HAVE_GETTID)
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#endif
/* Return the current thread ID as an unsigned long.
* Ideally, we use the operating-system's notion of a thread ID so
* that external process monitoring software will be using the same
* value that we log. If we don't know the system's mechanism, we
* use the address of the current NSThread object so that, even if
* it makes no sense externally, it can still be used to show that
* different threads generated different logs.
*/
static unsigned long
GSThreadID()
{
#if defined(__MINGW__)
return (unsigned long)GetCurrentThreadId();
#elsif defined(HAVE_GETTID)
return (unsigned long)syscall(SYS_gettid);
#else
return (unsigned long)GSCurrentThread();
#endif
}
// Some older BSD systems used a non-standard range of thread priorities.
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#elif HAVE_SYS_SLOG_H
@ -302,8 +329,8 @@ NSLog(NSString* format, ...)
* </p>
* <p>
* In GNUstep, the GSLogThread user default may be set to YES in
* order to instruct this function to include the internal ID of
* the current thread after the process ID. This can help you
* order to instruct this function to include the name (if any)
* of the current thread after the process ID. This can help you
* to track the behavior of a multi-threaded program.<br />
* Also the GSLogOffset user default may be set to YES in order
* to instruct this function to include the time zone offset in
@ -326,8 +353,9 @@ NSLog(NSString* format, ...)
void
NSLogv(NSString* format, va_list args)
{
NSString *prefix;
NSString *message;
NSMutableString *prefix;
NSString *message;
NSString *threadName = nil;
static int pid = 0;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
@ -345,23 +373,39 @@ NSLogv(NSString* format, va_list args)
#endif
}
if (GSPrivateDefaultsFlag(GSLogThread) == YES)
{
NSThread *t = GSCurrentThread();
threadName = [t name];
/* If no name has been set for the current thread, we log the address
* of the NSThread object instead.
*/
if ([threadName length] == 0)
{
threadName = [NSString stringWithFormat: @"%lu", (unsigned long)t];
}
}
prefix = [NSMutableString stringWithCapacity: 1000];
#ifdef HAVE_SYSLOG
if (GSPrivateDefaultsFlag(GSLogSyslog) == YES)
{
if (GSPrivateDefaultsFlag(GSLogThread) == YES)
{
prefix = [NSString stringWithFormat: @"[thread:%"PRIxPTR"] ",
(NSUInteger)GSCurrentThread()];
}
if (nil != threadName)
{
[prefix appendFormat: @"[thread:%lu,%@] ", GSThreadID(), threadName];
}
else
{
prefix = @"";
}
{
[prefix appendFormat: @"[thread:%lu] ", GSThreadID()];
}
}
else
#endif
{
NSString *fmt;
NSString *cal;
if (GSPrivateDefaultsFlag(GSLogOffset) == YES)
{
@ -371,33 +415,30 @@ NSLogv(NSString* format, va_list args)
{
fmt = @"%Y-%m-%d %H:%M:%S.%F";
}
cal = [[NSCalendarDate calendarDate] descriptionWithCalendarFormat: fmt];
if (GSPrivateDefaultsFlag(GSLogThread) == YES)
{
prefix = [NSString
stringWithFormat: @"%@ %@[%d,%"PRIxPTR"x] ",
[[NSCalendarDate calendarDate] descriptionWithCalendarFormat: fmt],
[[NSProcessInfo processInfo] processName],
pid, (NSUInteger)GSCurrentThread()];
}
[prefix appendString: cal];
[prefix appendString: @" "];
[prefix appendString: [[NSProcessInfo processInfo] processName]];
if (nil == threadName)
{
[prefix appendFormat: @"[%d:%lu] ",
pid, GSThreadID()];
}
else
{
prefix = [NSString
stringWithFormat: @"%@ %@[%d] ",
[[NSCalendarDate calendarDate] descriptionWithCalendarFormat: fmt],
[[NSProcessInfo processInfo] processName],
pid];
}
{
[prefix appendFormat: @"[%d:%lu,%@] ",
pid, GSThreadID(), threadName];
}
}
/* Check if there is already a newline at the end of the format */
if ([format hasSuffix: @"\n"] == NO)
message = [[NSString alloc] initWithFormat: format arguments: args];
[prefix appendString: message];
[message release];
if ([prefix hasSuffix: @"\n"] == NO)
{
format = [format stringByAppendingString: @"\n"];
[prefix appendString: @"\n"];
}
message = [NSString stringWithFormat: format arguments: args];
prefix = [prefix stringByAppendingString: message];
if (myLock == nil)
{

72
configure vendored
View file

@ -13711,7 +13711,6 @@ CFLAGS=$saved_CFLAGS
#--------------------------------------------------------------------
for ac_func in pthread_set_name_np
do
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
@ -13991,6 +13990,77 @@ _ACEOF
esac
fi
#--------------------------------------------------------------------
# Check whether we can get the system thread ID
#--------------------------------------------------------------------
{ $as_echo "$as_me:$LINENO: checking for gettid()" >&5
$as_echo_n "checking for gettid()... " >&6; }
if test "${ac_cv_gettid+set}" = set; then
$as_echo_n "(cached) " >&6
else
if test "$cross_compiling" = yes; then
ac_cv_gettid=no
else
cat >conftest.$ac_ext <<_ACEOF
/* confdefs.h. */
_ACEOF
cat confdefs.h >>conftest.$ac_ext
cat >>conftest.$ac_ext <<_ACEOF
/* end confdefs.h. */
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
int main(int argc, char **argv) {
pid_t t = syscall(SYS_gettid); return t == -1 ? 1 : 0; }
_ACEOF
rm -f conftest$ac_exeext
if { (ac_try="$ac_link"
case "(($ac_try" in
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
*) ac_try_echo=$ac_try;;
esac
eval ac_try_echo="\"\$as_me:$LINENO: $ac_try_echo\""
$as_echo "$ac_try_echo") >&5
(eval "$ac_link") 2>&5
ac_status=$?
$as_echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } && { ac_try='./conftest$ac_exeext'
{ (case "(($ac_try" in
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
*) ac_try_echo=$ac_try;;
esac
eval ac_try_echo="\"\$as_me:$LINENO: $ac_try_echo\""
$as_echo "$ac_try_echo") >&5
(eval "$ac_try") 2>&5
ac_status=$?
$as_echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); }; }; then
ac_cv_gettid=yes
else
$as_echo "$as_me: program exited with status $ac_status" >&5
$as_echo "$as_me: failed program was:" >&5
sed 's/^/| /' conftest.$ac_ext >&5
( exit $ac_status )
ac_cv_gettid=no
fi
rm -rf conftest.dSYM
rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
fi
fi
{ $as_echo "$as_me:$LINENO: result: $ac_cv_gettid" >&5
$as_echo "$ac_cv_gettid" >&6; }
if test "$ac_cv_gettid" = "yes"; then
cat >>confdefs.h <<\_ACEOF
#define HAVE_GETTID 1
_ACEOF
fi
#--------------------------------------------------------------------
# Check whether Objective-C /really/ works
#--------------------------------------------------------------------

View file

@ -1748,7 +1748,6 @@ CFLAGS=$saved_CFLAGS
# Check if we can name pthreads
#--------------------------------------------------------------------
AC_CHECK_FUNCS(pthread_set_name_np)
if test $ac_cv_func_pthread_set_name_np == yes; then
AC_DEFINE(PTHREAD_SETNAME(a), (pthread_set_name_np(pthread_self(), a), 0),
@ -1786,6 +1785,21 @@ else
esac
fi
#--------------------------------------------------------------------
# Check whether we can get the system thread ID
#--------------------------------------------------------------------
AC_CACHE_CHECK([for gettid()], ac_cv_gettid,
[AC_TRY_RUN(#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
int main(int argc, char **argv) {
pid_t t = syscall(SYS_gettid); return t == -1 ? 1 : 0; },
[ac_cv_gettid=yes], [ac_cv_gettid=no], [ac_cv_gettid=no])])
if test "$ac_cv_gettid" = "yes"; then
AC_DEFINE(HAVE_GETTID, 1, [Define if you have gettid()])
fi
#--------------------------------------------------------------------
# Check whether Objective-C /really/ works
#--------------------------------------------------------------------