Re: Name of current function/selector
Re: Name of current function/selector
- Subject: Re: Name of current function/selector
- From: Andy Lee <email@hidden>
- Date: Sat, 22 Oct 2005 10:25:54 -0700
On Oct 22, 2005, at 10:13 AM, Pontus Ilbring wrote:
There's a macro called __PRETTY_FUNCTION__ that returns the name of
the current function as a cstring.
That's handy to know.
Here are some macros I wrote for logging various conditions in
methods. Feel free to use/rename/improve/correct. The "DIGS" is for
"Digital Spokes."
I forget where I read about the "##" syntax for using varargs in macros.
--Andy
========== DIGSLog.h ==========
/*
* DIGSLog.h
*
* Created by Andy Lee on Wed Jul 10 2002.
* Copyright (c) 2003, 2004 Andy Lee. All rights reserved.
*
* $Revision: 1.12 $
*/
#import <Foundation/Foundation.h>
/*!
* @header DIGSLog
* @discussion Wrappers around NSLog that allow setting of log
verbosity
* levels.
*/
/*!
* @enum Log verbosity levels
* @abstract Values that can be passed to DIGSSetVerbosityLevel().
* @discussion Anything lower than DIGS_VERBOSITY_NONE works the same
* as DIGS_VERBOSITY_NONE, and anything higher than
* DIGS_VERBOSITY_ALL works the same as
DIGS_VERBOSITY_ALL.
*
* @constant DIGS_VERBOSITY_NONE
* Use DIGS_VERBOSITY_NONE to suppress all log output.
* @constant DIGS_VERBOSITY_ERROR
* Use DIGS_VERBOSITY_ERROR to log anomalies without
* workarounds.
* @constant DIGS_VERBOSITY_WARNING
* Use DIGS_VERBOSITY_WARNING to log anomalies with
* workarounds.
* @constant DIGS_VERBOSITY_INFO
* Use DIGS_VERBOSITY_INFO to log normal information
* about the state of the program. This is the
default
* verbosity level.
* @constant DIGS_VERBOSITY_DEBUG
* Use DIGS_VERBOSITY_DEBUG to log information that is
* only needed for debugging and should not be logged
* by a deployed version of the app.
* @constant DIGS_VERBOSITY_ALL
* Use DIGS_VERBOSITY_ALL to turn on all logging.
*/
enum
{
DIGS_VERBOSITY_NONE = 0,
DIGS_VERBOSITY_ERROR = 10,
DIGS_VERBOSITY_WARNING = 20,
DIGS_VERBOSITY_INFO = 30,
DIGS_VERBOSITY_DEBUG = 40,
DIGS_VERBOSITY_ALL = 99,
};
/*!
* @const DIGSLogVerbosityUserDefault
* @discussion For use by NSUserDefaults. Value is @"DIGSVerbosity".
*/
extern const NSString *DIGSLogVerbosityUserDefault;
/*!
* @function DIGSGetVerbosityLevel
* @discussion Returns the verbosity level used by the various
* DIGSLogXXX() functions.
*/
extern int DIGSGetVerbosityLevel();
/*!
* @function DIGSSetVerbosityLevel
* @discussion Sets the verbosity level used by the various
DIGSLogXXX()
* functions.
*/
extern void DIGSSetVerbosityLevel(int level);
/*!
* @function DIGSLogError
* @discussion Logs output if verbosity level >= DIGS_VERBOSITY_ERROR.
*/
#define DIGSLogError(format, ...)\
if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_ERROR)\
{\
NSLog(\
[@"[_ERROR_] " stringByAppendingString:(format)],\
## __VA_ARGS__);\
}
/*!
* @function DIGSLogWarning
* @discussion Logs output if verbosity level >=
DIGS_VERBOSITY_WARNING.
*/
#define DIGSLogWarning(format, ...)\
if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_WARNING)\
{\
NSLog(\
[@"[_WARNING_] " stringByAppendingString:(format)],\
## __VA_ARGS__);\
}
/*!
* @function DIGSLogInfo
* @discussion Logs output if verbosity level >= DIGS_VERBOSITY_INFO.
*/
#define DIGSLogInfo(format, ...)\
if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_INFO)\
{\
NSLog(\
[@"[_INFO_] " stringByAppendingString:(format)],\
## __VA_ARGS__);\
}
/*!
* @function DIGSLogDebug
* @discussion Logs output if verbosity level >= DIGS_VERBOSITY_DEBUG.
*/
#define DIGSLogDebug(format, ...)\
if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_DEBUG)\
{\
NSLog(\
[@"[_DEBUG_] " stringByAppendingString:(format)],\
## __VA_ARGS__);\
}
/*!
* @function DIGSLogMissingOverride
* @discussion Stick this in implementations of abstract methods.
*/
#define DIGSLogMissingOverride()\
{\
if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_ERROR)\
DIGSLogError(\
@"%@ must override %@",\
[self class],\
NSStringFromSelector(_cmd));\
}
/*!
* @function DIGSLogEnteringMethod
* @discussion Stick this at the beginning of a method to log the fact
* that it is being entered.
*/
#define DIGSLogEnteringMethod()\
{\
if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_DEBUG)\
DIGSLogDebug(\
@"%@ -- entering %@",\
[self class],\
NSStringFromSelector(_cmd));\
}
/*!
* @function DIGSLogExitingMethodPrematurely
* @discussion Call this to log the fact that you are about to return
* from a method prematurely due to an error condition.
*/
#define DIGSLogExitingMethodPrematurely(msgString)\
{\
if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_ERROR)\
DIGSLogError(\
@"%@ -- exiting %@ early -- %@",\
[self class],\
NSStringFromSelector(_cmd),\
(msgString));\
}
/*!
* @function DIGSLogExitingMethod
* @discussion Stick this at the end of a method to log the fact
that it
* is being exited.
*/
#define DIGSLogExitingMethod()\
{\
if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_DEBUG)\
DIGSLogDebug(\
@"%@ -- exiting %@",\
[self class],\
NSStringFromSelector(_cmd));\
}
/*!
* @function DIGSLogNondesignatedInitializer
* @discussion Call this in the implementation of an initializer that
* should never be called because it is not the designated
* initializer.
*/
#define DIGSLogNondesignatedInitializer()\
{\
if (DIGSGetVerbosityLevel() >= DIGS_VERBOSITY_ERROR)\
DIGSLogError(\
@"%@ -- '%@' is not the designated initializer",\
[self class],\
NSStringFromSelector(_cmd));\
}
========== DIGSLog.m ==========
/*
* DIGSLog.m
*
* Created by Andy Lee on Wed Jul 10 2002.
* Copyright (c) 2003, 2004 Andy Lee. All rights reserved.
*
* $Revision: 1.4 $
*/
#import "DIGSLog.h"
const NSString *DIGSLogVerbosityUserDefault = @"DIGSVerbosity";
static int g_verbosityLevel = DIGS_VERBOSITY_INFO;
int DIGSGetVerbosityLevel() { return g_verbosityLevel; }
void DIGSSetVerbosityLevel(int level) { g_verbosityLevel = level; }
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden