Skip to content

Commit

Permalink
Added Log Delegate for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
kober32 committed May 27, 2024
1 parent 187c9c2 commit b5a5943
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
29 changes: 28 additions & 1 deletion docs/PowerAuth-SDK-for-iOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2038,9 +2038,36 @@ PowerAuthLogSetVerbose(true)
```
<!-- begin box warning -->
Note that the functions above are effective only if PowerAuth SDK is compiled in the `DEBUG` build configuration.
Note that the functions above are effective only if PowerAuth SDK is compiled in the `DEBUG` build configuration or `ENABLE_PA2_LOG` compilation flag is set.
<!-- end -->
You can intercept the log and log it into your own report system, you can do so with `PowerAuthLogDelegate`.
```swift
import PowerAuth2
class MyClass: PowerAuthLogDelegate {
init {
// Only works in DEBUG build or when
// ENABLE_PA2_LOG compilation flag is set
PowerAuthLogSetEnabled(true)
#if DEBUG
// verbose logging should be used only in debug builds
// as it can contain sensitive information
PowerAuthLogSetVerbose(true)
#endif
PowerAuthLogSetDelegate(self)
}
// MARK: - PowerAuthLogDelegate implementation
func powerAuthLog(_ log: String) {
// Process the log...
}
}
```
## Additional Features
PowerAuth SDK for iOS contains multiple additional features that are useful for mobile apps.
Expand Down
32 changes: 27 additions & 5 deletions proj-xcode/PowerAuth2/PowerAuthLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
PowerAuthLog(...) macro prints a debug information into the debug console and is used internally
in the PowerAuth SDK. For DEBUG builds, the macro is expanded to internal function which uses NSLog().
For RELEASE builds, the message is completely suppressed during the compilation.
For RELEASE builds, the message is suppressed if `ENABLE_PA2_LOG` is not used.
*/
#define PowerAuthLog(...) PowerAuthLogImpl(__VA_ARGS__)

Expand All @@ -52,25 +52,25 @@

/**
Function enables or disables internal PowerAuth SDK logging.
Note that it's effective only when library is compiled in DEBUG build configuration.
Note that it's effective only when library is compiled in `DEBUG` build configuration or `ENABLE_PA2_LOG` compilation flag is set.
*/
PA2_EXTERN_C void PowerAuthLogSetEnabled(BOOL enabled);

/**
Function returns YES if internal PowerAuth SDK logging is enabled.
Note that when library is compiled in RELEASE configuration, then always returns NO.
Note that when library is compiled in `RELEASE` configuration and when `ENABLE_PA2_LOG` compilation flag is missing, then always returns NO.
*/
PA2_EXTERN_C BOOL PowerAuthLogIsEnabled(void);

/**
Function sets internal PowerAuth SDK logging to more verbose mode.
Note that it's effective only when library is compiled in DEBUG build configuration.
Note that it's effective only when library is compiled in `DEBUG` build configuration or `ENABLE_PA2_LOG` compilation flag is set.
*/
PA2_EXTERN_C void PowerAuthLogSetVerbose(BOOL verbose);

/**
Function returns YES if internal PowerAuth SDK logging is more talkative than usual.
Note that when library is compiled in RELEASE configuration, then always returns NO.
Note that when library is compiled in `RELEASE` configuration and when `ENABLE_PA2_LOG` compilation flag is missing, then always returns NO.
*/
PA2_EXTERN_C BOOL PowerAuthLogIsVerbose(void);

Expand All @@ -80,3 +80,25 @@ PA2_EXTERN_C BOOL PowerAuthLogIsVerbose(void);
console and cannot be supressed by configuration.
*/
PA2_EXTERN_C void PowerAuthCriticalWarning(NSString * format, ...);

/**
Protocol that represents a delegate that can tap into the library logs and use them for example
to report to a online system or to a logfile for user to send with some report.
Delegate will be called only when `ENABLE_PA2_LOG` copilation flag is set or the library is compiled
in the `DEBUG` mode (can be verified with `PowerAuthLogIsEnabled()`.
By default, all logs are also logged via NSLog.
*/
@protocol PowerAuthLogDelegate
/**
Log message reported by the library.
*/
- (void) powerAuthLog:(nonnull NSString*)log;
@end

/**
Function sets log delegate for further log processing.
Note that it's effective only when library is compiled in `DEBUG` build configuration or `ENABLE_PA2_LOG` compilation flag is set.
*/
PA2_EXTERN_C void PowerAuthLogSetDelegate(id<PowerAuthLogDelegate> _Nullable delegate);
12 changes: 12 additions & 0 deletions proj-xcode/PowerAuth2/PowerAuthLog.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#ifdef ENABLE_PA2_LOG
static BOOL s_log_enabled = NO;
static BOOL s_log_verbose = NO;
static id<PowerAuthLogDelegate> s_log_delegate = nil;
void PowerAuthLogImpl(NSString * format, ...)
{
if (!s_log_enabled) {
Expand All @@ -38,6 +39,10 @@ void PowerAuthLogImpl(NSString * format, ...)
NSString * message = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);

if (s_log_delegate) {
[s_log_delegate powerAuthLog:message];
}

NSLog(@"[PowerAuth] %@", message);
}
#endif // ENABLE_PA2_LOG
Expand Down Expand Up @@ -86,3 +91,10 @@ void PowerAuthCriticalWarning(NSString * format, ...)

NSLog(@"[PowerAuth] CRITICAL WARNING: %@", message);
}

void PowerAuthLogSetDelegate(id<PowerAuthLogDelegate> delegate)
{
#ifdef ENABLE_PA2_LOG
s_log_delegate = delegate;
#endif
}

0 comments on commit b5a5943

Please sign in to comment.