vector-im-hydrogen-web/src/logging/LogItem.js

194 lines
5.5 KiB
JavaScript
Raw Normal View History

2021-02-11 21:07:18 +01:00
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {LogLevel, LogFilter} from "./LogFilter.js";
2021-02-11 21:07:18 +01:00
export class LogItem {
2021-02-16 18:29:42 +01:00
constructor(labelOrValues, logLevel, filterCreator, clock) {
this._clock = clock;
this._start = clock.now();
2021-02-11 21:07:18 +01:00
this._end = null;
2021-02-16 20:05:33 +01:00
// (l)abel
this._values = typeof labelOrValues === "string" ? {l: labelOrValues} : labelOrValues;
this.error = null;
this.logLevel = logLevel;
this._children = null;
this._filterCreator = filterCreator;
2021-02-11 21:07:18 +01:00
}
2021-02-12 13:04:05 +01:00
/**
* Creates a new child item and runs it in `callback`.
*/
wrap(labelOrValues, callback, logLevel = null, filterCreator = null) {
const item = this.child(labelOrValues, logLevel, filterCreator);
2021-02-12 13:04:05 +01:00
return item.run(callback);
}
get duration() {
if (this._end) {
return this._end - this._start;
} else {
return null;
}
}
2021-02-12 13:04:05 +01:00
/**
* Creates a new child item that finishes immediately
* and can hence not be modified anymore.
*
* Hence, the child item is not returned.
*/
log(labelOrValues, logLevel = null) {
const item = this.child(labelOrValues, logLevel, null);
2021-02-12 13:04:05 +01:00
item.end = item.start;
2021-02-11 21:07:18 +01:00
}
set(key, value) {
if(typeof key === "object") {
const values = key;
Object.assign(this._values, values);
} else {
this._values[key] = value;
}
}
serialize(filter) {
if (this._filterCreator) {
try {
filter = this._filterCreator(new LogFilter(filter), this);
} catch (err) {
console.error("Error creating log item", err);
}
}
let children;
if (this._children !== null) {
children = this._children.reduce((array, c) => {
const s = c.serialize(filter);
if (s) {
if (array === null) {
array = [];
}
array.push(s);
}
return array;
}, null);
}
if (!filter.filter(this, children)) {
return null;
2021-02-11 21:07:18 +01:00
}
const item = {
// (s)tart
s: this._start,
// (d)uration
d: this.duration,
// (v)alues
v: this._values,
// (l)evel
l: this.logLevel
2021-02-11 21:07:18 +01:00
};
if (this.error) {
// (e)rror
item.e = {
stack: this.error.stack,
name: this.error.name
};
}
if (children) {
// (c)hildren
item.c = children;
}
return item;
2021-02-11 21:07:18 +01:00
}
2021-02-12 13:04:05 +01:00
/**
* You probably want to use `wrap` instead of this.
*
* Runs a callback passing this log item,
* recording the timing and any error.
*
* callback can return a Promise.
*
2021-02-12 18:35:33 +01:00
* Should only be called once.
2021-02-12 13:04:05 +01:00
*
* @param {Function} callback [description]
* @return {[type]} [description]
*/
run(callback) {
if (this._end !== null) {
2021-02-12 18:35:33 +01:00
console.trace("log item is finished, additional logs will likely not be recorded");
2021-02-12 13:04:05 +01:00
}
let result;
2021-02-11 21:07:18 +01:00
try {
2021-02-12 13:04:05 +01:00
result = callback(this);
if (result instanceof Promise) {
return result.then(promiseResult => {
this.finish();
2021-02-12 13:04:05 +01:00
return promiseResult;
}, err => {
2021-02-12 18:35:33 +01:00
throw this.catch(err);
2021-02-12 13:04:05 +01:00
});
} else {
this.finish();
2021-02-12 13:04:05 +01:00
return result;
}
2021-02-11 21:07:18 +01:00
} catch (err) {
2021-02-12 18:35:33 +01:00
throw this.catch(err);
2021-02-11 21:07:18 +01:00
}
}
2021-02-12 13:04:05 +01:00
/**
* finished the item, recording the end time. After finishing, an item can't be modified anymore as it will be persisted.
* @internal shouldn't typically be called by hand. allows to force finish if a promise is still running when closing the app
*/
finish() {
if (this._end === null) {
if (this._children !== null) {
for(const c of this._children) {
c.finish();
}
}
2021-02-16 18:29:42 +01:00
this._end = this._clock.now();
}
}
2021-02-12 16:08:07 +01:00
// expose log level without needing import everywhere
get level() {
return LogLevel;
}
2021-02-12 18:35:33 +01:00
catch(err) {
this.error = err;
this.logLevel = LogLevel.Error;
2021-02-12 18:35:33 +01:00
this.finish();
return err;
2021-02-12 13:04:05 +01:00
}
child(labelOrValues, logLevel, filterCreator) {
2021-02-12 13:04:05 +01:00
if (this._end !== null) {
2021-02-12 18:35:33 +01:00
console.trace("log item is finished, additional logs will likely not be recorded");
2021-02-12 13:04:05 +01:00
}
if (!logLevel) {
logLevel = this.logLevel || LogLevel.Info;
}
2021-02-16 18:29:42 +01:00
const item = new LogItem(labelOrValues, logLevel, filterCreator, this._clock);
if (this._children === null) {
this._children = [];
}
2021-02-12 13:04:05 +01:00
this._children.push(item);
return item;
}
2021-02-11 21:07:18 +01:00
}