This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [iOS] Support scan to redirect log to console. For release version, original NSLog may be blocked. * [iOS] Support scan to redirect log to console. For release version, original NSLog/printf may be blocked.
- Loading branch information
1 parent
f2b2e69
commit d857f37
Showing
7 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "WXModuleProtocol.h" | ||
|
||
/** | ||
Redirect and output log to console. | ||
*/ | ||
@interface WXConsoleLogModule : NSObject <WXModuleProtocol> | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#import "WXConsoleLogModule.h" | ||
#import "WXLog.h" | ||
|
||
#if !TARGET_OS_WATCH | ||
#import <asl.h> | ||
#endif | ||
|
||
#if !__has_feature(objc_arc) | ||
#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). | ||
#endif | ||
|
||
@interface WXConsoleLogHandler : NSObject<WXLogProtocol> | ||
|
||
@property (nonatomic, assign) WXLogLevel logLevel; | ||
|
||
+ (WXConsoleLogHandler *)sharedInstance; | ||
|
||
- (WXLogLevel)logLevel; | ||
|
||
- (void)log:(WXLogFlag)flag message:(NSString *)message; | ||
|
||
@end | ||
|
||
@implementation WXConsoleLogHandler | ||
|
||
+ (WXConsoleLogHandler *)sharedInstance | ||
{ | ||
static dispatch_once_t onceToken; | ||
static WXConsoleLogHandler* instance = nil; | ||
dispatch_once(&onceToken, ^{ | ||
instance = [[WXConsoleLogHandler alloc] init]; | ||
}); | ||
return instance; | ||
} | ||
|
||
- (instancetype)init | ||
{ | ||
if (self = [super init]) { | ||
_logLevel = WXLogLevelAll; | ||
} | ||
return self; | ||
} | ||
|
||
- (WXLogLevel)logLevel | ||
{ | ||
return _logLevel; | ||
} | ||
|
||
- (void)log:(WXLogFlag)flag message:(NSString *)message | ||
{ | ||
#if !TARGET_OS_WATCH | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
asl_log_message(ASL_LEVEL_NOTICE, "%s", [message UTF8String]); | ||
}); | ||
#else | ||
printf("\n%s\n", [message UTF8String]); | ||
#endif | ||
} | ||
|
||
@end | ||
|
||
@implementation WXConsoleLogModule | ||
|
||
WX_EXPORT_METHOD(@selector(switchLogLevel:callback:)) | ||
|
||
- (void)switchLogLevel:(NSString*)logLevel callback:(WXKeepAliveCallback)callback { | ||
static id<WXLogProtocol> OriginalLogger = nil; | ||
|
||
// When first invoke WXConsoleLogModule we record original logger | ||
if (OriginalLogger == nil) { | ||
OriginalLogger = [WXLog getCurrentExternalLog]; | ||
} | ||
|
||
if ([logLevel isEqualToString:@"off"]) { | ||
[WXLog registerExternalLog:OriginalLogger]; | ||
} | ||
else if ([logLevel isEqualToString:@"error"]) { | ||
[WXConsoleLogHandler sharedInstance].logLevel = WXLogLevelError; | ||
[WXLog registerExternalLog:[WXConsoleLogHandler sharedInstance]]; | ||
} | ||
else if ([logLevel isEqualToString:@"warning"]) { | ||
[WXConsoleLogHandler sharedInstance].logLevel = WXLogLevelWarning; | ||
[WXLog registerExternalLog:[WXConsoleLogHandler sharedInstance]]; | ||
} | ||
else if ([logLevel isEqualToString:@"info"]) { | ||
[WXConsoleLogHandler sharedInstance].logLevel = WXLogLevelInfo; | ||
[WXLog registerExternalLog:[WXConsoleLogHandler sharedInstance]]; | ||
} | ||
else if ([logLevel isEqualToString:@"debug"]) { | ||
[WXConsoleLogHandler sharedInstance].logLevel = WXLogLevelDebug; | ||
[WXLog registerExternalLog:[WXConsoleLogHandler sharedInstance]]; | ||
} | ||
|
||
if (callback) { | ||
callback(@{}, NO); | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters