Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): fix ConsoleModule.log not working #2812

Merged
merged 3 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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