Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Log scan switch (#2524)
Browse files Browse the repository at this point in the history
* [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
wqyfavor authored and jianhan-he committed Jun 11, 2019
1 parent f2b2e69 commit d857f37
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 3 deletions.
12 changes: 12 additions & 0 deletions ios/sdk/WeexSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@
74EF31C31DE6935600667A07 /* WXURLRewriteTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 74EF31C21DE6935600667A07 /* WXURLRewriteTests.m */; };
74F7BFF51DC782EC004D0871 /* testRootView.js in Resources */ = {isa = PBXBuildFile; fileRef = 74F7BFF41DC782EC004D0871 /* testRootView.js */; };
74FD6E041C7C0E9600DBEB6D /* WXScrollerProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 74FD6E031C7C0E9600DBEB6D /* WXScrollerProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; };
7704894822AA358500E7606F /* WXConsoleLogModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 7704894622AA358400E7606F /* WXConsoleLogModule.h */; };
7704894922AA358500E7606F /* WXConsoleLogModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 7704894622AA358400E7606F /* WXConsoleLogModule.h */; };
7704894A22AA358500E7606F /* WXConsoleLogModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 7704894722AA358400E7606F /* WXConsoleLogModule.m */; };
7704894B22AA358500E7606F /* WXConsoleLogModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 7704894722AA358400E7606F /* WXConsoleLogModule.m */; };
7715EB6221A69DD9001F1108 /* WXRichText.h in Headers */ = {isa = PBXBuildFile; fileRef = 7715EB6021A69DD8001F1108 /* WXRichText.h */; settings = {ATTRIBUTES = (Public, ); }; };
7715EB6321A69DD9001F1108 /* WXRichText.h in Headers */ = {isa = PBXBuildFile; fileRef = 7715EB6021A69DD8001F1108 /* WXRichText.h */; settings = {ATTRIBUTES = (Public, ); }; };
7715EB6421A69DD9001F1108 /* WXRichText.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7715EB6121A69DD9001F1108 /* WXRichText.mm */; };
Expand Down Expand Up @@ -1095,6 +1099,8 @@
74EF31C21DE6935600667A07 /* WXURLRewriteTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WXURLRewriteTests.m; sourceTree = "<group>"; };
74F7BFF41DC782EC004D0871 /* testRootView.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = testRootView.js; sourceTree = "<group>"; };
74FD6E031C7C0E9600DBEB6D /* WXScrollerProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXScrollerProtocol.h; sourceTree = "<group>"; };
7704894622AA358400E7606F /* WXConsoleLogModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXConsoleLogModule.h; sourceTree = "<group>"; };
7704894722AA358400E7606F /* WXConsoleLogModule.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WXConsoleLogModule.m; sourceTree = "<group>"; };
7715EB6021A69DD8001F1108 /* WXRichText.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXRichText.h; sourceTree = "<group>"; };
7715EB6121A69DD9001F1108 /* WXRichText.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WXRichText.mm; sourceTree = "<group>"; };
775BEE4D1C16F993008D1629 /* WXDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXDefine.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1848,6 +1854,8 @@
77E659D71C07F585008B8775 /* Module */ = {
isa = PBXGroup;
children = (
7704894622AA358400E7606F /* WXConsoleLogModule.h */,
7704894722AA358400E7606F /* WXConsoleLogModule.m */,
BA5F00EF1FC5AFFE00F76B5C /* WXLocaleModule.h */,
BA5F00F01FC5AFFE00F76B5C /* WXLocaleModule.m */,
DCE2CF981F46D4220021BDC4 /* WXVoiceOverModule.m */,
Expand Down Expand Up @@ -2488,6 +2496,7 @@
B8D66C8121255730003960BD /* render_type.h in Headers */,
B8D66CA121255730003960BD /* WeexApiHeader.h in Headers */,
B8D66CA921255730003960BD /* wson_util.h in Headers */,
7704894822AA358500E7606F /* WXConsoleLogModule.h in Headers */,
B8D66C8B21255730003960BD /* render_list_factory.h in Headers */,
745B2D681E5A8E1E0092D38A /* WXMultiColumnLayout.h in Headers */,
B89543F720EB18B5006EAD63 /* WXCoreBridge.h in Headers */,
Expand Down Expand Up @@ -2546,6 +2555,7 @@
B8D66C3621255730003960BD /* render_action_move_element.h in Headers */,
DCA445A31EFA570800D0CFA8 /* WXSDKManager.h in Headers */,
DCA445BE1EFA57BB00D0CFA8 /* WXComponentManager.h in Headers */,
7704894922AA358500E7606F /* WXConsoleLogModule.h in Headers */,
DCA4459E1EFA56E500D0CFA8 /* WXUtility.h in Headers */,
77A3D66F222F9F5B0078A6F5 /* WeexApiValue.h in Headers */,
DCA445B91EFA579D00D0CFA8 /* WXErrorView.h in Headers */,
Expand Down Expand Up @@ -3008,6 +3018,7 @@
742AD7321DF98C45007DC46C /* WXResourceRequestHandlerDefaultImpl.m in Sources */,
74CFDD421F45941E007A1A66 /* WXRecycleListTemplateManager.m in Sources */,
747DF6831E31AEE4005C53A8 /* WXLength.m in Sources */,
7704894A22AA358500E7606F /* WXConsoleLogModule.m in Sources */,
17E5ACE2209211BD00EE81F1 /* WXTransition.mm in Sources */,
77E65A0E1C155E99008B8775 /* WXDivComponent.m in Sources */,
B8D66CAB21255730003960BD /* wson_util.cpp in Sources */,
Expand Down Expand Up @@ -3212,6 +3223,7 @@
B8D66C4C21255730003960BD /* render_action_remove_event.cpp in Sources */,
74B81AE41F73C3E500D3A61D /* WXRecycleListComponent.mm in Sources */,
B8D66C6421255730003960BD /* render_text.cpp in Sources */,
7704894B22AA358500E7606F /* WXConsoleLogModule.m in Sources */,
DC7764951F3C685200B5727E /* WXRecyclerDragController.m in Sources */,
B89543F420EB18B5006EAD63 /* WXCoreBridge.mm in Sources */,
DCA4453D1EFA55B300D0CFA8 /* WXRootView.m in Sources */,
Expand Down
1 change: 1 addition & 0 deletions ios/sdk/WeexSDK/Sources/Engine/WXSDKEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ + (void)_registerDefaultModules
[self registerModule:@"meta" withClass:NSClassFromString(@"WXMetaModule")];
[self registerModule:@"webSocket" withClass:NSClassFromString(@"WXWebSocketModule")];
[self registerModule:@"voice-over" withClass:NSClassFromString(@"WXVoiceOverModule")];
[self registerModule:@"sdk-console-log" withClass:NSClassFromString(@"WXConsoleLogModule")];
}

+ (void)registerModule:(NSString *)name withClass:(Class)clazz
Expand Down
2 changes: 1 addition & 1 deletion ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ - (void)addComponent:(NSString*)ref
if (supercomponent.ignoreInteraction) {
component.ignoreInteraction = YES;
}
component.ignoreInteraction = [[component.attributes objectForKey:@"ignoreInteraction"] boolValue];
component.ignoreInteraction = [[attributes objectForKey:@"ignoreInteraction"] boolValue];

#ifdef DEBUG
WXLogDebug(@"flexLayout -> _recursivelyAddComponent : super:(%@,%@):[%f,%f] ,child:(%@,%@):[%f,%f],childClass:%@",
Expand Down
27 changes: 27 additions & 0 deletions ios/sdk/WeexSDK/Sources/Module/WXConsoleLogModule.h
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
118 changes: 118 additions & 0 deletions ios/sdk/WeexSDK/Sources/Module/WXConsoleLogModule.m
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
2 changes: 2 additions & 0 deletions ios/sdk/WeexSDK/Sources/Utility/WXLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ typedef NS_ENUM(NSUInteger, WeexLogLevel){

+ (void)registerExternalLog:(id<WXLogProtocol>)externalLog;

+ (id<WXLogProtocol>)getCurrentExternalLog;

@end

#define WX_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
Expand Down
8 changes: 6 additions & 2 deletions ios/sdk/WeexSDK/Sources/Utility/WXLog.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ + (void)log:(WXLogFlag)flag file:(const char *)fileName line:(NSUInteger)line me
{
NSString *flagString;
switch (flag) {
case WXLogFlagError: {
case WXLogFlagError:
flagString = @"error";
}
break;
case WXLogFlagWarning:
flagString = @"warn";
Expand Down Expand Up @@ -231,6 +230,11 @@ + (void)registerExternalLog:(id<WXLogProtocol>)externalLog
_externalLog = externalLog;
}

+ (id<WXLogProtocol>)getCurrentExternalLog
{
return _externalLog;
}

@end

#pragma mark - WXSafeLog
Expand Down

0 comments on commit d857f37

Please sign in to comment.