Skip to content

Commit

Permalink
chore(ios): demo log format
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifanyuan authored and hippy-actions[bot] committed Dec 29, 2023
1 parent 299e489 commit f67461d
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion examples/ios-demo/HippyDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,45 @@ @interface ViewController () <HippyBridgeDelegate, HippyMethodInterceptorProtoco

@end

static NSString *formatLog(NSDate *timestamp, HippyLogLevel level, NSString *fileName, NSNumber *lineNumber, NSString *message) {
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
];
}
}

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

HippySetLogFunction(^(HippyLogLevel level, HippyLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
NSLog(@"hippy says:%@ in file [%@:%d]", message, fileName, lineNumber.intValue);
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

0 comments on commit f67461d

Please sign in to comment.