mirror of
https://github.com/HeyPuter/puter.git
synced 2026-02-19 21:21:23 -06:00
dev: better log support for extensions
This commit is contained in:
@@ -40,7 +40,20 @@ class Extension extends AdvancedBase {
|
||||
constructor (...a) {
|
||||
super(...a);
|
||||
this.service = null;
|
||||
this.log = null;
|
||||
this.ensure_service_();
|
||||
|
||||
this.log = (...a) => {
|
||||
this.log_context.info(a.join(' '));
|
||||
};
|
||||
this.LOG = (...a) => {
|
||||
this.log_context.noticeme(a.join(' '));
|
||||
};
|
||||
['info','warn','debug','error','tick','noticeme','system'].forEach(lvl => {
|
||||
this.log[lvl] = (...a) => {
|
||||
this.log_context[lvl](...a);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
example () {
|
||||
@@ -72,6 +85,17 @@ class Extension extends AdvancedBase {
|
||||
return services;
|
||||
}
|
||||
|
||||
get log_context () {
|
||||
const log_context = this.service.values.get('log_context');
|
||||
if ( ! log_context ) {
|
||||
throw new Error(
|
||||
'extension tried to access "log_context" before it was ' +
|
||||
'initialized'
|
||||
);
|
||||
}
|
||||
return log_context;
|
||||
}
|
||||
|
||||
/**
|
||||
* This will create a GET endpoint on the default service.
|
||||
* @param {*} path - route for the endpoint
|
||||
|
||||
@@ -25,6 +25,7 @@ class ExtensionModule extends AdvancedBase {
|
||||
async install (context) {
|
||||
const services = context.get('services');
|
||||
|
||||
this.extension.name = context.name;
|
||||
this.extension.emit('install', { context, services })
|
||||
|
||||
if ( this.extension.service ) {
|
||||
|
||||
@@ -82,6 +82,8 @@ class ExtensionService extends BaseService {
|
||||
this.state = args.state;
|
||||
|
||||
this.state.values.set('services', this.services);
|
||||
this.state.values.set('log_context', this.services.get('log-service').create(
|
||||
this.state.extension.name));
|
||||
|
||||
// Create database access object for extension
|
||||
const db = this.services.get('database').get(DB_WRITE, 'extension');
|
||||
|
||||
Reference in New Issue
Block a user