Skip to content

Commit

Permalink
New logging feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Frugghi committed Oct 22, 2013
1 parent f2b77e4 commit 7c86097
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 27 deletions.
12 changes: 8 additions & 4 deletions NMSSH-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
18A0966717D6AA3D008B76FB /* NMSSHLogger.h in Headers */ = {isa = PBXBuildFile; fileRef = 18A0966417D6AA3D008B76FB /* NMSSHLogger.h */; };
18A0966817D6AA3D008B76FB /* socket_helper.h in Headers */ = {isa = PBXBuildFile; fileRef = 18A0966517D6AA3D008B76FB /* socket_helper.h */; };
18A0966917D6AA3D008B76FB /* socket_helper.m in Sources */ = {isa = PBXBuildFile; fileRef = 18A0966617D6AA3D008B76FB /* socket_helper.m */; };
18A0967117D6AA51008B76FB /* NMSFTP.h in Headers */ = {isa = PBXBuildFile; fileRef = 18A0966A17D6AA51008B76FB /* NMSFTP.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -114,13 +113,14 @@
18A0972117D6AA7B008B76FB /* libcrypto.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 18A096D017D6AA7B008B76FB /* libcrypto.a */; };
18A0972217D6AA7C008B76FB /* libssh2.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 18A096D117D6AA7B008B76FB /* libssh2.a */; };
18A0972317D6AA7C008B76FB /* libssl.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 18A096D217D6AA7B008B76FB /* libssl.a */; };
18F1A2D218158D78000635AB /* NMSSHLogger.h in Headers */ = {isa = PBXBuildFile; fileRef = 18F1A2D018158D78000635AB /* NMSSHLogger.h */; settings = {ATTRIBUTES = (Public, ); }; };
18F1A2D318158D78000635AB /* NMSSHLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 18F1A2D118158D78000635AB /* NMSSHLogger.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
18A0963017D6A85F008B76FB /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
18A0964F17D6A8C4008B76FB /* NMSSH.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NMSSH.framework; sourceTree = BUILT_PRODUCTS_DIR; };
18A0965817D6A8C4008B76FB /* NMSSH-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NMSSH-Prefix.pch"; sourceTree = "<group>"; };
18A0966417D6AA3D008B76FB /* NMSSHLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NMSSHLogger.h; sourceTree = "<group>"; };
18A0966517D6AA3D008B76FB /* socket_helper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = socket_helper.h; sourceTree = "<group>"; };
18A0966617D6AA3D008B76FB /* socket_helper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = socket_helper.m; sourceTree = "<group>"; };
18A0966A17D6AA51008B76FB /* NMSFTP.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NMSFTP.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -213,6 +213,8 @@
18A096D117D6AA7B008B76FB /* libssh2.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libssh2.a; sourceTree = "<group>"; };
18A096D217D6AA7B008B76FB /* libssl.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libssl.a; sourceTree = "<group>"; };
18A0972417D6AAA8008B76FB /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = usr/lib/libz.dylib; sourceTree = SDKROOT; };
18F1A2D018158D78000635AB /* NMSSHLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NMSSHLogger.h; sourceTree = "<group>"; };
18F1A2D118158D78000635AB /* NMSSHLogger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NMSSHLogger.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -284,9 +286,10 @@
18A0966317D6AA3D008B76FB /* Config */ = {
isa = PBXGroup;
children = (
18A0966417D6AA3D008B76FB /* NMSSHLogger.h */,
18A0966517D6AA3D008B76FB /* socket_helper.h */,
18A0966617D6AA3D008B76FB /* socket_helper.m */,
18F1A2D018158D78000635AB /* NMSSHLogger.h */,
18F1A2D118158D78000635AB /* NMSSHLogger.m */,
);
path = Config;
sourceTree = "<group>";
Expand Down Expand Up @@ -433,9 +436,9 @@
18A0967617D6AA51008B76FB /* NMSSHSession.h in Headers */,
18A0967B17D6AA64008B76FB /* NMSSHChannelDelegate.h in Headers */,
18A0967C17D6AA64008B76FB /* NMSSHSessionDelegate.h in Headers */,
18F1A2D218158D78000635AB /* NMSSHLogger.h in Headers */,
18A096D317D6AA7B008B76FB /* libssh2.h in Headers */,
18A096D517D6AA7B008B76FB /* libssh2_sftp.h in Headers */,
18A0966717D6AA3D008B76FB /* NMSSHLogger.h in Headers */,
18A0966817D6AA3D008B76FB /* socket_helper.h in Headers */,
18A096D417D6AA7B008B76FB /* libssh2_publickey.h in Headers */,
18A096D617D6AA7B008B76FB /* aes.h in Headers */,
Expand Down Expand Up @@ -613,6 +616,7 @@
18A0966917D6AA3D008B76FB /* socket_helper.m in Sources */,
18A0967217D6AA51008B76FB /* NMSFTP.m in Sources */,
18A0967517D6AA51008B76FB /* NMSSHChannel.m in Sources */,
18F1A2D318158D78000635AB /* NMSSHLogger.m in Sources */,
18A0967717D6AA51008B76FB /* NMSSHSession.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 6 additions & 0 deletions NMSSH.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
18E4D2391815F6F600432102 /* NMSSHLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E4D2381815F6F600432102 /* NMSSHLogger.m */; };
18E4D23A1815F70D00432102 /* NMSSHLogger.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F1CBB217206D730025EBFC /* NMSSHLogger.h */; settings = {ATTRIBUTES = (Public, ); }; };
4A04ECAE174F51E8006DD8E7 /* NMSSHChannelDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 18F9DABF17302F7F004CECAA /* NMSSHChannelDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
E40D7BBF15DCCAFB002F4B41 /* libssh2.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E40D7BBE15DCCAFB002F4B41 /* libssh2.a */; };
E42815BC1593D13800CF680C /* YAML.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = E4E96DD9158FD65D002E6E0A /* YAML.framework */; };
Expand Down Expand Up @@ -73,6 +75,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
18E4D2381815F6F600432102 /* NMSSHLogger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NMSSHLogger.m; sourceTree = "<group>"; };
18F9DABF17302F7F004CECAA /* NMSSHChannelDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NMSSHChannelDelegate.h; sourceTree = "<group>"; };
E40D7BBE15DCCAFB002F4B41 /* libssh2.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libssh2.a; sourceTree = "<group>"; };
E42815BD1593D6E900CF680C /* NMSSHSessionTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NMSSHSessionTests.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -276,6 +279,7 @@
isa = PBXGroup;
children = (
E4F1CBB217206D730025EBFC /* NMSSHLogger.h */,
18E4D2381815F6F600432102 /* NMSSHLogger.m */,
E4F1CBB5172073AC0025EBFC /* socket_helper.h */,
E4F1CBB3172073A00025EBFC /* socket_helper.m */,
);
Expand All @@ -295,6 +299,7 @@
4A04ECAE174F51E8006DD8E7 /* NMSSHChannelDelegate.h in Headers */,
E4F1E680159F5B13007B0B2F /* NMSSHChannel.h in Headers */,
E48DA7BD15D0EB2800721060 /* NMSFTP.h in Headers */,
18E4D23A1815F70D00432102 /* NMSSHLogger.h in Headers */,
E428160C1596629A00CF680C /* libssh2_publickey.h in Headers */,
E428160D1596629A00CF680C /* libssh2_sftp.h in Headers */,
E428160E1596629A00CF680C /* libssh2.h in Headers */,
Expand Down Expand Up @@ -411,6 +416,7 @@
E42815C31593D95200CF680C /* NMSSHSession.m in Sources */,
E4F1E681159F5B13007B0B2F /* NMSSHChannel.m in Sources */,
E48DA7BE15D0EB2800721060 /* NMSFTP.m in Sources */,
18E4D2391815F6F600432102 /* NMSSHLogger.m in Sources */,
E4F1CBB4172073A00025EBFC /* socket_helper.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
103 changes: 83 additions & 20 deletions NMSSH/Config/NMSSHLogger.h
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
89 changes: 89 additions & 0 deletions NMSSH/Config/NMSSHLogger.m
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
2 changes: 0 additions & 2 deletions NMSSH/NMSSH-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#import <Foundation/Foundation.h>

@class NMSSHChannel, NMSSHSession, NMSFTP;
#import "NMSSHLogger.h"
#import "NMSSH.h"

#endif
4 changes: 3 additions & 1 deletion NMSSH/NMSSH.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@

#import "NMSSHSession.h"
#import "NMSSHChannel.h"
#import "NMSFTP.h"
#import "NMSFTP.h"

#import "NMSSHLogger.h"

0 comments on commit 7c86097

Please sign in to comment.