/** Interface for NSLog for GNUStep Copyright (C) 1996, 1997 Free Software Foundation, Inc. Written by: Adam Fedor Date: November 1996 This file is part of the GNUstep Base Library. 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., 59 Temple Place, Suite 330, Boston, MA 02111 USA. NSLog class reference $Date$ $Revision$ */ #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_SYSLOG_H #include #endif #ifdef HAVE_UNISTD_H #include #endif #include "GSPrivate.h" int _NSLogDescriptor = 2; // Default descriptor for logging static void _NSLog_standard_printf_handler (NSString* message) { NSData *d; const char *buf; unsigned len; static NSStringEncoding enc = 0; if (enc == 0) { enc = [NSString defaultCStringEncoding]; } d = [message dataUsingEncoding: enc allowLossyConversion: NO]; if (d == nil) { d = [message dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion: NO]; } if (d == nil) // Should never happen. { buf = [message lossyCString]; len = strlen(buf); } else { buf = (const char*)[d bytes]; len = [d length]; } #ifdef HAVE_SYSLOG if (GSUserDefaultsFlag(GSLogSyslog) == YES || write(_NSLogDescriptor, buf, len) != len) { int mask; #ifdef LOG_ERR mask = LOG_ERR; #else # ifdef LOG_ERROR mask = LOG_ERROR; # else # error "Help, I can't find a logging level for syslog" # endif #endif #ifdef LOG_USER mask |= LOG_USER; #endif syslog(mask, "%s", buf); } #else write(_NSLogDescriptor, buf, len); #endif } NSLog_printf_handler *_NSLog_printf_handler = _NSLog_standard_printf_handler; void NSLog (NSString* format, ...) { va_list ap; va_start (ap, format); NSLogv (format, ap); va_end (ap); } void NSLogv (NSString* format, va_list args) { static NSRecursiveLock *myLock = nil; CREATE_AUTORELEASE_POOL(arp); NSString *prefix; NSString *message; int pid; if (_NSLog_printf_handler == NULL) _NSLog_printf_handler = *_NSLog_standard_printf_handler; #if defined(__MINGW__) pid = (int)GetCurrentProcessId(), #else pid = (int)getpid(); #endif 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"]) format = [format stringByAppendingString: @"\n"]; message = [NSString stringWithFormat: format arguments: args]; prefix = [prefix stringByAppendingString: message]; if (myLock == nil) { [gnustep_global_lock lock]; if (myLock == nil) { myLock = [NSRecursiveLock new]; } [gnustep_global_lock unlock]; } [myLock lock]; _NSLog_printf_handler(prefix); [myLock unlock]; RELEASE(arp); }