forked from veritech/NSLogger-CocoaLumberjack-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DDNSLoggerLogger.m
99 lines (81 loc) · 2.45 KB
/
DDNSLoggerLogger.m
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// DDNSLoggerLogger.m
// Created by Peter Steinberger on 26.10.10.
//
#import "DDNSLoggerLogger.h"
// NSLogger is needed: http://github.com/fpillet/NSLogger
#import "LoggerClient.h"
@implementation DDNSLoggerLogger
static DDNSLoggerLogger *sharedInstance;
// The logger instance we use
static Logger *_DDNSLogger_logger = nil;
/**
* The runtime sends initialize to each class in a program exactly one time just before the class,
* or any class that inherits from it, is sent its first message from within the program. (Thus the
* method may never be invoked if the class is not used.) The runtime sends the initialize message to
* classes in a thread-safe manner. Superclasses receive this message before their subclasses.
*
* This method may also be called directly (assumably by accident), hence the safety mechanism.
**/
+ (void)initialize
{
static BOOL initialized = NO;
if (!initialized)
{
initialized = YES;
sharedInstance = [[DDNSLoggerLogger alloc] init];
}
}
+ (DDNSLoggerLogger *)sharedInstance
{
return sharedInstance;
}
- (id)init
{
if (sharedInstance != nil)
{
return nil;
}
if ((self = [super init]))
{
// create and remember the logger instance
_DDNSLogger_logger = LoggerInit();
// configure the logger
LoggerSetOptions(_DDNSLogger_logger, kLoggerOption_BufferLogsUntilConnection | kLoggerOption_BrowseBonjour | kLoggerOption_BrowseOnlyLocalDomain );
LoggerStart(_DDNSLogger_logger);
}
return self;
}
- (void)setupWithBonjourServiceName:(NSString *)serviceName
{
LoggerSetupBonjour(_DDNSLogger_logger, NULL, (__bridge CFStringRef)serviceName);
}
- (void)logMessage:(DDLogMessage *)logMessage
{
NSString *logMsg = logMessage->logMsg;
if (formatter)
{
// formatting is supported but not encouraged!
logMsg = [formatter formatLogMessage:logMessage];
}
if (logMsg)
{
int nsloggerLogLevel;
switch (logMessage->logFlag)
{
// NSLogger log levels start a 0, the bigger the number,
// the more specific / detailed the trace is meant to be
case LOG_FLAG_ERROR : nsloggerLogLevel = 0; break;
case LOG_FLAG_WARN : nsloggerLogLevel = 1; break;
case LOG_FLAG_INFO : nsloggerLogLevel = 2; break;
default : nsloggerLogLevel = 3; break;
}
LogMessageF(logMessage->file, logMessage->lineNumber, logMessage->function, [logMessage fileName],
nsloggerLogLevel, @"%@", logMsg);
}
}
- (NSString *)loggerName
{
return @"cocoa.lumberjack.NSLogger";
}
@end