forked from NMSSH/NMSSH
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
189 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,83 @@ | ||
#ifndef NMSSH_DISABLE_LOGGING | ||
#ifdef LOG_VERBOSE | ||
#define NMSSH_LOG_CONTEXT 22 | ||
|
||
#define NMSSHLogError(frmt, ...) SYNC_LOG_OBJC_MAYBE(ddLogLevel, LOG_FLAG_ERROR, NMSSH_LOG_CONTEXT, frmt, ##__VA_ARGS__) | ||
#define NMSSHLogWarn(frmt, ...) ASYNC_LOG_OBJC_MAYBE(ddLogLevel, LOG_FLAG_WARN, NMSSH_LOG_CONTEXT, frmt, ##__VA_ARGS__) | ||
#define NMSSHLogInfo(frmt, ...) ASYNC_LOG_OBJC_MAYBE(ddLogLevel, LOG_FLAG_INFO, NMSSH_LOG_CONTEXT, frmt, ##__VA_ARGS__) | ||
#define NMSSHLogVerbose(frmt, ...) ASYNC_LOG_OBJC_MAYBE(ddLogLevel, LOG_FLAG_VERBOSE, NMSSH_LOG_CONTEXT, frmt, ##__VA_ARGS__) | ||
#else | ||
#define NMSSHLogError(frmt, ...) NSLog(frmt, ##__VA_ARGS__) | ||
#define NMSSHLogWarn(frmt, ...) NSLog(frmt, ##__VA_ARGS__) | ||
#define NMSSHLogInfo(frmt, ...) NSLog(frmt, ##__VA_ARGS__) | ||
#define NMSSHLogVerbose(frmt, ...) NSLog(frmt, ##__VA_ARGS__) | ||
#endif | ||
#else | ||
#define NMSSHLogError(frmt, ...) | ||
#define NMSSHLogWarn(frmt, ...) | ||
#define NMSSHLogInfo(frmt, ...) | ||
#define NMSSHLogVerbose(frmt, ...) | ||
#endif | ||
#define NMSSHLogVerbose(frmt, ...) [[NMSSHLogger logger] logVerbose:[NSString stringWithFormat:frmt, ##__VA_ARGS__]] | ||
#define NMSSHLogInfo(frmt, ...) [[NMSSHLogger logger] logInfo:[NSString stringWithFormat:frmt, ##__VA_ARGS__]] | ||
#define NMSSHLogWarn(frmt, ...) [[NMSSHLogger logger] logWarn:[NSString stringWithFormat:frmt, ##__VA_ARGS__]] | ||
#define NMSSHLogError(frmt, ...) [[NMSSHLogger logger] logError:[NSString stringWithFormat:frmt, ##__VA_ARGS__]] | ||
|
||
typedef NS_OPTIONS(NSUInteger, NMSSHLogLevel) { | ||
NMSSHLogLevelVerbose = (1 << 0 | 1 << 1 | 1 << 2 | 1 << 3), | ||
NMSSHLogLevelInfo = (1 << 1 | 1 << 2 | 1 << 3), | ||
NMSSHLogLevelWarn = (1 << 2 | 1 << 3), | ||
NMSSHLogLevelError = (1 << 3) | ||
}; | ||
|
||
/** | ||
NMSSHLogger provides the functionality to customize the framework logging. | ||
*/ | ||
@interface NMSSHLogger : NSObject | ||
|
||
/// ---------------------------------------------------------------------------- | ||
/// @name Retrieve the shared logger | ||
/// ---------------------------------------------------------------------------- | ||
|
||
/** | ||
* Retrieve the shared logger instance | ||
* | ||
* @returns Shared logger | ||
*/ | ||
+ (NMSSHLogger *)logger; | ||
|
||
/// ---------------------------------------------------------------------------- | ||
/// @name Logger settings | ||
/// ---------------------------------------------------------------------------- | ||
|
||
/** The block called to print the log message. | ||
* | ||
* The default implementation print the log | ||
* message using NSLog. | ||
* | ||
* The block takes two argument: | ||
* | ||
* _level_ - Log level<br> | ||
* _format_ - Log message | ||
*/ | ||
@property (nonatomic, copy) void (^logBlock)(NMSSHLogLevel level, NSString *format); | ||
|
||
/** The maximum log level */ | ||
@property (nonatomic, assign) NMSSHLogLevel logLevel; | ||
|
||
/** Enable or disable the logging feature */ | ||
@property (nonatomic, assign, getter = isEnabled) BOOL enabled; | ||
|
||
/// ---------------------------------------------------------------------------- | ||
/// @name Logging | ||
/// ---------------------------------------------------------------------------- | ||
|
||
/** | ||
* Log with verbose level | ||
* | ||
* @param format Log message | ||
*/ | ||
- (void)logVerbose:(NSString *)format; | ||
|
||
/** | ||
* Log with info level | ||
* | ||
* @param format Log message | ||
*/ | ||
- (void)logInfo:(NSString *)format; | ||
|
||
/** | ||
* Log with warn level | ||
* | ||
* @param format Log message | ||
*/ | ||
- (void)logWarn:(NSString *)format; | ||
|
||
/** | ||
* Log with error level | ||
* | ||
* @param format Log message | ||
*/ | ||
- (void)logError:(NSString *)format; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#import "NMSSHLogger.h" | ||
|
||
typedef NS_OPTIONS(NSUInteger, NMSSHLogFlag) { | ||
NMSSHLogFlagVerbose = (1 << 0), | ||
NMSSHLogFlagInfo = (1 << 1), | ||
NMSSHLogFlagWarn = (1 << 2), | ||
NMSSHLogFlagError = (1 << 3) | ||
}; | ||
|
||
@interface NMSSHLogger () | ||
|
||
@property (nonatomic, copy) void (^logBlockBackup)(NMSSHLogLevel level, NSString *format); | ||
|
||
@end | ||
|
||
@implementation NMSSHLogger | ||
|
||
// ----------------------------------------------------------------------------- | ||
#pragma mark - INITIALIZE THE LOGGER INSTANCE | ||
// ----------------------------------------------------------------------------- | ||
|
||
+ (NMSSHLogger *)logger { | ||
static NMSSHLogger *logger; | ||
|
||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
logger = [[NMSSHLogger alloc] init]; | ||
}); | ||
|
||
return logger; | ||
} | ||
|
||
- (id)init { | ||
if ((self = [super init])) { | ||
[self setEnabled:YES]; | ||
[self setLogLevel:NMSSHLogLevelVerbose]; | ||
[self setLogBlock:^(NMSSHLogLevel level, NSString *format) { | ||
NSLog(@"%@", format); | ||
}]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
#pragma mark - LOGGER SETTINGS | ||
// ----------------------------------------------------------------------------- | ||
|
||
- (void)setEnabled:(BOOL)enabled { | ||
if (enabled == _enabled) { | ||
return; | ||
} | ||
|
||
_enabled = enabled; | ||
|
||
if (enabled) { | ||
[self setLogBlock:self.logBlockBackup]; | ||
} else { | ||
[self setLogBlock:^(NMSSHLogLevel level, NSString *format) {}]; | ||
} | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
#pragma mark - LOGGING | ||
// ----------------------------------------------------------------------------- | ||
|
||
- (void)log:(NSString *)format level:(NMSSHLogLevel)level flag:(NMSSHLogFlag)flag { | ||
if (flag & self.logLevel) { | ||
self.logBlock(level, format); | ||
} | ||
} | ||
|
||
- (void)logVerbose:(NSString *)format { | ||
[self log:format level:NMSSHLogLevelVerbose flag:NMSSHLogFlagVerbose]; | ||
} | ||
|
||
- (void)logInfo:(NSString *)format{ | ||
[self log:format level:NMSSHLogLevelInfo flag:NMSSHLogFlagInfo]; | ||
} | ||
|
||
- (void)logWarn:(NSString *)format{ | ||
[self log:format level:NMSSHLogLevelWarn flag:NMSSHLogFlagWarn]; | ||
} | ||
|
||
- (void)logError:(NSString *)format{ | ||
[self log:format level:NMSSHLogLevelError flag:NMSSHLogFlagError]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,6 @@ | |
|
||
#import "NMSSHSession.h" | ||
#import "NMSSHChannel.h" | ||
#import "NMSFTP.h" | ||
#import "NMSFTP.h" | ||
|
||
#import "NMSSHLogger.h" |