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.
|
|
|
|
*/
|
|
|
|
|
2021-02-16 15:07:17 +01:00
|
|
|
import {LogLevel, LogFilter} from "./LogFilter.js";
|
2021-02-12 14:05:51 +01:00
|
|
|
|
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
|
2021-02-16 15:07:17 +01:00
|
|
|
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`.
|
|
|
|
*/
|
2021-02-16 15:07:17 +01:00
|
|
|
wrap(labelOrValues, callback, logLevel = LogLevel.Info, filterCreator = null) {
|
|
|
|
const item = this.child(labelOrValues, logLevel, filterCreator);
|
2021-02-12 13:04:05 +01:00
|
|
|
return item.run(callback);
|
|
|
|
}
|
2021-02-16 11:37:48 +01:00
|
|
|
|
2021-02-16 15:07:17 +01:00
|
|
|
get duration() {
|
2021-02-16 11:37:48 +01:00
|
|
|
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.
|
|
|
|
*/
|
2021-02-16 15:07:17 +01:00
|
|
|
log(labelOrValues, logLevel = LogLevel.Info) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 15:07:17 +01:00
|
|
|
serialize(filter, depth) {
|
|
|
|
if (this._filterCreator) {
|
2021-02-16 18:36:41 +01:00
|
|
|
try {
|
|
|
|
filter = this._filterCreator(new LogFilter(filter), this);
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error creating log item", err);
|
|
|
|
}
|
2021-02-12 14:05:51 +01:00
|
|
|
}
|
2021-02-16 15:07:17 +01:00
|
|
|
let children;
|
|
|
|
if (this._children !== null) {
|
|
|
|
children = this._children.reduce((array, c) => {
|
|
|
|
const s = c.serialize(filter, depth + 1);
|
|
|
|
if (s) {
|
|
|
|
if (array === null) {
|
|
|
|
array = [];
|
|
|
|
}
|
|
|
|
array.push(s);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}, null);
|
|
|
|
}
|
|
|
|
if (!filter.filter(this, children, depth)) {
|
|
|
|
return null;
|
2021-02-11 21:07:18 +01:00
|
|
|
}
|
2021-02-16 11:37:48 +01:00
|
|
|
const item = {
|
2021-02-16 15:07:17 +01:00
|
|
|
// (s)tart
|
2021-02-16 11:37:48 +01:00
|
|
|
s: this._start,
|
2021-02-16 15:07:17 +01:00
|
|
|
// (d)uration
|
|
|
|
d: this.duration,
|
|
|
|
// (v)alues
|
2021-02-16 11:37:48 +01:00
|
|
|
v: this._values,
|
2021-02-16 15:07:17 +01:00
|
|
|
// (l)evel
|
|
|
|
l: this.logLevel
|
2021-02-11 21:07:18 +01:00
|
|
|
};
|
2021-02-16 15:07:17 +01:00
|
|
|
if (this.error) {
|
|
|
|
// (e)rror
|
|
|
|
item.e = {
|
|
|
|
stack: this.error.stack,
|
|
|
|
name: this.error.name
|
|
|
|
};
|
2021-02-16 11:37:48 +01:00
|
|
|
}
|
|
|
|
if (children) {
|
2021-02-16 15:07:17 +01:00
|
|
|
// (c)hildren
|
2021-02-16 11:37:48 +01:00
|
|
|
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 => {
|
2021-02-12 14:05:51 +01:00
|
|
|
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 {
|
2021-02-12 14:05:51 +01:00
|
|
|
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
|
|
|
|
2021-02-12 14:05:51 +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) {
|
2021-02-16 15:07:17 +01:00
|
|
|
if (this._children !== null) {
|
|
|
|
for(const c of this._children) {
|
|
|
|
c.finish();
|
|
|
|
}
|
2021-02-12 14:05:51 +01:00
|
|
|
}
|
2021-02-16 18:29:42 +01:00
|
|
|
this._end = this._clock.now();
|
2021-02-12 14:05:51 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-12 16:08:07 +01:00
|
|
|
|
2021-02-16 11:37:48 +01:00
|
|
|
// expose log level without needing import everywhere
|
2021-02-12 14:05:51 +01:00
|
|
|
get level() {
|
|
|
|
return LogLevel;
|
|
|
|
}
|
|
|
|
|
2021-02-12 18:35:33 +01:00
|
|
|
catch(err) {
|
2021-02-16 15:07:17 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-16 15:07:17 +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
|
|
|
}
|
2021-02-16 18:29:42 +01:00
|
|
|
const item = new LogItem(labelOrValues, logLevel, filterCreator, this._clock);
|
2021-02-16 15:07:17 +01:00
|
|
|
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
|
|
|
}
|