2001-12-17 14:31:42 +00:00
|
|
|
/** Interface for NSLog for GNUStep
|
1997-01-06 22:04:07 +00:00
|
|
|
Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
|
|
|
Date: November 1996
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-01-22 23:22:11 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1996-01-22 23:22:11 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-01-22 23:22:11 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1996-01-22 23:22:11 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2006-10-20 10:56:27 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
2002-10-11 09:14:14 +00:00
|
|
|
<title>NSLog reference</title>
|
2001-12-18 16:54:15 +00:00
|
|
|
$Date$ $Revision$
|
2005-02-22 11:22:44 +00:00
|
|
|
*/
|
1996-01-22 23:22:11 +00:00
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2010-02-17 11:47:06 +00:00
|
|
|
#import "Foundation/NSDate.h"
|
|
|
|
#import "Foundation/NSCalendarDate.h"
|
|
|
|
#import "Foundation/NSTimeZone.h"
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
#import "Foundation/NSProcessInfo.h"
|
|
|
|
#import "Foundation/NSLock.h"
|
|
|
|
#import "Foundation/NSAutoreleasePool.h"
|
|
|
|
#import "Foundation/NSData.h"
|
|
|
|
#import "Foundation/NSThread.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
#import "GNUstepBase/NSString+GNUstepBase.h"
|
1997-01-06 22:04:07 +00:00
|
|
|
|
1999-03-09 05:55:19 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
|
|
|
|
2005-11-05 16:20:19 +00:00
|
|
|
#define UNISTR(X) \
|
|
|
|
((const unichar*)[(X) cStringUsingEncoding: NSUnicodeStringEncoding])
|
|
|
|
|
2005-10-09 09:32:54 +00:00
|
|
|
#if defined(HAVE_SYSLOG)
|
|
|
|
# if defined(LOG_ERR)
|
|
|
|
# if defined(LOG_USER)
|
|
|
|
# define SYSLOGMASK (LOG_ERR|LOG_USER)
|
|
|
|
# else
|
|
|
|
# define SYSLOGMASK (LOG_ERR)
|
|
|
|
# endif // LOG_USER
|
|
|
|
# elif defined(LOG_ERROR)
|
|
|
|
# if defined(LOG_USER)
|
|
|
|
# define SYSLOGMASK (LOG_ERROR|LOG_USER)
|
|
|
|
# else
|
|
|
|
# define SYSLOGMASK (LOG_ERROR)
|
|
|
|
# endif // LOG_USER
|
|
|
|
# else
|
|
|
|
# error "Help, I can't find a logging level for syslog"
|
|
|
|
# endif
|
|
|
|
#endif // HAVE_SYSLOG
|
|
|
|
|
2010-02-17 11:47:06 +00:00
|
|
|
#import "GSPrivate.h"
|
2001-11-10 17:31:39 +00:00
|
|
|
|
2005-02-22 14:06:28 +00:00
|
|
|
extern NSThread *GSCurrentThread();
|
|
|
|
|
2002-10-09 09:54:43 +00:00
|
|
|
/**
|
|
|
|
* A variable holding the file descriptor to which NSLogv() messages are
|
|
|
|
* written by default. GNUstep initialises this to stderr.<br />
|
|
|
|
* You may change this, but for thread safety should
|
|
|
|
* use the lock provided by GSLogLock() to protect the change.
|
|
|
|
*/
|
|
|
|
int _NSLogDescriptor = 2;
|
|
|
|
|
|
|
|
static NSRecursiveLock *myLock = nil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the lock used to protect the GNUstep NSLogv() implementation.
|
|
|
|
* Use this to protect changes to
|
|
|
|
* <ref type="variable" id="_NSLogDescriptor">_NSLogDescriptor</ref> and
|
|
|
|
* <ref type="variable" id="_NSLog_printf_handler">_NSLog_printf_handler</ref>
|
|
|
|
*/
|
|
|
|
NSRecursiveLock *
|
|
|
|
GSLogLock()
|
|
|
|
{
|
|
|
|
if (myLock == nil)
|
|
|
|
{
|
|
|
|
[gnustep_global_lock lock];
|
|
|
|
if (myLock == nil)
|
|
|
|
{
|
|
|
|
myLock = [NSRecursiveLock new];
|
|
|
|
}
|
|
|
|
[gnustep_global_lock unlock];
|
|
|
|
}
|
|
|
|
return myLock;
|
|
|
|
}
|
2002-05-27 14:03:10 +00:00
|
|
|
|
1997-01-06 22:04:07 +00:00
|
|
|
static void
|
|
|
|
_NSLog_standard_printf_handler (NSString* message)
|
|
|
|
{
|
2001-10-02 12:00:25 +00:00
|
|
|
NSData *d;
|
|
|
|
const char *buf;
|
|
|
|
unsigned len;
|
2010-03-19 12:10:11 +00:00
|
|
|
#if defined(__MINGW__)
|
2005-11-05 16:20:19 +00:00
|
|
|
LPCWSTR null_terminated_buf;
|
|
|
|
#else
|
|
|
|
#if defined(HAVE_SYSLOG)
|
|
|
|
char *null_terminated_buf;
|
|
|
|
#endif
|
2005-10-30 09:15:21 +00:00
|
|
|
#endif
|
2002-04-07 18:56:08 +00:00
|
|
|
static NSStringEncoding enc = 0;
|
1999-03-09 06:10:01 +00:00
|
|
|
|
2002-04-07 18:56:08 +00:00
|
|
|
if (enc == 0)
|
|
|
|
{
|
|
|
|
enc = [NSString defaultCStringEncoding];
|
|
|
|
}
|
|
|
|
d = [message dataUsingEncoding: enc allowLossyConversion: NO];
|
2001-10-02 12:00:25 +00:00
|
|
|
if (d == nil)
|
|
|
|
{
|
|
|
|
d = [message dataUsingEncoding: NSUTF8StringEncoding
|
|
|
|
allowLossyConversion: NO];
|
|
|
|
}
|
1999-03-09 06:10:01 +00:00
|
|
|
|
2001-10-02 12:00:25 +00:00
|
|
|
if (d == nil) // Should never happen.
|
|
|
|
{
|
|
|
|
buf = [message lossyCString];
|
|
|
|
len = strlen(buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf = (const char*)[d bytes];
|
|
|
|
len = [d length];
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2010-03-19 12:10:11 +00:00
|
|
|
#if defined(__MINGW__)
|
2005-11-05 16:20:19 +00:00
|
|
|
null_terminated_buf = UNISTR(message);
|
2005-10-30 09:15:21 +00:00
|
|
|
|
2005-11-05 16:20:19 +00:00
|
|
|
OutputDebugStringW(null_terminated_buf);
|
2005-10-30 09:15:21 +00:00
|
|
|
|
2006-10-20 10:56:27 +00:00
|
|
|
if ((GSPrivateDefaultsFlag(GSLogSyslog) == YES
|
2005-10-30 09:15:21 +00:00
|
|
|
|| write(_NSLogDescriptor, buf, len) != (int)len) && !IsDebuggerPresent())
|
|
|
|
{
|
|
|
|
static HANDLE eventloghandle = 0;
|
|
|
|
|
|
|
|
if (!eventloghandle)
|
|
|
|
{
|
2005-11-05 17:41:01 +00:00
|
|
|
eventloghandle = RegisterEventSourceW(NULL,
|
2005-11-05 16:20:19 +00:00
|
|
|
UNISTR([[NSProcessInfo processInfo] processName]));
|
2005-10-30 09:15:21 +00:00
|
|
|
}
|
|
|
|
if (eventloghandle)
|
|
|
|
{
|
2005-11-05 16:20:19 +00:00
|
|
|
ReportEventW(eventloghandle, // event log handle
|
2005-10-30 09:15:21 +00:00
|
|
|
EVENTLOG_WARNING_TYPE, // event type
|
|
|
|
0, // category zero
|
|
|
|
0, // event identifier
|
|
|
|
NULL, // no user security identifier
|
|
|
|
1, // one substitution string
|
|
|
|
0, // no data
|
2005-11-05 16:20:19 +00:00
|
|
|
&null_terminated_buf, // pointer to string array
|
2005-10-30 09:15:21 +00:00
|
|
|
NULL); // pointer to data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(HAVE_SYSLOG)
|
2006-10-20 10:56:27 +00:00
|
|
|
if (GSPrivateDefaultsFlag(GSLogSyslog) == YES
|
2003-01-03 20:14:47 +00:00
|
|
|
|| write(_NSLogDescriptor, buf, len) != (int)len)
|
1999-03-09 05:55:19 +00:00
|
|
|
{
|
2011-02-19 19:42:42 +00:00
|
|
|
null_terminated_buf = malloc(sizeof (char) * (len + 1));
|
2002-06-05 17:04:06 +00:00
|
|
|
strncpy (null_terminated_buf, buf, len);
|
|
|
|
null_terminated_buf[len] = '\0';
|
1999-03-09 06:10:01 +00:00
|
|
|
|
2005-10-09 09:32:54 +00:00
|
|
|
syslog(SYSLOGMASK, "%s", null_terminated_buf);
|
1999-03-09 06:10:01 +00:00
|
|
|
|
2011-02-19 19:42:42 +00:00
|
|
|
free(null_terminated_buf);
|
1999-03-09 05:55:19 +00:00
|
|
|
}
|
|
|
|
#else
|
2002-05-27 14:03:10 +00:00
|
|
|
write(_NSLogDescriptor, buf, len);
|
1999-03-09 05:55:19 +00:00
|
|
|
#endif
|
2010-03-19 12:10:11 +00:00
|
|
|
#endif // __MINGW__
|
1997-01-06 22:04:07 +00:00
|
|
|
}
|
|
|
|
|
2002-10-09 09:54:43 +00:00
|
|
|
/**
|
|
|
|
* A pointer to a function used to actually write the log data.
|
|
|
|
* <p>
|
|
|
|
* GNUstep initialises this to a function implementing the standard
|
|
|
|
* behavior for logging, but you may change this in your program
|
|
|
|
* in order to implement any custom behavior you wish. You should
|
|
|
|
* use the lock returned by GSLogLock() to protect any change you make.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
|
|
|
* Calls from NSLogv() to the function pointed to by this variable
|
|
|
|
* are protected by a lock, and should therefore be thread safe.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
|
|
|
* This function should accept a single NSString argument and return void.
|
|
|
|
* </p>
|
|
|
|
* The default implementation in GNUstep performs as follows -
|
|
|
|
* <list>
|
|
|
|
* <item>
|
|
|
|
* Converts the string to be logged to data in the default CString
|
|
|
|
* encoding or, if that is not possible, to UTF8 data.
|
|
|
|
* </item>
|
|
|
|
* <item>
|
|
|
|
* If the system supports writing to syslog and the user default to
|
|
|
|
* say that logging should be done to syslog (GSLogSyslog) is set,
|
2005-10-09 09:32:54 +00:00
|
|
|
* writes the data to the syslog.<br />
|
|
|
|
* On an mswindows system, where syslog is not available, the
|
|
|
|
* GSLogSyslog user default controls whether or not data is written
|
|
|
|
* to the system event log,
|
2002-10-09 09:54:43 +00:00
|
|
|
* </item>
|
|
|
|
* <item>
|
|
|
|
* Otherwise, writes the data to the file descriptor stored in the
|
|
|
|
* variable
|
|
|
|
* <ref type="variable" id="_NSLogDescriptor">_NSLogDescriptor</ref>,
|
|
|
|
* which is set by default to stderr.<br />
|
|
|
|
* Your program may change this descriptor ... but you should protect
|
|
|
|
* changes using the lock provided by GSLogLock().<br />
|
|
|
|
* NB. If the write to the descriptor fails, and the system supports
|
|
|
|
* writing to syslog, then the log is written to syslog as if the
|
|
|
|
* appropriate user default had been set.
|
|
|
|
* </item>
|
|
|
|
* </list>
|
|
|
|
*/
|
1999-07-22 14:17:27 +00:00
|
|
|
NSLog_printf_handler *_NSLog_printf_handler = _NSLog_standard_printf_handler;
|
|
|
|
|
2002-10-09 09:54:43 +00:00
|
|
|
/**
|
2002-10-11 09:14:14 +00:00
|
|
|
* <p>Provides the standard OpenStep logging facility. For details see
|
|
|
|
* the lower level NSLogv() function (which this function uses).
|
|
|
|
* </p>
|
|
|
|
* <p>GNUstep provides powerful alternatives for logging ... see
|
2003-07-01 17:00:14 +00:00
|
|
|
* NSDebugLog(), NSWarnLog() and GSPrintf() for example. We recommend
|
2002-10-11 09:14:14 +00:00
|
|
|
* the use of NSDebugLog() and its relatives for debug purposes, and
|
|
|
|
* GSPrintf() for general log messages, with NSLog() being reserved
|
2003-07-01 17:00:14 +00:00
|
|
|
* for reporting possible/likely errors. GSPrintf() is declared in
|
|
|
|
* GSObjCRuntime.h.
|
2002-10-11 09:14:14 +00:00
|
|
|
* </p>
|
2002-10-09 09:54:43 +00:00
|
|
|
*/
|
2005-02-22 11:22:44 +00:00
|
|
|
void
|
1997-01-06 22:04:07 +00:00
|
|
|
NSLog (NSString* format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
1996-01-22 23:22:11 +00:00
|
|
|
|
1997-01-06 22:04:07 +00:00
|
|
|
va_start (ap, format);
|
|
|
|
NSLogv (format, ap);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
1996-01-22 23:22:11 +00:00
|
|
|
|
2002-10-09 09:54:43 +00:00
|
|
|
/**
|
|
|
|
* The core logging function ...
|
|
|
|
* <p>
|
|
|
|
* The function generates a standard log entry by prepending
|
|
|
|
* process ID and date/time information to your message, and
|
|
|
|
* ensuring that a newline is present at the end of the message.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
2005-02-22 14:06:28 +00:00
|
|
|
* In GNUstep, the GSLogThread user default may be set to YES in
|
|
|
|
* order to instruct this function to include the internal ID of
|
2005-10-09 09:32:54 +00:00
|
|
|
* the current thread after the process ID. This can help you
|
2005-02-22 14:06:28 +00:00
|
|
|
* to track the behavior of a multi-threaded program.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
2002-10-09 09:54:43 +00:00
|
|
|
* 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
|
|
|
|
* logging is thread-safe. However, the actual creation of the
|
2003-07-22 08:52:37 +00:00
|
|
|
* message written is only as safe as the [NSObject-description] methods
|
|
|
|
* of the arguments you supply.
|
2002-10-09 09:54:43 +00:00
|
|
|
* </p>
|
|
|
|
* <p>
|
|
|
|
* The function to write the data is pointed to by
|
|
|
|
* <ref type="variable" id="_NSLog_printf_handler">_NSLog_printf_handler</ref>
|
|
|
|
* </p>
|
|
|
|
*/
|
2005-02-22 11:22:44 +00:00
|
|
|
void
|
1997-01-06 22:04:07 +00:00
|
|
|
NSLogv (NSString* format, va_list args)
|
1996-01-22 23:22:11 +00:00
|
|
|
{
|
2011-02-28 19:49:57 +00:00
|
|
|
NSString *prefix;
|
|
|
|
NSString *message;
|
|
|
|
static int pid = 0;
|
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
1999-03-09 05:55:19 +00:00
|
|
|
|
1997-01-06 22:04:07 +00:00
|
|
|
if (_NSLog_printf_handler == NULL)
|
2005-10-30 09:15:21 +00:00
|
|
|
{
|
|
|
|
_NSLog_printf_handler = *_NSLog_standard_printf_handler;
|
|
|
|
}
|
1997-01-06 22:04:07 +00:00
|
|
|
|
2005-10-30 09:15:21 +00:00
|
|
|
if (pid == 0)
|
|
|
|
{
|
2010-03-19 12:10:11 +00:00
|
|
|
#if defined(__MINGW__)
|
2005-10-30 09:15:21 +00:00
|
|
|
pid = (int)GetCurrentProcessId();
|
1999-05-06 05:49:55 +00:00
|
|
|
#else
|
2005-10-30 09:15:21 +00:00
|
|
|
pid = (int)getpid();
|
1999-05-06 05:49:55 +00:00
|
|
|
#endif
|
2005-10-30 09:15:21 +00:00
|
|
|
}
|
1999-05-06 05:49:55 +00:00
|
|
|
|
2002-06-06 05:22:45 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
2006-10-20 10:56:27 +00:00
|
|
|
if (GSPrivateDefaultsFlag(GSLogSyslog) == YES)
|
2005-02-22 14:06:28 +00:00
|
|
|
{
|
2006-10-20 10:56:27 +00:00
|
|
|
if (GSPrivateDefaultsFlag(GSLogThread) == YES)
|
2005-02-22 14:06:28 +00:00
|
|
|
{
|
2005-02-22 14:17:22 +00:00
|
|
|
prefix = [NSString stringWithFormat: @"[thread:%x] ",
|
|
|
|
GSCurrentThread()];
|
2005-02-22 14:06:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prefix = @"";
|
|
|
|
}
|
|
|
|
}
|
2002-06-06 05:22:45 +00:00
|
|
|
else
|
|
|
|
#endif
|
2005-02-22 14:06:28 +00:00
|
|
|
{
|
2006-10-20 10:56:27 +00:00
|
|
|
if (GSPrivateDefaultsFlag(GSLogThread) == YES)
|
2005-02-22 14:06:28 +00:00
|
|
|
{
|
|
|
|
prefix = [NSString
|
2005-02-22 14:17:22 +00:00
|
|
|
stringWithFormat: @"%@ %@[%d,%x] ",
|
2005-02-22 14:06:28 +00:00
|
|
|
[[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];
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 22:04:07 +00:00
|
|
|
|
|
|
|
/* Check if there is already a newline at the end of the format */
|
2005-10-30 09:15:21 +00:00
|
|
|
if ([format hasSuffix: @"\n"] == NO)
|
|
|
|
{
|
|
|
|
format = [format stringByAppendingString: @"\n"];
|
|
|
|
}
|
1997-01-06 22:04:07 +00:00
|
|
|
message = [NSString stringWithFormat: format arguments: args];
|
|
|
|
|
|
|
|
prefix = [prefix stringByAppendingString: message];
|
1999-03-09 05:55:19 +00:00
|
|
|
|
|
|
|
if (myLock == nil)
|
|
|
|
{
|
2002-10-09 09:54:43 +00:00
|
|
|
GSLogLock();
|
1999-03-09 05:55:19 +00:00
|
|
|
}
|
2002-10-09 09:54:43 +00:00
|
|
|
|
1999-03-09 05:55:19 +00:00
|
|
|
[myLock lock];
|
|
|
|
|
1999-07-22 14:17:27 +00:00
|
|
|
_NSLog_printf_handler(prefix);
|
1999-03-09 05:55:19 +00:00
|
|
|
|
|
|
|
[myLock unlock];
|
|
|
|
|
2011-05-27 11:48:44 +00:00
|
|
|
[arp drain];
|
1996-01-22 23:22:11 +00:00
|
|
|
}
|
1997-01-06 22:04:07 +00:00
|
|
|
|