feat:Doric cli show doric logs

This commit is contained in:
pengfeizhou 2021-02-05 19:36:25 +08:00 committed by osborn
parent d4c8d08660
commit ddc74a9027
7 changed files with 196 additions and 20 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -60,12 +60,20 @@ export default async function dev() {
await delay(3000);
console.warn("Start watching");
server.listen(7777);
const cachedContents: Record<string, string> = {}
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);
}
});

View File

@ -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
}

View File

@ -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;
}

View File

@ -0,0 +1,16 @@
//
// DoricDevMonitor.h
// DoricDevkit
//
// Created by pengfei.zhou on 2021/2/5.
//
#import <Foundation/Foundation.h>
#import "DoricMonitorProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@interface DoricDevMonitor : NSObject <DoricMonitorProtocol>
@end
NS_ASSUME_NONNULL_END

View File

@ -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