Skip to content

Commit

Permalink
fix(ios): set logging delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifanyuan authored and hippy-actions[bot] committed Nov 13, 2023
1 parent 54e9a86 commit 85b7853
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@

static NSString *const engineKey = @"Demo";

static NSString *formatLog(NSDate *timestamp, HippyLogLevel level, NSString *fileName, NSNumber *lineNumber, NSString *message) {
static NSArray *logLevelMap = @[@"TRACE", @"INFO", @"WARN", @"ERROR", @"FATAL"];
static NSDateFormatter *formatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [NSDateFormatter new];
formatter.dateFormat = formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";
});

NSString *levelStr = level < 0 || level > logLevelMap.count ? logLevelMap[1] : logLevelMap[level];

if(fileName){
return [[NSString alloc] initWithFormat:@"[%@][%@:%d][%@]%@",
[formatter stringFromDate:timestamp],
fileName.lastPathComponent,
lineNumber.intValue,
levelStr,
message
];
}else{
return [[NSString alloc] initWithFormat:@"[%@]%@",
[formatter stringFromDate:timestamp],
message
];
}
}

@interface HippyDemoViewController () <HippyMethodInterceptorProtocol, HippyBridgeDelegate, HippyRootViewDelegate> {
DriverType _driverType;
RenderType _renderType;
Expand Down Expand Up @@ -99,7 +126,12 @@ - (void)viewDidLoad {

- (void)registerLogFunction {
HippySetLogFunction(^(HippyLogLevel level, HippyLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
NSLog(@"hippy says:%@ in file %@ at line %@", message, fileName, lineNumber);
NSString *log = formatLog([NSDate date], level, fileName, lineNumber, message);
if([log hasSuffix:@"\n"]){
fprintf(stderr, "%s", log.UTF8String);
}else{
fprintf(stderr, "%s\n", log.UTF8String);
}
});
}

Expand Down
6 changes: 3 additions & 3 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ - (void)loadBundleURL:(NSURL *)bundleURL
}
return;
}
HippyLogInfo(@"Begin loading bundle(%s) at %s", HP_CSTR_NOT_NULL(bundleURL.absoluteString.lastPathComponent.UTF8String), HP_CSTR_NOT_NULL(bundleURL.absoluteString.UTF8String));
HippyLogInfo(@"[HP PERF] Begin loading bundle(%s) at %s", HP_CSTR_NOT_NULL(bundleURL.absoluteString.lastPathComponent.UTF8String), HP_CSTR_NOT_NULL(bundleURL.absoluteString.UTF8String));
[_bundleURLs addObject:bundleURL];
dispatch_async(HippyBridgeQueue(), ^{
[self beginLoadingBundle:bundleURL completion:completion];
Expand Down Expand Up @@ -509,15 +509,15 @@ - (void)loadInstanceForRootView:(NSNumber *)rootTag withProperties:(NSDictionary
- (void)innerLoadInstanceForRootView:(NSNumber *)rootTag withProperties:(NSDictionary *)props {
HippyAssert(_moduleName, @"module name must not be null");
HippyLogInfo(@"[Hippy_OC_Log][Life_Circle],Running application %@ (%@)", _moduleName, props);
HippyLogInfo(@"Begin loading instance for HippyBridge(%p)", self);
HippyLogInfo(@"[HP PERF] Begin loading instance for HippyBridge(%p)", self);
NSDictionary *param = @{@"name": _moduleName,
@"id": rootTag,
@"params": props ?: @{},
@"version": _HippySDKVersion};
footstone::value::HippyValue value = [param toHippyValue];
std::shared_ptr<footstone::value::HippyValue> domValue = std::make_shared<footstone::value::HippyValue>(value);
self.javaScriptExecutor.pScope->LoadInstance(domValue);
HippyLogInfo(@"End loading instance for HippyBridge(%p)", self);
HippyLogInfo(@"[HP PERF] End loading instance for HippyBridge(%p)", self);
}

- (void)rootViewSizeChangedEvent:(NSNumber *)tag params:(NSDictionary *)params {
Expand Down
3 changes: 2 additions & 1 deletion modules/footstone/include/footstone/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ class LogMessage {
}

std::ostringstream s;
s<<"thread:"<<pthread_self()<<", "<<log_msg<<std::endl;
s<<"["<<file<<":"<<line<<"]"
<<"[thread:"<<pthread_self()<<"], "<<log_msg<<std::endl;

if (LogMessage::delegate_) {
delegate_(s, TDF_LOG_WARNING);
Expand Down
7 changes: 7 additions & 0 deletions modules/ios/base/HippyLog.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "HippyLog.h"
#import "HippyBridge.h"
#import "HippyRedBox.h"
#include "footstone/logging.h"

#pragma mark NativeLog Methods

Expand Down Expand Up @@ -62,6 +63,12 @@ void HippySetLogThreshold(HippyLogLevel threshold) {

void HippySetLogFunction(HippyLogFunction logFunction) {
HPCurrentLogFunction = logFunction;

footstone::log::LogMessage::InitializeDelegate([](const std::ostringstream& stream,
footstone::log::LogSeverity severity) {
NSString* message = [NSString stringWithUTF8String:stream.str().c_str()];
HPCurrentLogFunction(HippyLogLevelInfo, HippyLogSourceNative, nil, nil, message);
});
}

HippyLogFunction HippyGetLogFunction() {
Expand Down

0 comments on commit 85b7853

Please sign in to comment.