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

168 lines
4.8 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} from "./LogLevel.js";
2021-02-11 21:07:18 +01:00
export class LogItem {
2021-02-12 13:04:05 +01:00
constructor(labelOrValues, logLevel, platform) {
this._platform = platform;
this._start = platform.clock.now();
2021-02-11 21:07:18 +01:00
this._end = null;
2021-02-12 13:04:05 +01:00
this._values = typeof labelOrValues === "string" ? {label: labelOrValues} : labelOrValues;
2021-02-11 21:07:18 +01:00
this._error = null;
this._children = [];
this._logLevel = logLevel;
}
2021-02-12 13:04:05 +01:00
/**
* Creates a new child item and runs it in `callback`.
*/
wrap(labelOrValues, callback, logLevel = this._logLevel) {
const item = this._createChild(labelOrValues, logLevel);
return item.run(callback);
}
/**
* 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 = this._logLevel) {
const item = this._createChild(labelOrValues, logLevel);
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-12 13:04:05 +01:00
anonymize(value) {
const buffer = this._platform.crypto.digest("SHA-256", value);
return this._platform.base64.encode(buffer);
2021-02-11 21:07:18 +01:00
}
serialize(logLevel) {
const children = this._children.reduce((array, c) => {
const s = c.serialize(logLevel);
if (s) {
array = array || [];
array.push(s);
}
return array;
}, null);
// neither our children or us have a loglevel high enough, bail out.
if (!children && this._logLevel < logLevel) {
return null;
}
let error = null;
2021-02-11 21:07:18 +01:00
if (this._error) {
error = {
message: this._error.message,
stack: this._error.stack,
};
}
return {
start: this._start,
end: this._end,
values: this._values,
error,
children,
2021-02-11 21:07:18 +01:00
logLevel: this._logLevel
};
}
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.
*
* Can only be called once, and will throw after.
*
* @param {Function} callback [description]
* @return {[type]} [description]
*/
run(callback) {
if (this._end !== null) {
throw new Error("item is finished");
}
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 => {
this._catch(err);
this.finish();
2021-02-12 13:04:05 +01:00
throw err;
});
} 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 13:04:05 +01:00
this._catch(err);
this.finish();
2021-02-11 21:07:18 +01:00
throw err;
}
}
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) {
for(const c of this._children) {
c.finish();
}
this._end = this._platform.clock.now();
}
}
// expose log level without needing
get level() {
return LogLevel;
}
2021-02-12 13:04:05 +01:00
_catch(err) {
this._error = err;
this._logLevel = LogLevel.Error;
2021-02-12 13:04:05 +01:00
console.error(`log item ${this.values.label} failed: ${err.message}:\n${err.stack}`);
}
_createChild(labelOrValues, logLevel) {
if (this._end !== null) {
throw new Error("item is finished");
}
const item = new LogItem(labelOrValues, logLevel, this._platform);
this._children.push(item);
return item;
}
2021-02-11 21:07:18 +01:00
}