Added GSPrintf()

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@14714 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2002-10-11 09:14:14 +00:00
parent 8584c46545
commit 9364a48117
12 changed files with 241 additions and 92 deletions

View file

@ -20,7 +20,7 @@
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
<title>NSLog class reference</title>
<title>NSLog reference</title>
$Date$ $Revision$
*/
@ -182,7 +182,15 @@ _NSLog_standard_printf_handler (NSString* message)
NSLog_printf_handler *_NSLog_printf_handler = _NSLog_standard_printf_handler;
/**
* Provides a standard logging facility via NSLogv().
* <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>
*/
void
NSLog (NSString* format, ...)
@ -264,3 +272,61 @@ NSLogv (NSString* format, va_list args)
RELEASE(arp);
}
/**
* <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;
}