mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-24 21:33:16 +08:00
新增 console.trace(), console.time(), console.timeEnd()
新增 console.setGlobalLogConfig()支持保存日志文件
This commit is contained in:
parent
237dbe0f50
commit
0f4f1da208
Binary file not shown.
@ -57,6 +57,9 @@ dependencies {
|
||||
api 'com.github.Stericson:RootShell:1.6'
|
||||
// Gson
|
||||
api 'com.google.code.gson:gson:2.8.2'
|
||||
// log4j
|
||||
api group: 'de.mindpipe.android', name: 'android-logging-log4j', version: '1.0.3'
|
||||
api group: 'log4j', name: 'log4j', version: '1.2.17'
|
||||
// Terminal emulator
|
||||
api(name: 'libtermexec-release', ext: 'aar')
|
||||
api(name: 'emulatorview-release', ext: 'aar')
|
||||
|
||||
@ -1,43 +1,82 @@
|
||||
|
||||
module.exports = function(__runtime__, scope){
|
||||
module.exports = function (__runtime__, scope) {
|
||||
var rtConsole = __runtime__.console;
|
||||
var console = {};
|
||||
|
||||
console.assert = function(value, message){
|
||||
console.assert = function (value, message) {
|
||||
message = message || "";
|
||||
rtConsole.assertTrue(value, message);
|
||||
}
|
||||
|
||||
console.rawInput = rtConsole.rawInput.bind(rtConsole);
|
||||
|
||||
console.input = function(data, param){
|
||||
console.input = function (data, param) {
|
||||
return eval(console.rawInput.call(console, [].slice(arguments)) + "");
|
||||
}
|
||||
|
||||
console.log = function(){
|
||||
console.log = function () {
|
||||
rtConsole.log(util.format.apply(util, arguments));
|
||||
}
|
||||
|
||||
console.verbose = function(){
|
||||
console.verbose = function () {
|
||||
rtConsole.verbose(util.format.apply(util, arguments));
|
||||
}
|
||||
|
||||
console.print = function(){
|
||||
console.print = function () {
|
||||
rtConsole.print(android.util.Log.DEBUG, util.format.apply(util, arguments));
|
||||
}
|
||||
|
||||
console.info = function(){
|
||||
console.info = function () {
|
||||
rtConsole.info(util.format.apply(util, arguments));
|
||||
}
|
||||
|
||||
console.warn = function(){
|
||||
console.warn = function () {
|
||||
rtConsole.warn(util.format.apply(util, arguments));
|
||||
}
|
||||
|
||||
console.error = function(){
|
||||
console.error = function () {
|
||||
rtConsole.error(util.format.apply(util, arguments));
|
||||
}
|
||||
|
||||
var timers = {}, ascu = android.os.SystemClock.uptimeMillis;
|
||||
console.time = console.time || function (label) {
|
||||
label = label || "default";
|
||||
timers[label] = ascu();
|
||||
}
|
||||
|
||||
console.timeEnd = console.timeEnd || function (label) {
|
||||
label = label || "default";
|
||||
var result = ascu() - timers[label];
|
||||
delete timers[label];
|
||||
console.log(label + ": " + result + "ms");
|
||||
}
|
||||
|
||||
console.trace = console.trace || function captureStack(message) {
|
||||
var k = {};
|
||||
Error.captureStackTrace(k, captureStack);
|
||||
console.log(util.format.apply(util, arguments) + "\n" + k.stack);
|
||||
};
|
||||
|
||||
console.setGlobalLogConfig = function (config) {
|
||||
let logConfigurator = new Packages["de.mindpipe.android.logging.log4j"].LogConfigurator();
|
||||
if (config.file) {
|
||||
logConfigurator.setFileName(files.path(config.file));
|
||||
logConfigurator.setUseFileAppender(true);
|
||||
}
|
||||
logConfigurator.setFilePattern(option(config.filePattern, "%m%n"))
|
||||
logConfigurator.setMaxFileSize(option(config.maxFileSize, 512 * 1024));
|
||||
logConfigurator.setImmediateFlush(option(config.immediateFlush, true));
|
||||
let rootLevel = option(config.rootLevel, "ALL");
|
||||
logConfigurator.setRootLevel(org.apache.log4j.Level[rootLevel.toUpperCase()]);
|
||||
logConfigurator.setMaxBackupSize(option(config.maxBackupSize, 5));
|
||||
logConfigurator.setResetConfiguration(option(config.resetConfiguration, true));
|
||||
logConfigurator.configure();
|
||||
}
|
||||
|
||||
function option(value, def) {
|
||||
return value == undefined ? def : value;
|
||||
}
|
||||
|
||||
console.show = rtConsole.show.bind(rtConsole);
|
||||
console.hide = rtConsole.hide.bind(rtConsole);
|
||||
console.clear = rtConsole.clear.bind(rtConsole);
|
||||
|
||||
@ -1,19 +1,24 @@
|
||||
package com.stardust.autojs.core.console;
|
||||
|
||||
import android.util.SparseArray;
|
||||
|
||||
import com.stardust.util.UiHandler;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.log4j.Priority;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import de.mindpipe.android.logging.log4j.LogConfigurator;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/10/22.
|
||||
*/
|
||||
|
||||
public class GlobalStardustConsole extends StardustConsole {
|
||||
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault());
|
||||
private static final Logger LOGGER = Logger.getLogger(GlobalStardustConsole.class);
|
||||
|
||||
public GlobalStardustConsole(UiHandler uiHandler) {
|
||||
super(uiHandler);
|
||||
@ -23,10 +28,29 @@ public class GlobalStardustConsole extends StardustConsole {
|
||||
public String println(int level, CharSequence charSequence) {
|
||||
String log = String.format(Locale.getDefault(), "%s/%s: %s",
|
||||
DATE_FORMAT.format(new Date()), getLevelChar(level), charSequence.toString());
|
||||
LOGGER.log(toLog4jLevel(level), log);
|
||||
super.println(level, log);
|
||||
return log;
|
||||
}
|
||||
|
||||
private Priority toLog4jLevel(int level) {
|
||||
switch (level) {
|
||||
case android.util.Log.VERBOSE:
|
||||
return Level.DEBUG;
|
||||
case android.util.Log.DEBUG:
|
||||
return Level.DEBUG;
|
||||
case android.util.Log.INFO:
|
||||
return Level.INFO;
|
||||
case android.util.Log.WARN:
|
||||
return Level.WARN;
|
||||
case android.util.Log.ERROR:
|
||||
return Level.ERROR;
|
||||
case android.util.Log.ASSERT:
|
||||
return Level.FATAL;
|
||||
}
|
||||
throw new IllegalArgumentException("invalid level = " + level);
|
||||
}
|
||||
|
||||
private String getLevelChar(int level) {
|
||||
switch (level) {
|
||||
case android.util.Log.VERBOSE:
|
||||
|
||||
@ -213,6 +213,7 @@ public class StardustConsole extends AbstractConsole {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void setSize(int w, int h) {
|
||||
if (mShown) {
|
||||
mUiHandler.post(() -> {
|
||||
|
||||
@ -43,4 +43,5 @@ public interface Console {
|
||||
String println(int level, CharSequence charSequence);
|
||||
|
||||
void setTitle(CharSequence title);
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user