Allow levelled logging for debug

This is useful for suppressing spew, but still allowing easy debugging
when needed, without rebuilding.

To use, use excmd:

     setlogginglevel <prefix> <level>

Where the level is one of:

    - never:  never log
    - error
    - warning
    - info
    - debug:  most verbose

Output is directed to console methods "error", "warn", "log", "debug".
This commit is contained in:
John Beard 2017-12-04 14:14:18 +00:00 committed by Colin Caine
parent c493c06e55
commit baefd09973
6 changed files with 97 additions and 6 deletions

View file

@ -7,6 +7,7 @@ import * as Messaging from './messaging'
import * as SELF from './commandline_frame'
import './number.clamp'
import state from './state'
import * as Logging from "./logging"
let activeCompletions: Completions.CompletionSource[] = undefined
let completionsDiv = window.document.getElementById("completions") as HTMLElement
@ -146,7 +147,7 @@ clInput.addEventListener("keydown", function (keyevent) {
clInput.addEventListener("input", () => {
// Fire each completion and add a callback to resize area
console.log(activeCompletions)
Logging.log("cmdline", Logging.LEVEL.DEBUG)(activeCompletions)
activeCompletions.forEach(comp =>
comp.filter(clInput.value).then(() => resizeArea())
)

View file

@ -8,6 +8,7 @@
//
// Really, we'd like a way of just letting things use the variables
//
const CONFIGNAME = "userconfig"
type StorageMap = browser.storage.StorageMap
@ -124,6 +125,13 @@ const DEFAULTS = o({
"ttsrate": 1, // 0.1 to 10
"ttspitch": 1, // 0 to 2
"vimium-gi": true,
// Default logging levels - generally 2 == WARNING
"logging": o({
"message": 2,
"cmdline": 2,
"controller": 2,
}),
})
// currently only supports 2D or 1D storage

View file

@ -3,6 +3,7 @@ import {isTextEditable} from './dom'
import {isSimpleKey} from './keyseq'
import state from "./state"
import {repeat} from './excmds_background'
import * as Logging from "./logging"
import {parser as exmode_parser} from './parsers/exmode'
import {parser as hintmode_parser} from './hinting_background'
@ -41,7 +42,8 @@ function *ParserController () {
state.mode = "normal"
}
}
console.log(keyevent, state.mode)
Logging.log("controller", Logging.LEVEL.DEBUG)(
keyevent, state.mode)
// Special keys (e.g. Backspace) are not handled properly
// yet. So drop them. This also drops all modifier keys.
@ -61,7 +63,8 @@ function *ParserController () {
response = (parsers[state.mode] as any)([keyevent])
break
}
console.debug(keys, response)
Logging.log("controller", Logging.LEVEL.DEBUG)(
keys, response)
if (response.ex_str){
ex_str = response.ex_str

View file

@ -95,6 +95,8 @@ import * as CommandLineBackground from './commandline_background'
import * as DOM from './dom'
import * as config from './config'
//#background_helper
import * as Logging from "./logging"
/** @hidden */
@ -171,6 +173,33 @@ function tabSetActive(id: number) {
// }}}
// {{{ INTERNAL/DEBUG
/**
* Set the logging level for a given logging module.
*
* @param logModule the logging module to set the level on
* @param level the level to log at: in increasing verbosity, one of
* "never", "error", "warning", "info", "debug"
*/
//#background
export function loggingsetlevel(logModule: string, level: string) {
const map = {
"never": Logging.LEVEL.NEVER,
"error": Logging.LEVEL.ERROR,
"warning": Logging.LEVEL.WARNING,
"info": Logging.LEVEL.INFO,
"debug": Logging.LEVEL.DEBUG,
}
let newLevel = map[level]
if (newLevel !== undefined) {
config.set("logging", newLevel, logModule)
}
}
// }}}
// {{{ PAGE CONTEXT

48
src/logging.ts Normal file
View file

@ -0,0 +1,48 @@
/**
* Helper functions for logging
*/
import * as Config from "./config"
export enum LEVEL {
NEVER = 0, // don't use this in calls to log()
ERROR = 1,
WARNING = 2,
INFO = 3,
DEBUG = 4,
}
/**
* Config-aware logging function.
*
* @param logModule the logging module name: this is ued to look up the
* configured/default level in the user config
* @param level the level of the logging - if <= configured, the message
* will be shown
*
* @return logging function: this is returned as a function to
* retain the call site
*/
export function log(logModule: string, level: LEVEL) {
let configedLevel = Config.get("logging", logModule) || LEVEL.WARNING
if (level <= configedLevel) {
// hand over to console.log, error or debug as needed
switch (level) {
case LEVEL.ERROR:
return console.error
case LEVEL.WARNING:
return console.warn
case LEVEL.INFO:
return console.log
case LEVEL.DEBUG:
return console.debug
}
}
// do nothing with the message
return function(...args) {}
}

View file

@ -1,4 +1,5 @@
import {l, browserBg, activeTabId} from './lib/webext'
import * as Logging from './logging'
export type TabMessageType =
"excmd_content" |
@ -24,7 +25,8 @@ export type listener = (message: Message, sender?, sendResponse?) => void|Promis
// Calls methods on obj that match .command and sends responses back
export function attributeCaller(obj) {
function handler(message: Message, sender, sendResponse) {
console.log("Message:", message)
Logging.log("message", Logging.LEVEL.DEBUG)(message)
// Args may be undefined, but you can't spread undefined...
if (message.args === undefined) message.args = []
@ -35,13 +37,13 @@ export function attributeCaller(obj) {
// Return response to sender
if (response instanceof Promise) {
console.log("Returning promise...", response)
Logging.log("Returning promise...", Logging.LEVEL.DEBUG)(response)
sendResponse(response)
// Docs say you should be able to return a promise, but that
// doesn't work.
/* return response */
} else if (response !== undefined) {
console.log("Returning synchronously...", response)
Logging.log("Returning synchronously...", Logging.LEVEL.DEBUG)(response)
sendResponse(response)
}
} catch (e) {