2001-12-17 14:31:42 +00:00
|
|
|
/** NSAssertionHandler - Object encapsulation of assertions
|
1997-01-06 22:40:16 +00:00
|
|
|
Copyright (C) 1995, 1997 Free Software Foundation, Inc.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1995-04-10 00:10:51 +00:00
|
|
|
Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
|
|
|
Date: Apr 1995
|
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
|
|
|
|
1995-04-10 00:10:51 +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
|
1995-04-10 00:10:51 +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
|
|
|
|
1995-04-10 00:10:51 +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
|
1995-04-10 00:10:51 +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
|
|
|
|
|
|
|
<title>NSAssertionHandler class reference</title>
|
|
|
|
$Date$ $Revision$
|
1995-04-10 00:34:30 +00:00
|
|
|
*/
|
1997-09-01 21:59:51 +00:00
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "config.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
#include "GNUstepBase/preface.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
#include "Foundation/NSDictionary.h"
|
|
|
|
#include "Foundation/NSObjCRuntime.h"
|
|
|
|
#include "Foundation/NSString.h"
|
|
|
|
#include "Foundation/NSThread.h"
|
1995-04-10 00:10:51 +00:00
|
|
|
|
2002-12-31 10:41:10 +00:00
|
|
|
/**
|
|
|
|
* <p>NSAssertionHandler objects are used to raise exceptions on behalf of
|
|
|
|
* macros implementing assertions.<br />
|
|
|
|
* Each thread has its own assertion handler instance.<br />
|
|
|
|
* </p>
|
|
|
|
* <p>The macros work together with the assertion handler object to
|
|
|
|
* produce meaningful exception messages containing the name of the
|
|
|
|
* source file, the position within that file, and the name of the
|
|
|
|
* ObjC method or C function in which the assertion failed.
|
|
|
|
* </p>
|
2003-07-11 00:35:21 +00:00
|
|
|
* <p>An NSAssertionHandler instance is created on demand for each thread
|
|
|
|
* and is stored in the thread's dictionary under the key NSAssertionHandler.
|
|
|
|
* A custom NSAssertionHandler can be used by adding it to the thread
|
|
|
|
* dictionary under this key.
|
|
|
|
* </p>
|
2002-12-31 10:41:10 +00:00
|
|
|
* The assertion macros are:
|
|
|
|
* NSAssert(), NSCAssert(),
|
|
|
|
* NSAssert1(), NSCAssert1(),
|
|
|
|
* NSAssert2(), NSCAssert2(),
|
|
|
|
* NSAssert3(), NSCAssert3(),
|
|
|
|
* NSAssert4(), NSCAssert4(),
|
|
|
|
* NSAssert5(), NSCAssert5(),
|
|
|
|
* NSParameterAssert(), NSCParameterAssert()
|
|
|
|
*/
|
1995-04-10 00:10:51 +00:00
|
|
|
@implementation NSAssertionHandler
|
|
|
|
|
1997-03-03 19:51:04 +00:00
|
|
|
/* Key for thread dictionary. */
|
2003-07-11 00:35:21 +00:00
|
|
|
static NSString *dict_key = @"NSAssertionHandler";
|
1997-03-03 19:51:04 +00:00
|
|
|
|
2002-05-03 07:51:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the assertion handler object for the current thread.<br />
|
|
|
|
* If none exists, creates one and returns it.
|
|
|
|
*/
|
2001-04-11 12:30:32 +00:00
|
|
|
+ (NSAssertionHandler*) currentHandler
|
1995-04-10 00:10:51 +00:00
|
|
|
{
|
2001-04-11 12:30:32 +00:00
|
|
|
NSMutableDictionary *dict;
|
|
|
|
NSAssertionHandler *handler;
|
1997-03-03 19:51:04 +00:00
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
dict = GSCurrentThreadDictionary();
|
1997-03-03 19:51:04 +00:00
|
|
|
handler = [dict objectForKey: dict_key];
|
|
|
|
if (handler == nil)
|
|
|
|
{
|
|
|
|
handler = [[NSAssertionHandler alloc] init];
|
|
|
|
[dict setObject: handler forKey: dict_key];
|
2002-05-03 08:17:04 +00:00
|
|
|
RELEASE(handler);
|
1997-03-03 19:51:04 +00:00
|
|
|
}
|
|
|
|
return handler;
|
1995-04-10 00:10:51 +00:00
|
|
|
}
|
|
|
|
|
2002-05-03 07:51:28 +00:00
|
|
|
/**
|
|
|
|
* Handles an assertion failure by using NSLogv() to print an error
|
|
|
|
* message built from the supplied arguments, and then raising an
|
|
|
|
* NSInternalInconsistencyException
|
|
|
|
*/
|
2005-02-22 11:22:44 +00:00
|
|
|
- (void) handleFailureInFunction: (NSString*)functionName
|
|
|
|
file: (NSString*)fileName
|
2009-02-23 20:42:32 +00:00
|
|
|
lineNumber: (NSInteger)line
|
2000-02-19 00:40:47 +00:00
|
|
|
description: (NSString*)format,...
|
1995-04-10 00:10:51 +00:00
|
|
|
{
|
2001-04-11 12:30:32 +00:00
|
|
|
id message;
|
|
|
|
va_list ap;
|
1995-04-10 00:10:51 +00:00
|
|
|
|
1995-04-10 00:34:30 +00:00
|
|
|
va_start(ap, format);
|
1997-09-01 21:59:51 +00:00
|
|
|
message =
|
|
|
|
[NSString
|
2000-02-19 00:40:47 +00:00
|
|
|
stringWithFormat: @"%@:%d Assertion failed in %@. %@",
|
|
|
|
fileName, line, functionName, format];
|
1997-09-01 21:59:51 +00:00
|
|
|
NSLogv(message, ap);
|
|
|
|
|
2001-04-11 12:30:32 +00:00
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: message arguments: ap];
|
2002-05-03 07:51:28 +00:00
|
|
|
va_end(ap);
|
1995-04-10 00:34:30 +00:00
|
|
|
/* NOT REACHED */
|
1995-04-10 00:10:51 +00:00
|
|
|
}
|
|
|
|
|
2002-05-03 07:51:28 +00:00
|
|
|
/**
|
|
|
|
* Handles an assertion failure by using NSLogv() to print an error
|
|
|
|
* message built from the supplied arguments, and then raising an
|
|
|
|
* NSInternalInconsistencyException
|
|
|
|
*/
|
1997-01-06 22:40:16 +00:00
|
|
|
- (void) handleFailureInMethod: (SEL) aSelector
|
|
|
|
object: object
|
|
|
|
file: (NSString *) fileName
|
2009-02-23 20:42:32 +00:00
|
|
|
lineNumber: (NSInteger) line
|
1997-01-06 22:40:16 +00:00
|
|
|
description: (NSString *) format,...
|
1995-04-10 00:10:51 +00:00
|
|
|
{
|
2001-04-11 12:30:32 +00:00
|
|
|
id message;
|
|
|
|
va_list ap;
|
1995-04-10 00:10:51 +00:00
|
|
|
|
1995-04-10 00:34:30 +00:00
|
|
|
va_start(ap, format);
|
1997-09-01 21:59:51 +00:00
|
|
|
message =
|
|
|
|
[NSString
|
2002-03-12 12:48:11 +00:00
|
|
|
stringWithFormat: @"%@:%d Assertion failed in %@(%@), method %@. %@",
|
2005-02-22 11:22:44 +00:00
|
|
|
fileName, line, NSStringFromClass([object class]),
|
2002-03-12 12:48:11 +00:00
|
|
|
[object isInstance] ? @"instance" : @"class",
|
2001-05-29 02:38:22 +00:00
|
|
|
NSStringFromSelector(aSelector), format];
|
1997-09-01 21:59:51 +00:00
|
|
|
NSLogv(message, ap);
|
1997-01-06 22:40:16 +00:00
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
[NSException raise: NSInternalInconsistencyException
|
2001-04-11 12:30:32 +00:00
|
|
|
format: message arguments: ap];
|
1995-04-10 00:34:30 +00:00
|
|
|
va_end(ap);
|
|
|
|
/* NOT REACHED */
|
1995-04-10 00:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|