Skip to content

Commit

Permalink
Pass js logs to java logs
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmanusta committed Dec 19, 2015
1 parent f7aeb63 commit 660c387
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
13 changes: 11 additions & 2 deletions conf/public/js/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ myWorker.onmessage = function (e) {
var data = (typeof e.data) == "string" ? JSON.parse(e.data) : e.data;

if (data.type == "log") {
afx.completeWebWorkerExceptionally(JSON.stringify(data.message), data.taskId);

var logLevel = data.level;

if (logLevel) {
if (logLevel == "error") {
afx.completeWebWorkerExceptionally(data.taskId);
}

afx[logLevel].call(afx, JSON.stringify(data.message));
}
}
else if (data.type == "afx") {
afx[data.func].apply(afx, data.parameters);
Expand All @@ -14,7 +23,7 @@ myWorker.onmessage = function (e) {

myWorker.onerror = function (e) {
var data = (typeof e) == "string" ? e : JSON.stringify(e);
afx.log(data);
afx["error"].call(afx, data);
};

myWorker.postMessage();
Expand Down
46 changes: 33 additions & 13 deletions conf/public/js/webworker.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
function sendConsole(message, taskId) {
function sendConsole(message, level) {
self.postMessage(JSON.stringify({
type: "log",
level: level,
message: message,
taskId: taskId || lastTaskId
taskId: lastTaskId
}));
}
var console = {
log: sendConsole,
debug: sendConsole,
warn: sendConsole,
error: sendConsole,
info: sendConsole

var console = {};

console.log = function (msg) {
sendConsole(msg, "log");
};

console.debug = function (msg) {
sendConsole(msg, "debug");
};

console.warn = function (msg) {
sendConsole(msg, "warn");
};

console.error = function (msg) {
sendConsole(msg, "error");
};

console.info = function (msg) {
sendConsole(msg, "info");
};

var afx = {};
Expand All @@ -29,10 +45,18 @@ importScripts("/afx/resource/js/asciidoctor-deck.js");
importScripts("/afx/resource/js/outliner.js");
importScripts("/afx/resource/js/webworker-converters.js");

self.onerror = function (e) {
console.error(e);
};

var lastTaskId = "";
self.onmessage = function (e) {
try {

if (!e.data) {
return;
}

var data = JSON.parse(e.data) || {};

var func = data.func;
Expand All @@ -47,10 +71,6 @@ self.onmessage = function (e) {

}
catch (e) {
console.log(e, lastTaskId);
console.error(e);
}
};

self.onerror = function (e) {
console.log(e);
};
50 changes: 25 additions & 25 deletions src/main/java/com/kodcu/controller/ApplicationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1908,13 +1908,14 @@ private Map<String, String> parseChartOptions(String options) {
}

@WebkitCall(from = "converter.js")
public void completeWebWorkerExceptionally(Object message, Object taskId) {
public void completeWebWorkerExceptionally(Object taskId) {
threadService.runTaskLater(() -> {
final Map<String, CompletableFuture<ConverterResult>> workerTasks = asciidocWebkitConverter.getWebWorkerTasks();
Optional.ofNullable(workerTasks.get(taskId))
.filter(c -> !c.isDone())
.ifPresent(c -> {
c.completeExceptionally(new RuntimeException(String.format("Task: %s is not completed - %s", taskId, message)));
final RuntimeException ex = new RuntimeException(String.format("Task: %s is not completed", taskId));
c.completeExceptionally(ex);
});
workerTasks.remove(taskId);
});
Expand Down Expand Up @@ -2196,26 +2197,6 @@ public void adjustSplitPane() {
}
}

@WebkitCall
public void debug(String message) {
logger.debug(message);
}

@WebkitCall
public void error(String message) {
logger.error(message);
}

@WebkitCall
public void info(String message) {
logger.info(message);
}

@WebkitCall
public void warn(String message) {
logger.warn(message);
}

public void saveDoc() {
current.currentTab().saveDoc();
}
Expand Down Expand Up @@ -2361,10 +2342,29 @@ public ShortcutProvider getShortcutProvider() {
return shortcutProvider;
}

public void log(Object... objects) {
for (Object object : objects) {
@WebkitCall
public void log(Object object) {
debug(object);
}

@WebkitCall
public void debug(Object object) {
logger.debug(object + "");
}

@WebkitCall
public void warn(Object object) {
logger.warn(object + "");
}

@WebkitCall
public void info(Object object) {
logger.info(object + "");
}

@WebkitCall
public void error(Object object) {
logger.error(object + "");
}
}

@FXML
Expand Down

0 comments on commit 660c387

Please sign in to comment.