Desde hace algún tiempo he estado usando un sitio de macros adoptado de varias arriba. Mi enfoque en el inicio de sesión en la consola, con énfasis en controlado & verbosidad filtrada; Si no te importa una gran cantidad de líneas de registro, pero desea cambiar fácilmente lotes de ellas en &, entonces puede que le resulte útil.
En primer lugar, sustituir opcionalmente NSLog con printf como se describe por @Rodrigo anteriormente
#define NSLOG_DROPCHAFF//comment out to get usual date/time ,etc:2011-11-03 13:43:55.632 myApp[3739:207] Hello Word
#ifdef NSLOG_DROPCHAFF
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#endif
A continuación, modificador de registro encendido o apagado.
#ifdef DEBUG
#define LOG_CATEGORY_DETAIL// comment out to turn all conditional logging off while keeping other DEBUG features
#endif
en el bloque principal, definir varios categorías correspondientes a los módulos en su aplicación. Defina también un nivel de registro por encima del cual no se invocarán las llamadas de registro. A continuación, definir varios sabores de la producción NSLog
#ifdef LOG_CATEGORY_DETAIL
//define the categories using bitwise leftshift operators
#define kLogGCD (1<<0)
#define kLogCoreCreate (1<<1)
#define kLogModel (1<<2)
#define kLogVC (1<<3)
#define kLogFile (1<<4)
//etc
//add the categories that should be logged...
#define kLOGIFcategory kLogModel+kLogVC+kLogCoreCreate
//...and the maximum detailLevel to report (use -1 to override the category switch)
#define kLOGIFdetailLTEQ 4
// output looks like this:"-[AppDelegate myMethod] log string..."
# define myLog(category,detailLevel,format, ...) if(detailLevel<0 || ((category&kLOGIFcategory)&&detailLevel<= kLOGIFdetailLTEQ)) {NSLog((@"%s " format), __PRETTY_FUNCTION__, ##__VA_ARGS__);}
// output also shows line number:"-[AppDelegate myMethod][l17] log string..."
# define myLogLine(category,detailLevel,format, ...) if(detailLevel<0 || ((category&kLOGIFcategory)&&detailLevel<= kLOGIFdetailLTEQ)) {NSLog((@"%s[l%i] " format), __PRETTY_FUNCTION__,__LINE__ ,##__VA_ARGS__);}
// output very simple:" log string..."
# define myLogSimple(category,detailLevel,format, ...) if(detailLevel<0 || ((category&kLOGIFcategory)&&detailLevel<= kLOGIFdetailLTEQ)) {NSLog((@"" format), ##__VA_ARGS__);}
//as myLog but only shows method name: "myMethod: log string..."
// (Doesn't work in C-functions)
# define myLog_cmd(category,detailLevel,format,...) if(detailLevel<0 || ((category&kLOGIFcategory)&&detailLevel<= kLOGIFdetailLTEQ)) {NSLog((@"%@: " format), NSStringFromSelector(_cmd), ##__VA_ARGS__);}
//as myLogLine but only shows method name: "myMethod>l17: log string..."
# define myLog_cmdLine(category,detailLevel,format, ...) if(detailLevel<0 || ((category&kLOGIFcategory)&&detailLevel<= kLOGIFdetailLTEQ)) {NSLog((@"%@>l%i: " format), NSStringFromSelector(_cmd),__LINE__ , ##__VA_ARGS__);}
//or define your own...
// # define myLogEAGLcontext(category,detailLevel,format, ...) if(detailLevel<0 || ((category&kLOGIFcategory)&&detailLevel<= kLOGIFdetailLTEQ)) {NSLog((@"%s>l%i (ctx:%@)" format), __PRETTY_FUNCTION__,__LINE__ ,[EAGLContext currentContext], ##__VA_ARGS__);}
#else
# define myLog_cmd(...)
# define myLog_cmdLine(...)
# define myLog(...)
# define myLogLine(...)
# define myLogSimple(...)
//# define myLogEAGLcontext(...)
#endif
Por lo tanto, la configuración actual de kLOGIFcategory y kLOGIFdetailLTEQ, una llamada como
myLogLine(kLogVC, 2, @"%@",self);
imprimirá pero esto no
myLogLine(kLogGCD, 2, @"%@",self);//GCD not being printed
ni tampoco
myLogLine(kLogGCD, 12, @"%@",self);//level too high
Si desea anular la configuración de una llamada de registro individual, utilice un nivel negativo:
myLogLine(kLogGCD, -2, @"%@",self);//now printed even tho' GCD category not active.
encuentro los pocos caracteres adicionales de escribir cada línea valen como pueda a continuación
- Active o desactive una categoría completa de comentarios (p. Ej. Sólo reportar esas llamadas marcadas Modelo)
- informe sobre los detalles finos con los números de nivel superior o sólo las llamadas más importantes marcados con los números más bajos
Estoy seguro de que muchos encontrarán esto un poco de una exageración, pero sólo en caso de que alguien encuentre conviene a sus propósitos ..
** primera pregunta dónde favoritos (estrella) son más que upvotes ... ** 1 .. –