1997-01-06 22:04:07 +00:00
|
|
|
/* Interface for NSLog for GNUStep
|
|
|
|
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
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#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>
|
1997-01-06 22:04:07 +00:00
|
|
|
|
1999-03-09 05:55:19 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
|
|
|
|
1997-12-11 19:09:56 +00:00
|
|
|
#ifndef __WIN32__
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1997-01-06 22:04:07 +00:00
|
|
|
static void
|
|
|
|
_NSLog_standard_printf_handler (NSString* message)
|
|
|
|
{
|
1999-03-09 06:10:01 +00:00
|
|
|
unsigned len = [message length];
|
|
|
|
char buf[len+1];
|
|
|
|
|
|
|
|
[message getCString: buf];
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
1999-07-21 15:16:43 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
1999-03-09 05:55:19 +00:00
|
|
|
|
1999-03-09 06:10:01 +00:00
|
|
|
if (write(2, buf, len) != len)
|
1999-03-09 05:55:19 +00:00
|
|
|
{
|
1999-03-09 06:10:01 +00:00
|
|
|
int mask;
|
|
|
|
|
|
|
|
#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
|
|
|
|
syslog(mask, "%s", buf);
|
1999-03-09 05:55:19 +00:00
|
|
|
}
|
|
|
|
#else
|
1999-03-09 06:10:01 +00:00
|
|
|
write(2, buf, len);
|
1999-03-09 05:55:19 +00:00
|
|
|
#endif
|
1997-01-06 22:04:07 +00:00
|
|
|
}
|
|
|
|
|
1999-07-22 14:17:27 +00:00
|
|
|
NSLog_printf_handler *_NSLog_printf_handler = _NSLog_standard_printf_handler;
|
|
|
|
|
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
|
|
|
|
1997-01-06 22:04:07 +00:00
|
|
|
void
|
|
|
|
NSLogv (NSString* format, va_list args)
|
1996-01-22 23:22:11 +00:00
|
|
|
{
|
1999-03-09 05:55:19 +00:00
|
|
|
static NSRecursiveLock *myLock = nil;
|
|
|
|
NSAutoreleasePool *arp;
|
|
|
|
NSString *prefix;
|
|
|
|
NSString *message;
|
1999-05-06 05:49:55 +00:00
|
|
|
int pid;
|
1999-03-09 05:55:19 +00:00
|
|
|
|
|
|
|
arp = [NSAutoreleasePool new];
|
1997-01-06 22:04:07 +00:00
|
|
|
|
|
|
|
if (_NSLog_printf_handler == NULL)
|
|
|
|
_NSLog_printf_handler = *_NSLog_standard_printf_handler;
|
|
|
|
|
1999-05-11 09:21:38 +00:00
|
|
|
#if defined(__WIN32__)
|
1999-05-06 05:49:55 +00:00
|
|
|
pid = (int)GetCurrentProcessId(),
|
|
|
|
#else
|
|
|
|
pid = (int)getpid();
|
|
|
|
#endif
|
|
|
|
|
1997-01-06 22:04:07 +00:00
|
|
|
prefix = [NSString
|
|
|
|
stringWithFormat: @"%@ %@[%d] ",
|
|
|
|
[[NSCalendarDate calendarDate]
|
|
|
|
descriptionWithCalendarFormat: @"%b %d %H:%M:%S"],
|
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)
|
|
|
|
{
|
|
|
|
[gnustep_global_lock lock];
|
|
|
|
if (myLock == nil)
|
|
|
|
{
|
|
|
|
myLock = [NSRecursiveLock new];
|
|
|
|
}
|
|
|
|
[gnustep_global_lock unlock];
|
|
|
|
}
|
|
|
|
[myLock lock];
|
|
|
|
|
1999-07-22 14:17:27 +00:00
|
|
|
_NSLog_printf_handler(prefix);
|
1999-03-09 05:55:19 +00:00
|
|
|
|
|
|
|
[myLock unlock];
|
|
|
|
|
1998-09-02 12:34:38 +00:00
|
|
|
[arp release];
|
1996-01-22 23:22:11 +00:00
|
|
|
}
|
1997-01-06 22:04:07 +00:00
|
|
|
|