Skip to content

Commit

Permalink
fix(ios): fix ConsoleModule.log not working
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg authored and ozonelmy committed Dec 21, 2022
1 parent 17e668f commit 8a3fd40
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 33 deletions.
15 changes: 9 additions & 6 deletions core/third_party/base/src/platform/ios/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ namespace {
const char* const kLogSeverityNames[TDF_LOG_NUM_SEVERITIES] = {"INFO", "WARNING", "ERROR", "FATAL"};

const char* GetNameForLogSeverity(LogSeverity severity) {
if (severity >= LOG_INFO && severity < TDF_LOG_NUM_SEVERITIES) return kLogSeverityNames[severity];
if (severity >= TDF_LOG_INFO && severity < TDF_LOG_NUM_SEVERITIES) {
return kLogSeverityNames[severity];
}
return "UNKNOWN";
}

Expand Down Expand Up @@ -65,12 +67,13 @@ std::mutex LogMessage::mutex_;
LogMessage::LogMessage(LogSeverity severity, const char* file, int line, const char* condition)
: severity_(severity), file_(file), line_(line) {
stream_ << "[";
if (severity >= LOG_INFO)
if (severity >= TDF_LOG_INFO) {
stream_ << GetNameForLogSeverity(severity);
else
stream_ << "VERBOSE" << -severity;
stream_ << ":" << (severity > LOG_INFO ? StripDots(file_) : StripPath(file_)) << "(" << line_
<< ")] ";
} else {
stream_ << "VERBOSE" << -TDF_LOG_FATAL;
}
stream_ << ":" << (severity > TDF_LOG_FATAL ? StripDots(file_) : StripPath(file_))
<< "(" << line_ << ")] ";

if (condition) stream_ << "Check failed: " << condition << ". ";
}
Expand Down
5 changes: 5 additions & 0 deletions examples/hippy-react-demo/src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
StyleSheet,
View,
Text,
ConsoleModule,
} from '@hippy/react';
import HomeEntry from './pages/entry';
import RemoteDebug from './pages/remote-debug';
Expand Down Expand Up @@ -42,6 +43,10 @@ export default class App extends Component {
});
}

componentDidMount() {
ConsoleModule.log('~~~~~~~~~~~~~~~~~ This is a log from ConsoleModule ~~~~~~~~~~~~~~~~~');
}

render() {
const { pageIndex } = this.state;
const { __instanceId__: instanceId } = this.props;
Expand Down
2 changes: 1 addition & 1 deletion examples/ios-demo/HippyDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (void)viewDidLoad {
// Do any additional setup after loading the view.

HippySetLogFunction(^(HippyLogLevel level, HippyLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
NSLog(@"hippy says:%@ in file %@ at line %@", message, fileName, lineNumber);
NSLog(@"hippy says:%@ in file [%@:%d]", message, fileName, lineNumber.intValue);
});


Expand Down
29 changes: 3 additions & 26 deletions ios/sdk/utils/HippyLog.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
static HippyLogFunction HippyCurrentLogFunction;
static HippyLogLevel HippyCurrentLogThreshold = HippyDefaultLogThreshold;

static BOOL getFileNameAndLineNumberFromLogMessage(NSString *message, NSString **fileName, int *lineNumber);
static void registerTDFLogHandler() {
static std::once_flag flag;
std::call_once(flag, [](){
Expand All @@ -60,39 +59,17 @@ static void registerTDFLogHandler() {
std::string string = stream.str();
if (string.length()) {
NSString *message = [NSString stringWithUTF8String:string.c_str()];
NSString *fileName = nil;
int lineNumber = 0;
if (getFileNameAndLineNumberFromLogMessage(message, &fileName, &lineNumber)) {
_HippyLogNativeInternal(HippyLogLevelInfo, [fileName UTF8String], lineNumber, @"%@", message);
if (message == nil) { // fixme: deal with unicode characters
message = [NSString stringWithCString:string.c_str() encoding:NSASCIIStringEncoding];
}
_HippyLogNativeInternal(HippyLogLevelInfo, "TDFCore", 0, @"%@", message);
}
}
};
tdf::base::LogMessage::InitializeDelegate(logFunction);
});
}

static BOOL getFileNameAndLineNumberFromLogMessage(NSString *message, NSString **fileName, int *lineNumber) {
//[VERBOSE0:worker_task_runner.cc(84)] WorkerThread create
static NSString *prefixString = @"[VERBOSE0:";
@try {
if ([message hasPrefix:prefixString] && fileName && lineNumber) {
NSUInteger messageLength = [message length];
NSUInteger fileNameStartLocation = [prefixString length];
NSUInteger firstParenthesisPosition = [message rangeOfString:@"(" options:(0) range:NSMakeRange(fileNameStartLocation, messageLength - fileNameStartLocation)].location;
NSUInteger secondParenthesisPosition = [message rangeOfString:@")" options:(0) range:NSMakeRange(fileNameStartLocation, messageLength - fileNameStartLocation)].location;
NSString *name = [message substringWithRange:NSMakeRange(fileNameStartLocation, firstParenthesisPosition - fileNameStartLocation)];
NSString *line = [message substringWithRange:NSMakeRange(firstParenthesisPosition + 1, secondParenthesisPosition - firstParenthesisPosition - 1)];
*fileName = [name copy];
*lineNumber = [line intValue];
return YES;
}
} @catch (NSException *exception) {
return NO;
}
return NO;
}

HippyLogLevel HippyGetLogThreshold() {
return HippyCurrentLogThreshold;
}
Expand Down

0 comments on commit 8a3fd40

Please sign in to comment.