Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request swisspol#379 from kayak/runtime-logger
Browse files Browse the repository at this point in the history
Added support to override built-in logger at runtime
  • Loading branch information
swisspol authored Mar 13, 2019
2 parents 8d0a359 + 1125433 commit 2c53064
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 15 additions & 0 deletions GCDWebServer/Core/GCDWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ typedef GCDWebServerResponse* _Nullable (^GCDWebServerProcessBlock)(__kindof GCD
typedef void (^GCDWebServerCompletionBlock)(GCDWebServerResponse* _Nullable response);
typedef void (^GCDWebServerAsyncProcessBlock)(__kindof GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock);

/**
* The GCDWebServerBuiltInLoggerBlock is used to override the built-in logger at runtime.
* The block will be passed the log level and the log message, see setLogLevel for
* documentation of the log levels for the built-in logger.
*/
typedef void (^GCDWebServerBuiltInLoggerBlock)(int level, NSString* _Nonnull message);

/**
* The port used by the GCDWebServer (NSNumber / NSUInteger).
*
Expand Down Expand Up @@ -573,6 +580,14 @@ extern NSString* const GCDWebServerAuthenticationMethod_DigestAccess;
*/
+ (void)setLogLevel:(int)level;

/**
* Set a logger to be used instead of the built-in logger which logs to stderr.
*
* IMPORTANT: In order for this override to work, you should not be specifying
* a custom logger at compile time with "__GCDWEBSERVER_LOGGING_HEADER__".
*/
+ (void)setBuiltInLogger:(GCDWebServerBuiltInLoggerBlock)block;

/**
* Logs a message to the logging facility at the VERBOSE level.
*/
Expand Down
18 changes: 16 additions & 2 deletions GCDWebServer/Core/GCDWebServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,24 @@

#ifdef __GCDWEBSERVER_LOGGING_FACILITY_BUILTIN__

static GCDWebServerBuiltInLoggerBlock _builtInLoggerBlock;

void GCDWebServerLogMessage(GCDWebServerLoggingLevel level, NSString* format, ...) {
static const char* levelNames[] = {"DEBUG", "VERBOSE", "INFO", "WARNING", "ERROR"};
static int enableLogging = -1;
if (enableLogging < 0) {
enableLogging = (isatty(STDERR_FILENO) ? 1 : 0);
}
if (enableLogging) {
if (_builtInLoggerBlock || enableLogging) {
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]);
if (_builtInLoggerBlock) {
_builtInLoggerBlock(level, message);
} else {
fprintf(stderr, "[%s] %s\n", levelNames[level], [message UTF8String]);
}
}
}

Expand Down Expand Up @@ -1071,6 +1077,14 @@ + (void)setLogLevel:(int)level {
#endif
}

+ (void)setBuiltInLogger:(GCDWebServerBuiltInLoggerBlock)block {
#if defined(__GCDWEBSERVER_LOGGING_FACILITY_BUILTIN__)
_builtInLoggerBlock = block;
#else
GWS_DNOT_REACHED(); // Built-in logger must be enabled in order to override
#endif
}

- (void)logVerbose:(NSString*)format, ... {
va_list arguments;
va_start(arguments, format);
Expand Down

0 comments on commit 2c53064

Please sign in to comment.