Skip to content

Commit bb822b9

Browse files
author
erichmzhang
committed
Add external log output
1 parent c0a1b58 commit bb822b9

File tree

5 files changed

+77
-7
lines changed

5 files changed

+77
-7
lines changed

QCloudCore/Classes/Logger/QCloudFileLogger.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
//
88

99
#import <Foundation/Foundation.h>
10-
10+
#import "QCloudLoggerOutput.h"
1111
@class QCloudFileLogger;
1212
@protocol QCloudFileLoggerDelegate <NSObject>
1313
- (void) fileLoggerDidFull:(QCloudFileLogger*)logger;
1414
@end
1515

1616
@class QCloudLogModel;
17-
@interface QCloudFileLogger : NSObject
17+
@interface QCloudFileLogger : QCloudLoggerOutput
1818
@property (nonatomic, weak) id<QCloudFileLoggerDelegate> delegate;
1919
@property (nonatomic, strong, readonly) NSString* path;
2020
@property (nonatomic, assign, readonly) uint64_t maxSize;
2121
@property (nonatomic, assign, readonly) uint64_t currentSize;
2222
@property (nonatomic, assign, readonly) BOOL isFull;
2323
- (instancetype) initWithPath:(NSString *)path maxSize:(uint64_t)maxSize;
24-
- (void) appendLog:(QCloudLogModel*(^)(void))logCreate;
2524
@end

QCloudCore/Classes/Logger/QCloudLogger.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import <Foundation/Foundation.h>
1010
#import "QCloudLogModel.h"
11+
#import "QCloudLoggerOutput.h"
1112

1213
#define QCloudLog(level, frmt, ...) \
1314
[[QCloudLogger sharedLogger] logMessageWithLevel:level cmd:__PRETTY_FUNCTION__ line:__LINE__ file:__FILE__ format:(frmt), ##__VA_ARGS__]
@@ -46,16 +47,19 @@ QCloudLog(QCloudLogLevelDebug,@"%@",[NSThread callStackSymbols])
4647
@property (nonatomic, assign) uint64_t maxStoarageSize;
4748

4849
@property (nonatomic, assign) float keepDays;
50+
51+
52+
4953
///--------------------------------------
5054
#pragma mark - Shared Logger
5155
///--------------------------------------
5256

5357
/**
54-
A shared instance of `PFLogger` that should be used for all logging.
58+
A shared instance of `QCloudLogger` that should be used for all logging.
5559
56-
@return An shared singleton instance of `PFLogger`.
60+
@return An shared singleton instance of `QCloudLogger`.
5761
*/
58-
+ (instancetype)sharedLogger; //TODO: (nlutsenko) Convert to use an instance everywhere instead of a shared singleton.
62+
+ (instancetype)sharedLogger;
5963

6064
///--------------------------------------
6165
#pragma mark - Logging Messages
@@ -67,4 +71,21 @@ QCloudLog(QCloudLogLevelDebug,@"%@",[NSThread callStackSymbols])
6771
line:(int)line
6872
file:(const char*)file
6973
format:(NSString *)format, ...;
74+
75+
76+
77+
/**
78+
增加一个输出源
79+
80+
@param output 输出源
81+
*/
82+
- (void) addLogger:(QCloudLoggerOutput*)output;
83+
84+
85+
/**
86+
删除一个输出源
87+
88+
@param output 删除一个输出源
89+
*/
90+
- (void) removeLogger:(QCloudLoggerOutput*)output;
7091
@end

QCloudCore/Classes/Logger/QCloudLogger.m

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ @interface QCloudLogger () <QCloudFileLoggerDelegate>
5454
@end
5555

5656
@implementation QCloudLogger
57+
{
58+
NSMutableArray* _loggerOutputs;
59+
}
5760

5861
+ (instancetype)sharedLogger
5962
{
@@ -85,8 +88,10 @@ - (instancetype) init
8588
if (!self) {
8689
return self;
8790
}
91+
_loggerOutputs = [NSMutableArray new];
8892
_currentFileLogger = [[QCloudFileLogger alloc] initWithPath:[self avilableLogFilePath] maxSize:QCloudEachLogFileSize];
8993
_currentFileLogger.delegate = self;
94+
[_loggerOutputs addObject:_currentFileLogger];
9095
_maxStoarageSize = 70*1024*1024;
9196
_keepDays = 3;
9297
//
@@ -113,6 +118,9 @@ - (instancetype) init
113118

114119
- (void) fileLoggerDidFull:(QCloudFileLogger *)logger
115120
{
121+
if (logger != _currentFileLogger) {
122+
return;
123+
}
116124
NSString* nextLogPath = [self avilableLogFilePath];
117125
if (_currentFileLogger.isFull) {
118126
QCloudFileLogger * fileLogger =[[QCloudFileLogger alloc] initWithPath:nextLogPath maxSize:QCloudEachLogFileSize];
@@ -207,7 +215,22 @@ - (void)logMessageWithLevel:(QCloudLogLevel)level
207215
QCloudLogModel* model = CreateLog();
208216
NSLog(@"%@",[model debugDescription]);
209217
}
210-
[self.currentFileLogger appendLog:CreateLog];
218+
for (QCloudLoggerOutput* output in _loggerOutputs) {
219+
[output appendLog:CreateLog];
220+
}
211221
va_end(args);
212222
}
223+
224+
- (void) addLogger:(QCloudLoggerOutput *)output
225+
{
226+
if (!output) {
227+
return;
228+
}
229+
[_loggerOutputs addObject:output];
230+
}
231+
232+
- (void) removeLogger:(QCloudLoggerOutput *)output
233+
{
234+
[_loggerOutputs removeObject:output];
235+
}
213236
@end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// QCloudLoggerOutput.h
3+
// QCloudCore
4+
//
5+
// Created by Dong Zhao on 2018/5/29.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
@class QCloudLogModel;
10+
@interface QCloudLoggerOutput : NSObject
11+
- (void) appendLog:(QCloudLogModel*(^)(void))logCreate;
12+
@end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// QCloudLoggerOutput.m
3+
// QCloudCore
4+
//
5+
// Created by Dong Zhao on 2018/5/29.
6+
//
7+
8+
#import "QCloudLoggerOutput.h"
9+
10+
@implementation QCloudLoggerOutput
11+
- (void) appendLog:(QCloudLogModel *(^)(void))logCreate
12+
{
13+
[NSException exceptionWithName:@"com.qcloud.logger" reason:@"You must implementation this method in subclass of QCloudLoggerOutput" userInfo:nil];
14+
}
15+
@end

0 commit comments

Comments
 (0)