octo-deno/lib/logging/console.js

88 lines
1.8 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/. */
const utils = require('../utils')
let websocket = null
2017-04-22 22:49:49 +00:00
const sep = '\n/* ### NEXT TESTCASE ############################## */'
const color = {
red: '\u{1b}[1;31m',
green: '\u{1b}[1;32m',
clear: '\u{1b}[0m'
}
if (utils.platform.name.isWindows) {
color.red = ''
color.green = ''
color.clear = ''
}
class logger {
static 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') {
try {
print(msg) // eslint-disable-line no-undef
} catch (e) {
console.log(msg)
}
2017-04-22 22:49:49 +00:00
} else if (window.dump) {
2018-03-19 04:54:40 +00:00
dump(msg) // eslint-disable-line no-undef
2017-04-22 22:49:49 +00:00
} else if (window.console && window.console.log) {
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
}
}
static dump (msg) {
this.console(msg)
2017-04-22 22:49:49 +00:00
}
static dumpln (msg) {
this.dump(`${msg}\n`)
2017-06-28 00:16:34 +00:00
}
static log (msg) {
this.dumpln(`/*L*/ ${utils.script.safely(msg)}`)
}
static info (msg) {
this.dumpln(`/*L*/ /* ${msg} */`)
2017-04-22 22:49:49 +00:00
}
static error (msg) {
this.dumpln(color.red + msg + color.clear)
2017-04-22 22:49:49 +00:00
}
static ok (msg) { // eslint-disable-line no-unused-vars
this.dumpln(color.green + msg + color.green)
2017-06-10 06:35:35 +00:00
}
static JSError (msg) {
this.error(`/* ERROR: ${msg} */`)
2017-04-22 22:49:49 +00:00
}
static comment (msg) {
this.dumpln(`/*L*/ // ${msg}`)
2017-04-22 22:49:49 +00:00
}
static separator () {
this.dumpln(color.green + sep + color.clear)
2017-04-22 22:49:49 +00:00
}
static traceback () {
this.error('===[ Traceback ] ===')
2017-04-22 22:49:49 +00:00
try {
throw new Error()
2017-04-22 22:49:49 +00:00
} catch (e) {
this.dump(e.stack || e.stacktrace || '')
2017-04-22 22:49:49 +00:00
}
}
}
2017-04-22 22:49:49 +00:00
2018-03-19 04:54:40 +00:00
module.exports = logger