Logging and NARGS utils.
authordsc <david.schoonover@gmail.com>
Fri, 24 Jun 2011 06:11:44 +0000 (23:11 -0700)
committerdsc <david.schoonover@gmail.com>
Fri, 24 Jun 2011 06:11:44 +0000 (23:11 -0700)
src/qq/util/QQLogging.h [new file with mode: 0644]
src/qq/util/QQLogging.mm [new file with mode: 0644]
src/qq/util/QQ_NARG.h [new file with mode: 0644]

diff --git a/src/qq/util/QQLogging.h b/src/qq/util/QQLogging.h
new file mode 100644 (file)
index 0000000..6cfa704
--- /dev/null
@@ -0,0 +1,64 @@
+// Copyright (c) 2008-2010, Vincent Gable.
+// vincent.gable@gmail.com
+
+#ifndef QQ_LOGGING_H
+#define QQ_LOGGING_H
+
+
+//based off of http://www.dribin.org/dave/blog/archives/2008/09/22/convert_to_nsstring/
+NSString* VTPG_DDToStringFromTypeAndValue(const char* typeCode, void* value);
+NSString* VTPG_DDToStringFromTypeAndValue(const char* typeCode, const void* value);
+
+// WARNING: if NO_LOG_MACROS is #define-ed, than THE ARGUMENT WILL NOT BE EVALUATED
+#ifndef NO_LOG_MACROS
+
+#import "qq/util/QQ_NARG.h"
+
+
+#define QQLogExpr(_X_, ...) do{                                                                                                                \
+    __typeof__(_X_) _Y_ = (_X_);                                                                                                               \
+    const char* _TYPE_CODE_ = @encode(__typeof__(_X_));                                                                                        \
+    NSString* _STR_ = VTPG_DDToStringFromTypeAndValue(_TYPE_CODE_, &_Y_);                                                                      \
+    if(_STR_)                                                                                                                                  \
+        NSLog(@"%s = %@", #_X_, _STR_);                                                                                                        \
+    else                                                                                                                                       \
+        NSLog(@"Unknown _TYPE_CODE_: %s for expression %s in function %s, file %s, line %d", _TYPE_CODE_, #_X_, __func__, __FILE__, __LINE__); \
+}while(0)
+
+#define QQLogExprWithName(_X_, _X_NAME_, ...) do{                                                                                              \
+    __typeof__(_X_) _Y_ = (_X_);                                                                                                               \
+    const char* _TYPE_CODE_ = @encode(__typeof__(_X_));                                                                                        \
+    NSString* _STR_ = VTPG_DDToStringFromTypeAndValue(_TYPE_CODE_, &_Y_);                                                                      \
+    if(_STR_)                                                                                                                                  \
+        NSLog(@"%s = %@", _X_NAME_, _STR_);                                                                                                    \
+    else                                                                                                                                       \
+        NSLog(@"Unknown _TYPE_CODE_: %s for expression %s in function %s, file %s, line %d", _TYPE_CODE_, #_X_, __func__, __FILE__, __LINE__); \
+}while(0)
+
+#define QQLog(...) do{                                                       \
+    if (QQ_NARG(__VA_ARGS__) == 1)                                           \
+        QQLogExpr(__VA_ARGS__);                                              \
+    else                                                                     \
+        QQLogExprWithName(__VA_ARGS__, 0);                                   \
+}while(0)
+
+
+
+#else /* NO_LOG_MACROS */
+
+#define QQLogExpr(_X_, ...)
+#define QQLogExprWithName(_X_, _X_NAME_, ...)
+#define QQLog(...)
+
+#endif /* NO_LOG_MACROS */
+
+
+
+// http://www.wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html
+static inline BOOL IsEmpty(id thing) {
+    return thing == nil ||
+            ([thing respondsToSelector:@selector(length)] && [(NSData *)thing length] == 0) ||
+            ([thing respondsToSelector:@selector(count)]  && [(NSArray *)thing count] == 0);
+}
+
+#endif
diff --git a/src/qq/util/QQLogging.mm b/src/qq/util/QQLogging.mm
new file mode 100644 (file)
index 0000000..d5729fc
--- /dev/null
@@ -0,0 +1,102 @@
+#import "qq/util/QQLogging.h"
+// Copyright (c) 2008-2010, Vincent Gable.
+// http://vincentgable.com
+//
+//based off http://www.dribin.org/dave/blog/archives/2008/09/22/convert_to_nsstring/
+//
+static BOOL TypeCodeIsCharArray(const char *typeCode){
+    int lastCharOffset = strlen(typeCode) - 1;
+    int secondToLastCharOffset = lastCharOffset - 1 ;
+    
+    BOOL isCharArray = typeCode[0] == '[' &&
+                        typeCode[secondToLastCharOffset] == 'c' && typeCode[lastCharOffset] == ']';
+    for(int i = 1; i < secondToLastCharOffset; i++)
+        isCharArray = isCharArray && isdigit(typeCode[i]);
+    return isCharArray;
+}
+
+//since BOOL is #defined as a signed char, we treat the value as
+//a BOOL if it is exactly YES or NO, and a char otherwise.
+static NSString* VTPGStringFromBoolOrCharValue(BOOL boolOrCharvalue) {
+    if(boolOrCharvalue == YES)
+        return @"YES";
+    if(boolOrCharvalue == NO)
+        return @"NO";
+    return [NSString stringWithFormat:@"'%c'", boolOrCharvalue];
+}
+
+static NSString* VTPGStringFromFourCharCodeOrUnsignedInt32(FourCharCode fourcc) {
+    return [NSString stringWithFormat:@"%u ('%c%c%c%c')",
+                                        fourcc,
+                                        (fourcc >> 24) & 0xFF,
+                                        (fourcc >> 16) & 0xFF,
+                                        (fourcc >> 8) & 0xFF,
+                                        fourcc & 0xFF];
+}
+
+static NSString* StringFromNSDecimalWithCurrentLocal(NSDecimal dcm) {
+    return NSDecimalString(&dcm, [NSLocale currentLocale]);
+}
+
+NSString* VTPG_DDToStringFromTypeAndValue(const char* typeCode, void* value) {
+    return VTPG_DDToStringFromTypeAndValue(typeCode, (const void*)value);
+}
+NSString* VTPG_DDToStringFromTypeAndValue(const char* typeCode, const void* value) {
+    #define IF_TYPE_MATCHES_INTERPRET_WITH(typeToMatch,func) \
+        if (strcmp(typeCode, @encode(typeToMatch)) == 0) \
+            return (func)(*(typeToMatch*)value)
+
+#if TARGET_OS_IPHONE
+    IF_TYPE_MATCHES_INTERPRET_WITH(CGPoint,NSStringFromCGPoint);
+    IF_TYPE_MATCHES_INTERPRET_WITH(CGSize,NSStringFromCGSize);
+    IF_TYPE_MATCHES_INTERPRET_WITH(CGRect,NSStringFromCGRect);
+#else
+    IF_TYPE_MATCHES_INTERPRET_WITH(NSPoint,NSStringFromPoint);
+    IF_TYPE_MATCHES_INTERPRET_WITH(NSSize,NSStringFromSize);
+    IF_TYPE_MATCHES_INTERPRET_WITH(NSRect,NSStringFromRect);
+#endif
+    IF_TYPE_MATCHES_INTERPRET_WITH(NSRange,NSStringFromRange);
+    IF_TYPE_MATCHES_INTERPRET_WITH(Class,NSStringFromClass);
+    IF_TYPE_MATCHES_INTERPRET_WITH(SEL,NSStringFromSelector);
+    IF_TYPE_MATCHES_INTERPRET_WITH(BOOL,VTPGStringFromBoolOrCharValue);
+    IF_TYPE_MATCHES_INTERPRET_WITH(NSDecimal,StringFromNSDecimalWithCurrentLocal);
+    
+    #define IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(typeToMatch,formatString) \
+        if (strcmp(typeCode, @encode(typeToMatch)) == 0) \
+            return [NSString stringWithFormat:(formatString), (*(typeToMatch*)value)]
+    
+    
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(CFStringRef,@"%@"); //CFStringRef is toll-free bridged to NSString*
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(CFArrayRef,@"%@"); //CFArrayRef is toll-free bridged to NSArray*
+    IF_TYPE_MATCHES_INTERPRET_WITH(FourCharCode, VTPGStringFromFourCharCodeOrUnsignedInt32);
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(long long,@"%lld");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(unsigned long long,@"%llu");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(float,@"%f");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(double,@"%f");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(id,@"%@");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(short,@"%hi");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(unsigned short,@"%hu");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(int,@"%i");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(unsigned, @"%u");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(long,@"%i");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(long double,@"%Lf"); //WARNING on older versions of OS X, @encode(long double) == @encode(double)
+    
+    //C-strings
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(char*, @"%s");
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(const char*, @"%s");
+    if(TypeCodeIsCharArray(typeCode))
+        return [NSString stringWithFormat:@"%s", (char*)value];
+    
+    IF_TYPE_MATCHES_INTERPRET_WITH_FORMAT(void*,@"(void*)%p");
+    
+    //This is a hack to print out CLLocationCoordinate2D, without needing to #import <CoreLocation/CoreLocation.h>
+    //A CLLocationCoordinate2D is a struct made up of 2 doubles.
+    //We detect it by hard-coding the result of @encode(CLLocationCoordinate2D).
+    //We get at the fields by treating it like an array of doubles, which it is identical to in memory.
+    if(strcmp(typeCode, "{?=dd}")==0)//@encode(CLLocationCoordinate2D)
+        return [NSString stringWithFormat:@"{latitude=%g,longitude=%g}",((double*)value)[0],((double*)value)[1]];
+    
+    //we don't know how to convert this typecode into an NSString
+    return nil;
+}
diff --git a/src/qq/util/QQ_NARG.h b/src/qq/util/QQ_NARG.h
new file mode 100644 (file)
index 0000000..8cfb588
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef QQ_NARG_H
+#define QQ_NARG_H
+
+
+/* The QQ_NARG macro returns the number of arguments that have been passed to it.
+ * https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s
+ */
+#define QQ_NARG(...)                         \
+    QQ_NARG_(__VA_ARGS__,QQ_RSEQ_N())
+
+#define QQ_NARG_(...)                        \
+    QQ_ARG_N(__VA_ARGS__)
+
+#define QQ_ARG_N(                            \
+     _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
+    _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
+    _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
+    _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
+    _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
+    _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
+    _61,_62,_63,N,...) N
+
+#define QQ_RSEQ_N()                          \
+    63,62,61,60,                             \
+    59,58,57,56,55,54,53,52,51,50,           \
+    49,48,47,46,45,44,43,42,41,40,           \
+    39,38,37,36,35,34,33,32,31,30,           \
+    29,28,27,26,25,24,23,22,21,20,           \
+    19,18,17,16,15,14,13,12,11,10,           \
+    9,8,7,6,5,4,3,2,1,0
+
+
+#define QQ_VA_ARG1(__X1__, ...)                     __X1__
+#define QQ_VA_ARG2(__X1__, __X2__, ...)             __X2__
+#define QQ_VA_ARG3(__X1__, __X2__, __X3__, ...)     __X3__
+
+#define QQ_VA_HEAD(__HEAD__, ...)                   __HEAD__
+#define QQ_VA_TAIL(__HEAD__, ...)                   __VA_ARGS__
+
+#endif