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
|
1996-01-22 23:22:11 +00:00
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1997-01-06 22:04:07 +00:00
|
|
|
|
1996-01-22 23:22:11 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, 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$
|
1996-01-22 23:22:11 +00:00
|
|
|
*/
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1999-05-11 09:21:38 +00:00
|
|
|
#include <base/preface.h>
|
1997-01-06 22:04:07 +00:00
|
|
|
#include <Foundation/NSObjCRuntime.h>
|
|
|
|
#include <Foundation/NSDate.h>
|
2001-03-08 14:48:27 +00:00
|
|
|
#include <Foundation/NSCalendarDate.h>
|
|
|
|
#include <Foundation/NSTimeZone.h>
|
1997-01-06 22:04:07 +00:00
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSProcessInfo.h>
|
1999-03-09 05:55:19 +00:00
|
|
|
#include <Foundation/NSLock.h>
|
1998-09-02 12:34:38 +00:00
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
2001-10-02 12:00:25 +00:00
|
|
|
#include <Foundation/NSData.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
|
|
|
|
|
2002-05-02 21:22:06 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1997-12-11 19:09:56 +00:00
|
|
|
#include <unistd.h>
|
2002-02-20 06:42:05 +00:00
|
|
|
#endif
|
1997-12-11 19:09:56 +00:00
|
|
|
|
2002-03-13 09:58:43 +00:00
|
|
|
#include "GSPrivate.h"
|
2001-11-10 17:31:39 +00:00
|
|
|
|
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;
|
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];
|
|
|
|
}
|
|
|
|
|
1999-07-21 15:16:43 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
1999-03-09 05:55:19 +00:00
|
|
|
|
2002-05-27 14:03:10 +00:00
|
|
|
if (GSUserDefaultsFlag(GSLogSyslog) == YES
|
|
|
|
|| write(_NSLogDescriptor, buf, len) != len)
|
1999-03-09 05:55:19 +00:00
|
|
|
{
|
1999-03-09 06:10:01 +00:00
|
|
|
int mask;
|
2002-06-05 17:04:06 +00:00
|
|
|
/* We NULL-terminate the string in order to feed it to
|
|
|
|
* syslog. */
|
|
|
|
char *null_terminated_buf = objc_malloc (sizeof (char) * (len + 1));
|
|
|
|
strncpy (null_terminated_buf, buf, len);
|
|
|
|
null_terminated_buf[len] = '\0';
|
1999-03-09 06:10:01 +00:00
|
|
|
|
|
|
|
#ifdef LOG_ERR
|
|
|
|
mask = LOG_ERR;
|
|
|
|
#else
|
|
|
|
# ifdef LOG_ERROR
|
|
|
|
mask = LOG_ERROR;
|
|
|
|
# else
|
1999-03-10 14:30:27 +00:00
|
|
|
# error "Help, I can't find a logging level for syslog"
|
1999-03-09 06:10:01 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LOG_USER
|
|
|
|
mask |= LOG_USER;
|
|
|
|
#endif
|
2002-06-05 17:04:06 +00:00
|
|
|
syslog(mask, "%s", null_terminated_buf);
|
|
|
|
objc_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
|
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,
|
|
|
|
* writes the data to the syslog.
|
|
|
|
* </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
|
|
|
|
* NSDebugLog(), NSWarnLog(), and GSPrintf() for example. We recommend
|
|
|
|
* the use of NSDebugLog() and its relatives for debug purposes, and
|
|
|
|
* GSPrintf() for general log messages, with NSLog() being reserved
|
|
|
|
* for reporting possible/likely errors.
|
|
|
|
* </p>
|
2002-10-09 09:54:43 +00:00
|
|
|
*/
|
1997-01-06 22:04:07 +00:00
|
|
|
void
|
|
|
|
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>
|
|
|
|
* 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
|
|
|
|
* message written is only as safe as the -description methods of
|
|
|
|
* the arguments you supply.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
|
|
|
* The function to write the data is pointed to by
|
|
|
|
* <ref type="variable" id="_NSLog_printf_handler">_NSLog_printf_handler</ref>
|
|
|
|
* </p>
|
|
|
|
*/
|
1997-01-06 22:04:07 +00:00
|
|
|
void
|
|
|
|
NSLogv (NSString* format, va_list args)
|
1996-01-22 23:22:11 +00:00
|
|
|
{
|
2002-10-09 09:54:43 +00:00
|
|
|
NSString *prefix;
|
|
|
|
NSString *message;
|
|
|
|
int pid;
|
2002-06-25 12:13:19 +00:00
|
|
|
CREATE_AUTORELEASE_POOL(arp);
|
1999-03-09 05:55:19 +00:00
|
|
|
|
1997-01-06 22:04:07 +00:00
|
|
|
if (_NSLog_printf_handler == NULL)
|
|
|
|
_NSLog_printf_handler = *_NSLog_standard_printf_handler;
|
|
|
|
|
2000-06-12 05:17:41 +00:00
|
|
|
#if defined(__MINGW__)
|
1999-05-06 05:49:55 +00:00
|
|
|
pid = (int)GetCurrentProcessId(),
|
|
|
|
#else
|
|
|
|
pid = (int)getpid();
|
|
|
|
#endif
|
|
|
|
|
2002-06-06 05:22:45 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
if (GSUserDefaultsFlag(GSLogSyslog) == YES)
|
|
|
|
prefix = @"";
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
prefix = [NSString
|
1997-01-06 22:04:07 +00:00
|
|
|
stringWithFormat: @"%@ %@[%d] ",
|
|
|
|
[[NSCalendarDate calendarDate]
|
2002-05-15 16:24:46 +00:00
|
|
|
descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%F"],
|
1999-09-03 08:59:07 +00:00
|
|
|
[[NSProcessInfo processInfo] processName],
|
1999-05-06 05:49:55 +00:00
|
|
|
pid];
|
1997-01-06 22:04:07 +00:00
|
|
|
|
|
|
|
/* Check if there is already a newline at the end of the format */
|
|
|
|
if (![format hasSuffix: @"\n"])
|
|
|
|
format = [format stringByAppendingString: @"\n"];
|
|
|
|
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];
|
|
|
|
|
2001-04-28 05:53:56 +00:00
|
|
|
RELEASE(arp);
|
1996-01-22 23:22:11 +00:00
|
|
|
}
|
1997-01-06 22:04:07 +00:00
|
|
|
|
2002-10-11 09:14:14 +00:00
|
|
|
/**
|
|
|
|
* <p>Prints a message to fptr using the format string provided and any
|
|
|
|
* additional arguments. The format string is interpreted as by
|
|
|
|
* the NSString formatted initialisers, and understands the '%@' syntax
|
|
|
|
* for printing an object.
|
|
|
|
* </p>
|
|
|
|
* <p>The data is written to the file pointer in the default CString
|
|
|
|
* encoding if possible, as a UTF8 string otherwise.
|
|
|
|
* </p>
|
|
|
|
* <p>This function is recommended for printing general log messages.
|
|
|
|
* For debug messages use NSDebugLog() and friends. For error logging
|
|
|
|
* use NSLog(), and for warnings you might consider NSWarnLog().
|
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
GSPrintf (FILE *fptr, NSString* format, ...)
|
|
|
|
{
|
|
|
|
static Class stringClass = 0;
|
|
|
|
static NSStringEncoding enc;
|
|
|
|
CREATE_AUTORELEASE_POOL(arp);
|
|
|
|
va_list ap;
|
|
|
|
NSString *message;
|
|
|
|
NSData *data;
|
|
|
|
BOOL ok = NO;
|
|
|
|
|
|
|
|
if (stringClass == 0)
|
|
|
|
{
|
|
|
|
[gnustep_global_lock lock];
|
|
|
|
if (stringClass == 0)
|
|
|
|
{
|
|
|
|
stringClass = [NSString class];
|
|
|
|
enc = [stringClass defaultCStringEncoding];
|
|
|
|
}
|
|
|
|
[gnustep_global_lock unlock];
|
|
|
|
}
|
|
|
|
message = [stringClass allocWithZone: NSDefaultMallocZone()];
|
|
|
|
va_start (ap, format);
|
|
|
|
message = [message initWithFormat: format locale: nil arguments: ap];
|
|
|
|
va_end (ap);
|
|
|
|
data = [message dataUsingEncoding: enc];
|
|
|
|
if (data == nil)
|
|
|
|
{
|
|
|
|
data = [message dataUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
}
|
|
|
|
RELEASE(message);
|
|
|
|
|
|
|
|
if (data != nil)
|
|
|
|
{
|
|
|
|
unsigned int length = [data length];
|
|
|
|
|
|
|
|
if (length == 0 || fwrite([data bytes], 1, length, fptr) == length)
|
|
|
|
{
|
|
|
|
ok = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RELEASE(arp);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|