Turn logging levels into string in the config

Using numbers instead of strings made things complicated when dealing
with logging (:set was more complex, :get returned a number instead of a
string...). This commit fixes this.

Note that this commit removes validation for logging levels. This means
that users can now set erroneous logging levels. Fixing this will
require better type info generation in the compiler and will happen in
another commit.
This commit is contained in:
glacambre 2018-09-10 17:35:05 +02:00
parent 9450798006
commit 7bf5198e65
No known key found for this signature in database
GPG key ID: B9625DB1767553AC
3 changed files with 50 additions and 80 deletions

View file

@ -37,6 +37,11 @@ function schlepp(settings) {
/** @hidden */
let USERCONFIG = o({})
/** @hidden
* Ideally, LoggingLevel should be in logging.ts and imported from there. However this would cause a circular dependency, which webpack can't deal with
*/
export type LoggingLevel = "never" | "error" | "warning" | "info" | "debug"
/**
* This is the default configuration that Tridactyl comes with.
*
@ -557,19 +562,17 @@ class default_config {
jumpdelay = "3000"
/**
* Default logging levels - 2 === WARNING
*
* NB: these cannot be set directly with `set` - you must use magic words such as `WARNING` or `DEBUG`.
* Logging levels. Unless you're debugging Tridactyl, it's unlikely you'll ever need to change these.
*/
logging = {
messaging: 2,
cmdline: 2,
controller: 2,
containers: 2,
hinting: 2,
state: 2,
excmd: 1,
styling: 2,
logging: { [key: string]: LoggingLevel } = {
messaging: "warning",
cmdline: "warning",
controller: "warning",
containers: "warning",
hinting: "warning",
state: "warning",
excmd: "error",
styling: "warning",
}
noiframeon: string[] = []
@ -655,7 +658,7 @@ const DEFAULTS = o(new default_config())
@param target path of properties as an array
@hidden
*/
function getDeepProperty(obj, target) {
function getDeepProperty(obj, target: string[]) {
if (obj !== undefined && target.length) {
return getDeepProperty(obj[target[0]], target.slice(1))
} else {
@ -793,7 +796,7 @@ export async function update() {
}
},
"1.0": () => {
let vimiumgi = getDeepProperty(USERCONFIG, "vimium-gi")
let vimiumgi = getDeepProperty(USERCONFIG, ["vimium-gi"])
if (vimiumgi === true || vimiumgi === "true")
set("gimode", "nextinput")
else if (vimiumgi === false || vimiumgi === "false")
@ -801,6 +804,22 @@ export async function update() {
unset("vimium-gi")
set("configversion", "1.1")
},
"1.1": () => {
let leveltostr: { [key: number]: LoggingLevel } = {
0: "never",
1: "error",
2: "warning",
3: "info",
4: "debug",
}
let logging = getDeepProperty(USERCONFIG, ["logging"])
// logging is not necessarily defined if the user didn't change default values
if (logging)
Object.keys(logging).forEach(l =>
set("logging", l, leveltostr[logging[l]]),
)
set("configversion", "1.2")
},
}
if (!get("configversion")) set("configversion", "0.0")
const updatetest = v => {

View file

@ -562,36 +562,6 @@ 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.toLowerCase()]
if (newLevel !== undefined) {
config.set("logging", logModule, newLevel)
} else {
throw "Bad log level!"
}
}
// }}}
// {{{ PAGE CONTEXT
/** @hidden */
@ -2581,23 +2551,6 @@ export function set(key: string, ...values: string[]) {
const target = key.split(".")
const last = target[target.length - 1]
// Special case conversions
// TODO: Should we do any special case shit here?
switch (target[0]) {
case "logging":
const map = {
never: Logging.LEVEL.NEVER,
error: Logging.LEVEL.ERROR,
warning: Logging.LEVEL.WARNING,
info: Logging.LEVEL.INFO,
debug: Logging.LEVEL.DEBUG,
}
let level = map[values[0].toLowerCase()]
if (level === undefined) throw "Bad log level!"
else config.set(...target, level)
return
}
const currentValue = config.get(...target)
let value: string | string[] = values

View file

@ -4,13 +4,12 @@
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,
}
const LevelToNum = new Map<Config.LoggingLevel, number>()
LevelToNum.set("never", 0)
LevelToNum.set("error", 1)
LevelToNum.set("warning", 2)
LevelToNum.set("info", 3)
LevelToNum.set("debug", 4)
export class Logger {
/**
@ -30,14 +29,13 @@ export class Logger {
* @return logging function: this is returned as a function to
* retain the call site
*/
private log(level: LEVEL) {
let configedLevel =
Config.get("logging", this.logModule) || LEVEL.WARNING
private log(level: Config.LoggingLevel) {
let configedLevel = Config.get("logging", this.logModule)
if (level <= configedLevel) {
if (LevelToNum.get(level) <= LevelToNum.get(configedLevel)) {
// hand over to console.log, error or debug as needed
switch (level) {
case LEVEL.ERROR:
case "error":
// TODO: replicate this for other levels, don't steal focus
// work out how to import messaging/webext without breaking everything
return async (...message) => {
@ -71,11 +69,11 @@ export class Logger {
},
)
}
case LEVEL.WARNING:
case "warning":
return console.warn
case LEVEL.INFO:
case "info":
return console.log
case LEVEL.DEBUG:
case "debug":
return console.debug
}
}
@ -88,16 +86,16 @@ export class Logger {
// logger.debug('blah') translates into console.debug('blah') with the
// filename and line correct.
public get debug() {
return this.log(LEVEL.DEBUG)
return this.log("debug")
}
public get info() {
return this.log(LEVEL.INFO)
return this.log("info")
}
public get warning() {
return this.log(LEVEL.WARNING)
return this.log("warning")
}
public get error() {
return this.log(LEVEL.ERROR)
return this.log("error")
}
}