Skip to content

Commit

Permalink
Add ability to customize logging output with JRLogLogger protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ddribin committed Jan 26, 2007
1 parent 053679d commit 64ec0d5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
15 changes: 15 additions & 0 deletions JRLog.h
Expand Up @@ -19,12 +19,27 @@ typedef enum {
JRLogLevel_Off,
} JRLogLevel;

@protocol JRLogLogger

- (void)logWithLevel:(JRLogLevel)callerLevel_
instance:(NSString*)instance_
file:(const char*)file_
line:(unsigned)line_
function:(const char*)function_
message:(NSString*)message_;

@end

@interface NSObject (JRLogAdditions)
+ (JRLogLevel)classJRLogLevel;
+ (void)setClassJRLogLevel:(JRLogLevel)level_;

+ (JRLogLevel)defaultJRLogLevel;
+ (void)setDefaultJRLogLevel:(JRLogLevel)level_;

+ (void)setJRLogLogger: (id<JRLogLogger>) logger_;
+ (id<JRLogLogger>)JRLogLogger;
+ (id<JRLogLogger>)defaultJRLogLogger;
@end

BOOL IsJRLogLevelActive(id self_, JRLogLevel level_);
Expand Down
35 changes: 28 additions & 7 deletions JRLog.m
Expand Up @@ -29,7 +29,7 @@ @protocol JRLogDestinationDO
- (oneway void)logWithDictionary:(bycopy NSDictionary*)dictionary_;
@end

@interface JRLogOutput : NSObject {
@interface JRLogOutput : NSObject <JRLogLogger> {
NSString *sessionUUID;
BOOL tryDO;
id<JRLogDestinationDO> destination;
Expand Down Expand Up @@ -207,12 +207,13 @@ BOOL IsJRLogLevelActive(id self_, JRLogLevel callerLevel_) {
NSString *message = [[NSString alloc] initWithFormat:format_ arguments:args];
va_end(args);

[[JRLogOutput sharedOutput] logWithLevel:callerLevel_
instance:self_ ? [NSString stringWithFormat:@"<%@: %p>", [self_ className], self_] : @"nil"
file:file_
line:line_
function:function_
message:message];
id<JRLogLogger> logger = [JRLogOutput JRLogLogger];
[logger logWithLevel:callerLevel_
instance:self_ ? [NSString stringWithFormat:@"<%@: %p>", [self_ className], self_] : @"nil"
file:file_
line:line_
function:function_
message:message];

if (JRLogLevel_Fatal == callerLevel_) {
exit(0);
Expand Down Expand Up @@ -255,4 +256,24 @@ + (void)setDefaultJRLogLevel:(JRLogLevel)level_ {
gDefaultJRLogLevel = level_;
}

static id<JRLogLogger> sLogger = nil;

+ (void)setJRLogLogger: (id<JRLogLogger>) logger_;
{
sLogger = logger_;
}

+ (id<JRLogLogger>)JRLogLogger;
{
if (sLogger == nil)
return [JRLogOutput sharedOutput];
else
return sLogger;
}

+ (id<JRLogLogger>)defaultJRLogLogger;
{
return [JRLogOutput sharedOutput];
}

@end
10 changes: 9 additions & 1 deletion JRLogDemo/AppController.h
Expand Up @@ -7,8 +7,16 @@
//

#import <Cocoa/Cocoa.h>
#define JRLogOverrideNSLog 1
#import "JRLog.h"

@interface AppController : NSObject {
@interface AppController : NSObject <JRLogLogger> {
}

- (void)logWithLevel:(JRLogLevel)callerLevel_
instance:(NSString*)instance_
file:(const char*)file_
line:(unsigned)line_
function:(const char*)function_
message:(NSString*)message_;
@end
23 changes: 20 additions & 3 deletions JRLogDemo/AppController.m
@@ -1,6 +1,4 @@
#import "AppController.h"
#define JRLogOverrideNSLog 1
#import "JRLog.h"

@implementation AppController

Expand All @@ -17,8 +15,27 @@ static void logC() {
}

- (void)log:(NSTimer*)timer_ {
static i = 0;
i++;
if ((i % 2) == 0)
[AppController setJRLogLogger: self];
else
[AppController setJRLogLogger: nil]; // Set to default logger

//logC();
NSLog(@"time: %@", [NSCalendarDate date]);
JRLogInfo(@"time: %@", [NSCalendarDate date]);
}


- (void)logWithLevel:(JRLogLevel)callerLevel_
instance:(NSString*)instance_
file:(const char*)file_
line:(unsigned)line_
function:(const char*)function_
message:(NSString*)message_;
{
// NSLog is overridden to JRLog and will cause infinite recursion
fprintf(stderr, "custom log: %s\n", [message_ UTF8String]);
}

@end

0 comments on commit 64ec0d5

Please sign in to comment.