Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add ability to customize logging output with JRLogLogger protocol

  • Loading branch information...
commit 64ec0d5e7163a709dd6e4ed9663c94ea570740b5 1 parent 053679d
@ddribin ddribin authored
View
15 JRLog.h
@@ -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_);
View
35 JRLog.m
@@ -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;
@@ -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);
@@ -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
View
10 JRLogDemo/AppController.h
@@ -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
View
23 JRLogDemo/AppController.m
@@ -1,6 +1,4 @@
#import "AppController.h"
-#define JRLogOverrideNSLog 1
-#import "JRLog.h"
@implementation AppController
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.