From ddc74a902725d4f637a4ceb6c9529d04b6b71c3c Mon Sep 17 00:00:00 2001 From: pengfeizhou Date: Fri, 5 Feb 2021 19:36:25 +0800 Subject: [PATCH] feat:Doric cli show doric logs --- .../main/java/pub/doric/devkit/DevKit.java | 13 ++++ .../pub/doric/devkit/DoricDevMonitor.java | 70 +++++++++++++++++++ doric-cli/src/dev.ts | 40 ++++++----- doric-cli/src/server.ts | 6 +- doric-iOS/Devkit/Classes/DoricDev.m | 16 +++++ doric-iOS/Devkit/Classes/DoricDevMonitor.h | 16 +++++ doric-iOS/Devkit/Classes/DoricDevMonitor.m | 55 +++++++++++++++ 7 files changed, 196 insertions(+), 20 deletions(-) create mode 100644 doric-android/devkit/src/main/java/pub/doric/devkit/DoricDevMonitor.java create mode 100644 doric-iOS/Devkit/Classes/DoricDevMonitor.h create mode 100644 doric-iOS/Devkit/Classes/DoricDevMonitor.m diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DevKit.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DevKit.java index 4326bddf..8281be03 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/DevKit.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DevKit.java @@ -9,9 +9,12 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; + import pub.doric.Doric; import pub.doric.DoricContext; import pub.doric.DoricContextManager; +import pub.doric.DoricLibrary; +import pub.doric.DoricRegistry; import pub.doric.devkit.event.ConnectExceptionEvent; import pub.doric.devkit.event.EOFExceptionEvent; import pub.doric.devkit.event.EnterDebugEvent; @@ -31,6 +34,16 @@ public class DevKit implements IDevKit { } private DevKit() { + Doric.registerLibrary(new DoricLibrary() { + @Override + public void load(DoricRegistry registry) { + registry.registerMonitor(new DoricDevMonitor()); + } + }); + for (DoricContext context : DoricContextManager.aliveContexts()) { + context.getDriver().getRegistry().registerMonitor(new DoricDevMonitor()); + break; + } EventBus.getDefault().register(this); } diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDevMonitor.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDevMonitor.java new file mode 100644 index 00000000..d09b059e --- /dev/null +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDevMonitor.java @@ -0,0 +1,70 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed 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. + */ +package pub.doric.devkit; + +import android.util.Log; + +import com.google.gson.JsonObject; + +import java.io.StringWriter; + +import pub.doric.DoricContext; +import pub.doric.IDoricMonitor; +import pub.doric.utils.DoricLog; + +/** + * @Description: pub.doric.devkit + * @Author: pengfei.zhou + * @CreateDate: 2/5/21 + */ +public class DoricDevMonitor implements IDoricMonitor { + @Override + public void onException(DoricContext context, Exception e) { + if (!DoricDev.getInstance().isInDevMode()) { + return; + } + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("source", "In source file: " + (context != null ? context.getSource() : "Unknown")); + StringWriter stringWriter = new StringWriter(); + jsonObject.addProperty("exception", stringWriter.toString()); + DevKit.getInstance().sendDevCommand(IDevKit.Command.EXCEPTION, jsonObject); + } + + @Override + public void onLog(int type, String message) { + if (!DoricDev.getInstance().isInDevMode()) { + return; + } + String typeString = "DEFAULT"; + switch (type) { + case Log.ERROR: + DoricLog.suffix_e("_js", message); + typeString = "ERROR"; + break; + case Log.WARN: + DoricLog.suffix_w("_js", message); + typeString = "WARN"; + break; + default: + DoricLog.suffix_d("_js", message); + break; + } + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("type", typeString); + jsonObject.addProperty("message", message); + DevKit.getInstance().sendDevCommand(IDevKit.Command.LOG, jsonObject); + } +} diff --git a/doric-cli/src/dev.ts b/doric-cli/src/dev.ts index 2bf1e066..8385c65f 100644 --- a/doric-cli/src/dev.ts +++ b/doric-cli/src/dev.ts @@ -60,12 +60,20 @@ export default async function dev() { await delay(3000); console.warn("Start watching"); server.listen(7777); + const cachedContents: Record = {} chokidar .watch(process.cwd() + "/bundle", { ignored: /.*?\.map/, alwaysStat: true, }) .on("change", (jsFile) => { + const content = fs.readFileSync(jsFile, "utf-8"); + if (cachedContents[jsFile]) { + if (content.indexOf(cachedContents[jsFile]) >= 0) { + return; + } + } + cachedContents[jsFile] = content; console.log("*******", jsFile.replace(process.cwd(), "") .replace("/bundle/src/", "") .replace(".js", "") @@ -74,23 +82,21 @@ export default async function dev() { console.log("debugging, hot reload by pass"); return; } - fs.readFile(jsFile, "utf-8", (error, data) => { - try { - const sourceMap = mergeMap(`${jsFile}.map`); - server.connections.forEach((e: any) => { - e.sendText( - JSON.stringify({ - cmd: "RELOAD", - script: data, - source: (jsFile.match(/[^/\\]*$/) || [""])[0], - sourceMap, - }) - ); - }); - } catch (e) { - console.error(e); - } - }); + try { + const sourceMap = mergeMap(`${jsFile}.map`); + server.connections.forEach((e: any) => { + e.sendText( + JSON.stringify({ + cmd: "RELOAD", + script: content, + source: (jsFile.match(/[^/\\]*$/) || [""])[0], + sourceMap, + }) + ); + }); + } catch (e) { + console.error(e); + } }); diff --git a/doric-cli/src/server.ts b/doric-cli/src/server.ts index 013449dd..83616d42 100644 --- a/doric-cli/src/server.ts +++ b/doric-cli/src/server.ts @@ -50,11 +50,11 @@ export async function createServer() { break; case 'LOG': if (resultObject.data.type == 'DEFAULT') { - console.log((resultObject.data.message as string).green); + console.log(`>>>>>>${(resultObject.data.message as string).green}>>>>>>`); } else if (resultObject.data.type == 'ERROR') { - console.log((resultObject.data.message as string).red); + console.log(`>>>>>>${(resultObject.data.message as string).red}>>>>>>`); } else if (resultObject.data.type == 'WARN') { - console.log((resultObject.data.message as string).yellow); + console.log(`>>>>>>${(resultObject.data.message as string).yellow}>>>>>>`); } break } diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index 9e765f6c..710eda62 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -28,6 +28,16 @@ #import "DoricWSClient.h" #import "DoricDebugDriver.h" #import "DoricDevViewController.h" +#import "DoricDevMonitor.h" + +@interface DoricDevLibrary : DoricLibrary +@end + +@implementation DoricDevLibrary +- (void)load:(DoricRegistry *)registry { + +} +@end @interface DoricDev () @property(nonatomic, strong) DoricWSClient *wsclient; @@ -46,6 +56,12 @@ - (instancetype)init { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEnterDebugEvent) name:@"EnterDebugEvent" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onStopDebugEvent) name:@"StopDebugEvent" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDebuggerReadyEvent) name:@"DebuggerReadyEvent" object:nil]; + [Doric registerLibrary:[DoricDevLibrary new]]; + NSValue *value = DoricContextManager.instance.aliveContexts.firstObject; + if (value) { + DoricContext *context = value.nonretainedObjectValue; + [context.driver.registry registerMonitor:[DoricDevMonitor new]]; + } } return self; } diff --git a/doric-iOS/Devkit/Classes/DoricDevMonitor.h b/doric-iOS/Devkit/Classes/DoricDevMonitor.h new file mode 100644 index 00000000..55c29233 --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDevMonitor.h @@ -0,0 +1,16 @@ +// +// DoricDevMonitor.h +// DoricDevkit +// +// Created by pengfei.zhou on 2021/2/5. +// + +#import +#import "DoricMonitorProtocol.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface DoricDevMonitor : NSObject +@end + +NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricDevMonitor.m b/doric-iOS/Devkit/Classes/DoricDevMonitor.m new file mode 100644 index 00000000..a90ac42d --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDevMonitor.m @@ -0,0 +1,55 @@ +// +// DoricDevMonitor.m +// DoricDevkit +// +// Created by pengfei.zhou on 2021/2/5. +// + +#import "DoricDevMonitor.h" + +#import "DoricDev.h" +#import "NSString+JsonString.h" +#import "Doric.h" + +@implementation DoricDevMonitor +- (void)onException:(NSException *)exception inContext:(DoricContext *)context { + if (!DoricDev.instance.isInDevMode) { + return; + } + NSDictionary *jsonDic = @{ + @"cmd": @"EXCEPTION", + @"data": @{ + @"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"], + @"exception": exception.reason + } + }; + + NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; + [[DoricDev instance] sendDevCommand:jsonStr]; +} + +- (void)onLog:(DoricLogType)type message:(NSString *)message { + if (!DoricDev.instance.isInDevMode) { + return; + } + NSString *typeString = @"DEFAULT"; + if (type == DoricLogTypeDebug) { + typeString = @"DEFAULT"; + } else if (type == DoricLogTypeWarning) { + typeString = @"WARN"; + } else if (type == DoricLogTypeError) { + typeString = @"ERROR"; + } + + NSDictionary *jsonDic = @{ + @"cmd": @"LOG", + @"data": @{ + @"type": typeString, + @"message": message + } + }; + + NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; + [[DoricDev instance] sendDevCommand:jsonStr]; +} +@end