Skip to content

Commit

Permalink
Add external log output
Browse files Browse the repository at this point in the history
  • Loading branch information
erichmzhang committed Jun 8, 2018
1 parent c0a1b58 commit bb822b9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
5 changes: 2 additions & 3 deletions QCloudCore/Classes/Logger/QCloudFileLogger.h
Expand Up @@ -7,19 +7,18 @@
//

#import <Foundation/Foundation.h>

#import "QCloudLoggerOutput.h"
@class QCloudFileLogger;
@protocol QCloudFileLoggerDelegate <NSObject>
- (void) fileLoggerDidFull:(QCloudFileLogger*)logger;
@end

@class QCloudLogModel;
@interface QCloudFileLogger : NSObject
@interface QCloudFileLogger : QCloudLoggerOutput
@property (nonatomic, weak) id<QCloudFileLoggerDelegate> delegate;
@property (nonatomic, strong, readonly) NSString* path;
@property (nonatomic, assign, readonly) uint64_t maxSize;
@property (nonatomic, assign, readonly) uint64_t currentSize;
@property (nonatomic, assign, readonly) BOOL isFull;
- (instancetype) initWithPath:(NSString *)path maxSize:(uint64_t)maxSize;
- (void) appendLog:(QCloudLogModel*(^)(void))logCreate;
@end
27 changes: 24 additions & 3 deletions QCloudCore/Classes/Logger/QCloudLogger.h
Expand Up @@ -8,6 +8,7 @@

#import <Foundation/Foundation.h>
#import "QCloudLogModel.h"
#import "QCloudLoggerOutput.h"

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

@property (nonatomic, assign) float keepDays;



///--------------------------------------
#pragma mark - Shared Logger
///--------------------------------------

/**
A shared instance of `PFLogger` that should be used for all logging.
A shared instance of `QCloudLogger` that should be used for all logging.
@return An shared singleton instance of `PFLogger`.
@return An shared singleton instance of `QCloudLogger`.
*/
+ (instancetype)sharedLogger; //TODO: (nlutsenko) Convert to use an instance everywhere instead of a shared singleton.
+ (instancetype)sharedLogger;

///--------------------------------------
#pragma mark - Logging Messages
Expand All @@ -67,4 +71,21 @@ QCloudLog(QCloudLogLevelDebug,@"%@",[NSThread callStackSymbols])
line:(int)line
file:(const char*)file
format:(NSString *)format, ...;



/**
增加一个输出源
@param output 输出源
*/
- (void) addLogger:(QCloudLoggerOutput*)output;


/**
删除一个输出源
@param output 删除一个输出源
*/
- (void) removeLogger:(QCloudLoggerOutput*)output;
@end
25 changes: 24 additions & 1 deletion QCloudCore/Classes/Logger/QCloudLogger.m
Expand Up @@ -54,6 +54,9 @@ @interface QCloudLogger () <QCloudFileLoggerDelegate>
@end

@implementation QCloudLogger
{
NSMutableArray* _loggerOutputs;
}

+ (instancetype)sharedLogger
{
Expand Down Expand Up @@ -85,8 +88,10 @@ - (instancetype) init
if (!self) {
return self;
}
_loggerOutputs = [NSMutableArray new];
_currentFileLogger = [[QCloudFileLogger alloc] initWithPath:[self avilableLogFilePath] maxSize:QCloudEachLogFileSize];
_currentFileLogger.delegate = self;
[_loggerOutputs addObject:_currentFileLogger];
_maxStoarageSize = 70*1024*1024;
_keepDays = 3;
//
Expand All @@ -113,6 +118,9 @@ - (instancetype) init

- (void) fileLoggerDidFull:(QCloudFileLogger *)logger
{
if (logger != _currentFileLogger) {
return;
}
NSString* nextLogPath = [self avilableLogFilePath];
if (_currentFileLogger.isFull) {
QCloudFileLogger * fileLogger =[[QCloudFileLogger alloc] initWithPath:nextLogPath maxSize:QCloudEachLogFileSize];
Expand Down Expand Up @@ -207,7 +215,22 @@ - (void)logMessageWithLevel:(QCloudLogLevel)level
QCloudLogModel* model = CreateLog();
NSLog(@"%@",[model debugDescription]);
}
[self.currentFileLogger appendLog:CreateLog];
for (QCloudLoggerOutput* output in _loggerOutputs) {
[output appendLog:CreateLog];
}
va_end(args);
}

- (void) addLogger:(QCloudLoggerOutput *)output
{
if (!output) {
return;
}
[_loggerOutputs addObject:output];
}

- (void) removeLogger:(QCloudLoggerOutput *)output
{
[_loggerOutputs removeObject:output];
}
@end
12 changes: 12 additions & 0 deletions QCloudCore/Classes/Logger/QCloudLoggerOutput.h
@@ -0,0 +1,12 @@
//
// QCloudLoggerOutput.h
// QCloudCore
//
// Created by Dong Zhao on 2018/5/29.
//

#import <Foundation/Foundation.h>
@class QCloudLogModel;
@interface QCloudLoggerOutput : NSObject
- (void) appendLog:(QCloudLogModel*(^)(void))logCreate;
@end
15 changes: 15 additions & 0 deletions QCloudCore/Classes/Logger/QCloudLoggerOutput.m
@@ -0,0 +1,15 @@
//
// QCloudLoggerOutput.m
// QCloudCore
//
// Created by Dong Zhao on 2018/5/29.
//

#import "QCloudLoggerOutput.h"

@implementation QCloudLoggerOutput
- (void) appendLog:(QCloudLogModel *(^)(void))logCreate
{
[NSException exceptionWithName:@"com.qcloud.logger" reason:@"You must implementation this method in subclass of QCloudLoggerOutput" userInfo:nil];
}
@end

0 comments on commit bb822b9

Please sign in to comment.