Optimized logging

This commit is contained in:
Pierre-Olivier Latour
2014-04-09 19:15:03 -07:00
parent a3996f3fbf
commit 1e1fd24b5d
2 changed files with 32 additions and 19 deletions
+25 -13
View File
@@ -63,6 +63,14 @@
} }
@end @end
#ifndef __GCDWEBSERVER_LOGGING_HEADER__
#ifdef NDEBUG
long GCDLogMinLevel = 2; // INFO level and higher
#else
long GCDLogMinLevel = 0; // DEBUG level and higher
#endif
#endif
static NSDateFormatter* _dateFormatterRFC822 = nil; static NSDateFormatter* _dateFormatterRFC822 = nil;
static dispatch_queue_t _dateFormatterQueue = NULL; static dispatch_queue_t _dateFormatterQueue = NULL;
#if !TARGET_OS_IPHONE #if !TARGET_OS_IPHONE
@@ -73,19 +81,12 @@ static BOOL _run;
void GCDLogMessage(long level, NSString* format, ...) { void GCDLogMessage(long level, NSString* format, ...) {
static const char* levelNames[] = {"DEBUG", "VERBOSE", "INFO", "WARNING", "ERROR", "EXCEPTION"}; static const char* levelNames[] = {"DEBUG", "VERBOSE", "INFO", "WARNING", "ERROR", "EXCEPTION"};
static long minLevel = -1; va_list arguments;
if (minLevel < 0) { va_start(arguments, format);
const char* logLevel = getenv("logLevel"); NSString* message = [[NSString alloc] initWithFormat:format arguments:arguments];
minLevel = logLevel ? atoi(logLevel) : 0; va_end(arguments);
} fprintf(stderr, "[%s] %s\n", levelNames[level], [message UTF8String]);
if (level >= minLevel) { ARC_RELEASE(message);
va_list arguments;
va_start(arguments, format);
NSString* message = [[NSString alloc] initWithFormat:format arguments:arguments];
va_end(arguments);
fprintf(stderr, "[%s] %s\n", levelNames[level], [message UTF8String]);
ARC_RELEASE(message);
}
} }
#endif #endif
@@ -305,6 +306,17 @@ static void _SignalHandler(int signal) {
@synthesize handlers=_handlers, port=_port; @synthesize handlers=_handlers, port=_port;
#ifndef __GCDWEBSERVER_LOGGING_HEADER__
+ (void)load {
const char* logLevel = getenv("logLevel");
if (logLevel) {
GCDLogMinLevel = atoi(logLevel);
}
}
#endif
+ (void)initialize { + (void)initialize {
if (_dateFormatterRFC822 == nil) { if (_dateFormatterRFC822 == nil) {
DCHECK([NSThread isMainThread]); // NSDateFormatter should be initialized on main thread DCHECK([NSThread isMainThread]); // NSDateFormatter should be initialized on main thread
+7 -6
View File
@@ -71,13 +71,14 @@
#else #else
extern long GCDLogMinLevel;
extern void GCDLogMessage(long level, NSString* format, ...) NS_FORMAT_FUNCTION(2, 3); extern void GCDLogMessage(long level, NSString* format, ...) NS_FORMAT_FUNCTION(2, 3);
#define LOG_VERBOSE(...) GCDLogMessage(1, __VA_ARGS__) #define LOG_VERBOSE(...) do { if (GCDLogMinLevel <= 1) GCDLogMessage(1, __VA_ARGS__); } while (0)
#define LOG_INFO(...) GCDLogMessage(2, __VA_ARGS__) #define LOG_INFO(...) do { if (GCDLogMinLevel <= 2) GCDLogMessage(2, __VA_ARGS__); } while (0)
#define LOG_WARNING(...) GCDLogMessage(3, __VA_ARGS__) #define LOG_WARNING(...) do { if (GCDLogMinLevel <= 3) GCDLogMessage(3, __VA_ARGS__); } while (0)
#define LOG_ERROR(...) GCDLogMessage(4, __VA_ARGS__) #define LOG_ERROR(...) do { if (GCDLogMinLevel <= 4) GCDLogMessage(4, __VA_ARGS__); } while (0)
#define LOG_EXCEPTION(__EXCEPTION__) GCDLogMessage(5, @"%@", __EXCEPTION__) #define LOG_EXCEPTION(__EXCEPTION__) do { if (GCDLogMinLevel <= 5) GCDLogMessage(5, @"%@", __EXCEPTION__); } while (0)
#ifdef NDEBUG #ifdef NDEBUG
@@ -94,7 +95,7 @@ extern void GCDLogMessage(long level, NSString* format, ...) NS_FORMAT_FUNCTION(
} \ } \
} while (0) } while (0)
#define DNOT_REACHED() abort() #define DNOT_REACHED() abort()
#define LOG_DEBUG(...) GCDLogMessage(0, __VA_ARGS__) #define LOG_DEBUG(...) do { if (GCDLogMinLevel <= 0) GCDLogMessage(0, __VA_ARGS__); } while (0)
#endif #endif