mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 03:25:12 +01:00
support referencing other log items that are detached (fire & forget)
for async tasks that are not awaited
This commit is contained in:
parent
b2621b3001
commit
ed1b37d251
@ -24,13 +24,39 @@ export class BaseLogger {
|
||||
}
|
||||
|
||||
log(labelOrValues, logLevel = LogLevel.Info) {
|
||||
const item = new LogItem(labelOrValues, logLevel, null, this._platform.clock);
|
||||
const item = new LogItem(labelOrValues, logLevel, null, this);
|
||||
item._end = item._start;
|
||||
this._persistItem(item.serialize(null));
|
||||
}
|
||||
|
||||
run(labelOrValues, callback, logLevel = LogLevel.Info, filterCreator = null) {
|
||||
const item = new LogItem(labelOrValues, logLevel, null, this._platform.clock);
|
||||
wrapOrRun(item, labelOrValues, callback, logLevel = null, filterCreator = null) {
|
||||
if (item) {
|
||||
return item.wrap(labelOrValues, callback, logLevel, filterCreator);
|
||||
} else {
|
||||
return this.run(labelOrValues, callback, logLevel, filterCreator);
|
||||
}
|
||||
}
|
||||
|
||||
runDetached(labelOrValues, callback, logLevel = null, filterCreator = null) {
|
||||
if (logLevel === null) {
|
||||
logLevel = LogLevel.Info;
|
||||
}
|
||||
const item = new LogItem(labelOrValues, logLevel, null, this);
|
||||
const refId = Math.round(this._platform.random() * Number.MAX_SAFE_INTEGER);
|
||||
item.set("refId", refId);
|
||||
this._run(item, callback, logLevel, filterCreator, false /* don't throw, nobody is awaiting */);
|
||||
return item;
|
||||
}
|
||||
|
||||
run(labelOrValues, callback, logLevel = null, filterCreator = null) {
|
||||
if (logLevel === null) {
|
||||
logLevel = LogLevel.Info;
|
||||
}
|
||||
const item = new LogItem(labelOrValues, logLevel, null, this);
|
||||
return this._run(item, callback, logLevel, filterCreator, true);
|
||||
}
|
||||
|
||||
_run(item, callback, logLevel, filterCreator, shouldThrow) {
|
||||
this._openItems.add(item);
|
||||
|
||||
const finishItem = () => {
|
||||
@ -64,7 +90,9 @@ export class BaseLogger {
|
||||
return promiseResult;
|
||||
}, err => {
|
||||
finishItem();
|
||||
if (shouldThrow) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
finishItem();
|
||||
@ -72,9 +100,11 @@ export class BaseLogger {
|
||||
}
|
||||
} catch (err) {
|
||||
finishItem();
|
||||
if (shouldThrow) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_finishOpenItems() {
|
||||
for (const openItem of this._openItems) {
|
||||
@ -106,4 +136,8 @@ export class BaseLogger {
|
||||
get level() {
|
||||
return LogLevel;
|
||||
}
|
||||
|
||||
_now() {
|
||||
return this._platform.clock.now();
|
||||
}
|
||||
}
|
||||
|
@ -17,9 +17,9 @@ limitations under the License.
|
||||
import {LogLevel, LogFilter} from "./LogFilter.js";
|
||||
|
||||
export class LogItem {
|
||||
constructor(labelOrValues, logLevel, filterCreator, clock) {
|
||||
this._clock = clock;
|
||||
this._start = clock.now();
|
||||
constructor(labelOrValues, logLevel, filterCreator, logger) {
|
||||
this._logger = logger;
|
||||
this._start = logger._now();
|
||||
this._end = null;
|
||||
// (l)abel
|
||||
this._values = typeof labelOrValues === "string" ? {l: labelOrValues} : labelOrValues;
|
||||
@ -29,6 +29,14 @@ export class LogItem {
|
||||
this._filterCreator = filterCreator;
|
||||
}
|
||||
|
||||
runDetached(labelOrValues, callback, logLevel, filterCreator) {
|
||||
return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator);
|
||||
}
|
||||
|
||||
wrapDetached(labelOrValues, callback, logLevel, filterCreator) {
|
||||
this.refDetached(this.runDetached(labelOrValues, callback, logLevel, filterCreator));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new child item and runs it in `callback`.
|
||||
*/
|
||||
@ -70,6 +78,9 @@ export class LogItem {
|
||||
log(labelOrValues, logLevel = null) {
|
||||
const item = this.child(labelOrValues, logLevel, null);
|
||||
item.end = item.start;
|
||||
|
||||
refDetached(logItem, logLevel = null) {
|
||||
return this.log({ref: logItem._values.refId}, logLevel);
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
@ -177,7 +188,7 @@ export class LogItem {
|
||||
c.finish();
|
||||
}
|
||||
}
|
||||
this._end = this._clock.now();
|
||||
this._end = this._logger._now();
|
||||
}
|
||||
}
|
||||
|
||||
@ -200,7 +211,7 @@ export class LogItem {
|
||||
if (!logLevel) {
|
||||
logLevel = this.logLevel || LogLevel.Info;
|
||||
}
|
||||
const item = new LogItem(labelOrValues, logLevel, filterCreator, this._clock);
|
||||
const item = new LogItem(labelOrValues, logLevel, filterCreator, this._logger);
|
||||
if (this._children === null) {
|
||||
this._children = [];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user