octo-deno/lib/logging/console.js

85 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-04-22 22:49:49 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var websocket = null
2017-04-22 22:49:49 +00:00
var logger = (function () { // eslint-disable-line no-unused-vars
2017-05-02 14:17:36 +00:00
const sep = '\n/* ### NEXT TESTCASE ############################## */'
2017-05-02 14:26:49 +00:00
const color = {
red: '\u0033[1;31m',
green: '\u0033[1;32m',
clear: '\u0033[0m'
}
2017-04-25 17:59:34 +00:00
if (utils.platform.isWindows) {
2017-05-02 14:26:49 +00:00
color.red = ''
color.green = ''
color.clear = ''
2017-04-22 22:49:49 +00:00
}
function console (msg) {
2017-04-22 22:49:49 +00:00
if (websocket) {
websocket.send(msg)
2017-04-22 22:49:49 +00:00
}
2017-04-25 15:22:15 +00:00
if (typeof window === 'undefined') {
print(msg) // eslint-disable-line no-undef
2017-04-22 22:49:49 +00:00
} else if (window.dump) {
window.dump(msg)
2017-04-22 22:49:49 +00:00
} else if (window.console && window.console.log) {
window.console.log(msg)
2017-04-22 22:49:49 +00:00
} else {
throw new Error('Unable to run console logger.')
2017-04-22 22:49:49 +00:00
}
}
function dump (msg) {
console(msg)
2017-04-22 22:49:49 +00:00
}
function testcase (msg) {
dump('/*L*/ ' + JSON.stringify(msg) + '\n')
2017-04-22 22:49:49 +00:00
}
function dumpln (msg) {
dump(msg + '\n')
2017-04-22 22:49:49 +00:00
}
function error (msg) {
dumpln(color.red + msg + color.clear)
2017-04-22 22:49:49 +00:00
}
function JSError (msg) {
2017-04-22 22:49:49 +00:00
error(comment(msg))
}
function comment (msg) {
return '/* ' + msg + ' */'
2017-04-22 22:49:49 +00:00
}
function separator () {
dumpln(color.green + sep + color.clear)
2017-04-22 22:49:49 +00:00
}
function traceback () {
error('===[ Traceback ]')
2017-04-22 22:49:49 +00:00
try {
throw new Error()
2017-04-22 22:49:49 +00:00
} catch (e) {
dump(e.stack || e.stacktrace || '')
2017-04-22 22:49:49 +00:00
}
error('===')
2017-04-22 22:49:49 +00:00
}
return {
console: console,
dump: dump,
error: error,
JSError: JSError,
dumpln: dumpln,
comment: comment,
testcase: testcase,
separator: separator,
traceback: traceback
}
})()