Skip to content

Commit

Permalink
Merge branch 'main' into feature/update-vue-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
open-hippy authored Sep 6, 2023
2 parents a9c52fc + f285fc7 commit ed0c225
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
13 changes: 13 additions & 0 deletions framework/ios/base/bridge/HippyBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ HP_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);

@end


@interface HippyBridge (RedBoxDebug)

/// The last current active bridge instance.
+ (instancetype)currentBridge;

/// Record the last active bridge instance.
/// - Parameter currentBridge: bridge instance, pass nil to reset.
+ (void)setCurrentBridge:(nullable HippyBridge *)currentBridge;

@end


HP_EXTERN void HippyBridgeFatal(NSError *, HippyBridge *);

HP_EXTERN void HippyBridgeHandleException(NSException *exception, HippyBridge *bridge);
Expand Down
28 changes: 28 additions & 0 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ - (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
HPExecuteOnMainThread(^{
[self bindKeys];
}, YES);
[HippyBridge setCurrentBridge:self];
HPLogInfo(@"[Hippy_OC_Log][Life_Circle],%@ Init %p", NSStringFromClass([self class]), self);
}
return self;
Expand Down Expand Up @@ -838,6 +839,10 @@ - (void)invalidate {
_moduleSetup = nil;
_startTime = footstone::TimePoint::SystemNow();
self.moduleSemaphore = nil;

if ([HippyBridge currentBridge] == self) {
[HippyBridge setCurrentBridge:nil];
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[jsExecutor executeBlockOnJavaScriptQueue:^{
@autoreleasepool {
Expand Down Expand Up @@ -994,3 +999,26 @@ void HippyBridgeFatal(NSError *error, HippyBridge *bridge) {
void HippyBridgeHandleException(NSException *exception, HippyBridge *bridge) {
HPHandleException(exception, bridge?@{@"bridge": bridge}:nil);
}


#pragma mark -

@implementation HippyBridge (RedBoxDebug)

static HippyBridge *HippyCurrentBridgeInstance = nil;

/**
* The last current active bridge instance. This is set automatically whenever
* the bridge is accessed. It can be useful for static functions or singletons
* that need to access the bridge for purposes such as logging, but should not
* be relied upon to return any particular instance, due to race conditions.
*/
+ (instancetype)currentBridge {
return HippyCurrentBridgeInstance;
}

+ (void)setCurrentBridge:(nullable HippyBridge *)currentBridge {
HippyCurrentBridgeInstance = currentBridge;
}

@end
8 changes: 8 additions & 0 deletions modules/ios/base/HPLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
#endif //#ifdef DEBUG
#endif //#ifndef HP_LOG_ENABLED

/**
* Thresholds for logs to display a redbox. You can override these values when debugging
* in order to tweak the default logging behavior.
*/
#ifndef HPLOG_REDBOX_LEVEL
#define HPLOG_REDBOX_LEVEL HPLogLevelError
#endif

/**
* Logging macros. Use these to log information, warnings and errors in your
* own code.
Expand Down
11 changes: 9 additions & 2 deletions modules/ios/base/HPLog.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
*/

#import "HPLog.h"

#import "HippyBridge.h"
#import "HippyRedBox.h"
#include <string>
#include <mutex>

Expand Down Expand Up @@ -165,7 +166,7 @@ void HPLogNativeInternal(HPLogLevel level, const char *fileName, int lineNumber,
va_end(args);
NSArray<NSDictionary *> *callStacks = nil;
#if HP_DEBUG
if (level >= HPLogLevelError) {
if (level >= HPLOG_REDBOX_LEVEL) {
NSArray<NSString *> *stackSymbols = [NSThread callStackSymbols];
NSMutableArray<NSDictionary *> *stack = [NSMutableArray arrayWithCapacity:(stackSymbols.count - 1)];
[stackSymbols enumerateObjectsUsingBlock:^(NSString *frameSymbols, NSUInteger idx, __unused BOOL *stop) {
Expand All @@ -182,6 +183,12 @@ void HPLogNativeInternal(HPLogLevel level, const char *fileName, int lineNumber,
}
}];
callStacks = [stack copy];

dispatch_async(dispatch_get_main_queue(), ^{
// red box is thread safe, but by deferring to main queue we avoid a startup
// race condition that causes the module to be accessed before it has loaded
[[HippyBridge currentBridge].redBox showErrorMessage:message withStack:stack];
});
}
#endif
// Call log function
Expand Down

0 comments on commit ed0c225

Please sign in to comment.