You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-24 23:26:50 +02:00
All: Added global logger
Avoids having to pass around a logger to every module and sub-module, and makes it easier to set log level globally.
This commit is contained in:
@ -426,7 +426,7 @@ class Application extends BaseApplication {
|
|||||||
|
|
||||||
const AppGui = require('./app-gui.js');
|
const AppGui = require('./app-gui.js');
|
||||||
this.gui_ = new AppGui(this, this.store(), keymap);
|
this.gui_ = new AppGui(this, this.store(), keymap);
|
||||||
this.gui_.setLogger(this.logger_);
|
this.gui_.setLogger(this.logger());
|
||||||
await this.gui_.start();
|
await this.gui_.start();
|
||||||
|
|
||||||
// Since the settings need to be loaded before the store is created, it will never
|
// Since the settings need to be loaded before the store is created, it will never
|
||||||
|
@ -147,6 +147,8 @@ logger.addTarget('console');
|
|||||||
logger.addTarget('file', { path: `${logDir}/log.txt` });
|
logger.addTarget('file', { path: `${logDir}/log.txt` });
|
||||||
logger.setLevel(Logger.LEVEL_WARN); // Set to DEBUG to display sync process in console
|
logger.setLevel(Logger.LEVEL_WARN); // Set to DEBUG to display sync process in console
|
||||||
|
|
||||||
|
Logger.initializeGlobalLogger(logger);
|
||||||
|
|
||||||
BaseItem.loadClass('Note', Note);
|
BaseItem.loadClass('Note', Note);
|
||||||
BaseItem.loadClass('Folder', Folder);
|
BaseItem.loadClass('Folder', Folder);
|
||||||
BaseItem.loadClass('Resource', Resource);
|
BaseItem.loadClass('Resource', Resource);
|
||||||
|
@ -490,15 +490,9 @@ class Application extends BaseApplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async initPluginService() {
|
private async initPluginService() {
|
||||||
const pluginLogger = new Logger();
|
|
||||||
pluginLogger.addTarget(TargetType.File, { path: `${Setting.value('profileDir')}/log-plugins.txt` });
|
|
||||||
pluginLogger.addTarget(TargetType.Console, { prefix: 'Plugin Service:' });
|
|
||||||
pluginLogger.setLevel(Setting.value('env') == 'dev' ? Logger.LEVEL_DEBUG : Logger.LEVEL_INFO);
|
|
||||||
|
|
||||||
const service = PluginService.instance();
|
const service = PluginService.instance();
|
||||||
|
|
||||||
const pluginRunner = new PluginRunner();
|
const pluginRunner = new PluginRunner();
|
||||||
service.setLogger(pluginLogger);
|
|
||||||
service.initialize(packageInfo.version, PlatformImplementation.instance(), pluginRunner, this.store());
|
service.initialize(packageInfo.version, PlatformImplementation.instance(), pluginRunner, this.store());
|
||||||
|
|
||||||
const pluginSettings = service.unserializePluginSettings(Setting.value('plugins.states'));
|
const pluginSettings = service.unserializePluginSettings(Setting.value('plugins.states'));
|
||||||
|
@ -399,6 +399,8 @@ async function initialize(dispatch) {
|
|||||||
mainLogger.setLevel(Logger.LEVEL_DEBUG);
|
mainLogger.setLevel(Logger.LEVEL_DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Logger.initializeGlobalLogger(mainLogger);
|
||||||
|
|
||||||
reg.setLogger(mainLogger);
|
reg.setLogger(mainLogger);
|
||||||
reg.setShowErrorMessageBoxHandler((message) => { alert(message); });
|
reg.setShowErrorMessageBoxHandler((message) => { alert(message); });
|
||||||
|
|
||||||
|
@ -46,13 +46,13 @@ const MigrationService = require('./services/MigrationService');
|
|||||||
const { toSystemSlashes } = require('./path-utils');
|
const { toSystemSlashes } = require('./path-utils');
|
||||||
const { setAutoFreeze } = require('immer');
|
const { setAutoFreeze } = require('immer');
|
||||||
|
|
||||||
|
const appLogger = Logger.create('App');
|
||||||
|
|
||||||
// const ntpClient = require('./vendor/ntp-client');
|
// const ntpClient = require('./vendor/ntp-client');
|
||||||
// ntpClient.dgram = require('dgram');
|
// ntpClient.dgram = require('dgram');
|
||||||
|
|
||||||
export default class BaseApplication {
|
export default class BaseApplication {
|
||||||
|
|
||||||
private logger_: Logger;
|
|
||||||
private dbLogger_: Logger;
|
|
||||||
private eventEmitter_: any;
|
private eventEmitter_: any;
|
||||||
private scheduleAutoAddResourcesIID_: any = null;
|
private scheduleAutoAddResourcesIID_: any = null;
|
||||||
private database_: any = null;
|
private database_: any = null;
|
||||||
@ -68,10 +68,7 @@ export default class BaseApplication {
|
|||||||
protected store_: any = null;
|
protected store_: any = null;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.logger_ = new Logger();
|
|
||||||
this.dbLogger_ = new Logger();
|
|
||||||
this.eventEmitter_ = new EventEmitter();
|
this.eventEmitter_ = new EventEmitter();
|
||||||
|
|
||||||
this.decryptionWorker_resourceMetadataButNotBlobDecrypted = this.decryptionWorker_resourceMetadataButNotBlobDecrypted.bind(this);
|
this.decryptionWorker_resourceMetadataButNotBlobDecrypted = this.decryptionWorker_resourceMetadataButNotBlobDecrypted.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,15 +98,13 @@ export default class BaseApplication {
|
|||||||
EncryptionService.instance_ = null;
|
EncryptionService.instance_ = null;
|
||||||
DecryptionWorker.instance_ = null;
|
DecryptionWorker.instance_ = null;
|
||||||
|
|
||||||
this.logger_.info('Base application terminated...');
|
appLogger.info('Base application terminated...');
|
||||||
this.logger_ = null;
|
|
||||||
this.dbLogger_ = null;
|
|
||||||
this.eventEmitter_ = null;
|
this.eventEmitter_ = null;
|
||||||
this.decryptionWorker_resourceMetadataButNotBlobDecrypted = null;
|
this.decryptionWorker_resourceMetadataButNotBlobDecrypted = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger() {
|
logger() {
|
||||||
return this.logger_;
|
return appLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public store() {
|
public store() {
|
||||||
@ -289,7 +284,7 @@ export default class BaseApplication {
|
|||||||
parentType = BaseModel.TYPE_SMART_FILTER;
|
parentType = BaseModel.TYPE_SMART_FILTER;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger().debug('Refreshing notes:', parentType, parentId);
|
appLogger.debug('Refreshing notes:', parentType, parentId);
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
order: stateUtils.notesOrder(state.settings),
|
order: stateUtils.notesOrder(state.settings),
|
||||||
@ -460,7 +455,7 @@ export default class BaseApplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async generalMiddleware(store: any, next: any, action: any) {
|
async generalMiddleware(store: any, next: any, action: any) {
|
||||||
// this.logger().debug('Reducer action', this.reducerActionToString(action));
|
// appLogger.debug('Reducer action', this.reducerActionToString(action));
|
||||||
|
|
||||||
const result = next(action);
|
const result = next(action);
|
||||||
const newState = store.getState();
|
const newState = store.getState();
|
||||||
@ -701,35 +696,30 @@ export default class BaseApplication {
|
|||||||
const extraFlags = await this.readFlagsFromFile(`${profileDir}/flags.txt`);
|
const extraFlags = await this.readFlagsFromFile(`${profileDir}/flags.txt`);
|
||||||
initArgs = Object.assign(initArgs, extraFlags);
|
initArgs = Object.assign(initArgs, extraFlags);
|
||||||
|
|
||||||
this.logger_.addTarget(TargetType.File, { path: `${profileDir}/log.txt` });
|
|
||||||
if (Setting.value('env') === 'dev' && !shim.isTestingEnv()) {
|
|
||||||
// this.logger_.addTarget(TargetType.Console, { level: Logger.LEVEL_DEBUG });
|
|
||||||
}
|
|
||||||
this.logger_.setLevel(initArgs.logLevel);
|
|
||||||
|
|
||||||
reg.setLogger(this.logger_);
|
|
||||||
|
|
||||||
|
const globalLogger = new Logger();
|
||||||
|
globalLogger.addTarget(TargetType.File, { path: `${profileDir}/log.txt` });
|
||||||
|
if (Setting.value('appType') === 'desktop') {
|
||||||
|
globalLogger.addTarget(TargetType.Console);
|
||||||
|
}
|
||||||
|
globalLogger.setLevel(initArgs.logLevel);
|
||||||
|
Logger.initializeGlobalLogger(globalLogger);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
reg.setLogger(Logger.create(''));
|
||||||
reg.dispatch = () => {};
|
reg.dispatch = () => {};
|
||||||
|
|
||||||
BaseService.logger_ = this.logger_;
|
BaseService.logger_ = globalLogger;
|
||||||
// require('lib/ntpDate').setLogger(reg.logger());
|
|
||||||
|
|
||||||
this.dbLogger_.addTarget(TargetType.File, { path: `${profileDir}/log-database.txt` });
|
|
||||||
this.dbLogger_.setLevel(initArgs.logLevel);
|
|
||||||
|
|
||||||
if (Setting.value('appType') === 'desktop') {
|
appLogger.info(`Profile directory: ${profileDir}`);
|
||||||
this.logger_.addTarget(TargetType.Console, { level: Logger.LEVEL_WARN });
|
|
||||||
this.dbLogger_.addTarget(TargetType.Console, { level: Logger.LEVEL_WARN });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Setting.value('env') === 'dev') {
|
|
||||||
this.dbLogger_.setLevel(Logger.LEVEL_INFO);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger_.info(`Profile directory: ${profileDir}`);
|
|
||||||
|
|
||||||
this.database_ = new JoplinDatabase(new DatabaseDriverNode());
|
this.database_ = new JoplinDatabase(new DatabaseDriverNode());
|
||||||
this.database_.setLogExcludedQueryTypes(['SELECT']);
|
this.database_.setLogExcludedQueryTypes(['SELECT']);
|
||||||
this.database_.setLogger(this.dbLogger_);
|
this.database_.setLogger(globalLogger);
|
||||||
|
|
||||||
// if (Setting.value('env') === 'dev') {
|
// if (Setting.value('env') === 'dev') {
|
||||||
// if (shim.isElectron()) {
|
// if (shim.isElectron()) {
|
||||||
@ -756,7 +746,7 @@ export default class BaseApplication {
|
|||||||
|
|
||||||
await loadKeychainServiceAndSettings(KeychainServiceDriver);
|
await loadKeychainServiceAndSettings(KeychainServiceDriver);
|
||||||
|
|
||||||
this.logger_.info(`Client ID: ${Setting.value('clientId')}`);
|
appLogger.info(`Client ID: ${Setting.value('clientId')}`);
|
||||||
|
|
||||||
if (Setting.value('firstStart')) {
|
if (Setting.value('firstStart')) {
|
||||||
const locale = shim.detectAndSetLocale(Setting);
|
const locale = shim.detectAndSetLocale(Setting);
|
||||||
@ -817,9 +807,9 @@ export default class BaseApplication {
|
|||||||
|
|
||||||
KvStore.instance().setDb(reg.db());
|
KvStore.instance().setDb(reg.db());
|
||||||
|
|
||||||
EncryptionService.instance().setLogger(this.logger_);
|
EncryptionService.instance().setLogger(globalLogger);
|
||||||
BaseItem.encryptionService_ = EncryptionService.instance();
|
BaseItem.encryptionService_ = EncryptionService.instance();
|
||||||
DecryptionWorker.instance().setLogger(this.logger_);
|
DecryptionWorker.instance().setLogger(globalLogger);
|
||||||
DecryptionWorker.instance().setEncryptionService(EncryptionService.instance());
|
DecryptionWorker.instance().setEncryptionService(EncryptionService.instance());
|
||||||
DecryptionWorker.instance().setKvStore(KvStore.instance());
|
DecryptionWorker.instance().setKvStore(KvStore.instance());
|
||||||
await EncryptionService.instance().loadMasterKeysFromSettings();
|
await EncryptionService.instance().loadMasterKeysFromSettings();
|
||||||
@ -828,7 +818,7 @@ export default class BaseApplication {
|
|||||||
ResourceFetcher.instance().setFileApi(() => {
|
ResourceFetcher.instance().setFileApi(() => {
|
||||||
return reg.syncTarget().fileApi();
|
return reg.syncTarget().fileApi();
|
||||||
});
|
});
|
||||||
ResourceFetcher.instance().setLogger(this.logger_);
|
ResourceFetcher.instance().setLogger(globalLogger);
|
||||||
ResourceFetcher.instance().on('downloadComplete', this.resourceFetcher_downloadComplete);
|
ResourceFetcher.instance().on('downloadComplete', this.resourceFetcher_downloadComplete);
|
||||||
ResourceFetcher.instance().start();
|
ResourceFetcher.instance().start();
|
||||||
|
|
||||||
|
@ -26,6 +26,13 @@ interface Target {
|
|||||||
source?: string;
|
source?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface LoggerWrapper {
|
||||||
|
debug: Function;
|
||||||
|
info: Function;
|
||||||
|
warn: Function;
|
||||||
|
error: Function;
|
||||||
|
}
|
||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
|
|
||||||
// For backward compatibility
|
// For backward compatibility
|
||||||
@ -36,6 +43,7 @@ class Logger {
|
|||||||
public static LEVEL_DEBUG = LogLevel.Debug;
|
public static LEVEL_DEBUG = LogLevel.Debug;
|
||||||
|
|
||||||
public static fsDriver_: any = null;
|
public static fsDriver_: any = null;
|
||||||
|
private static globalLogger_: Logger = null;
|
||||||
|
|
||||||
private targets_: Target[] = [];
|
private targets_: Target[] = [];
|
||||||
private level_: LogLevel = LogLevel.Info;
|
private level_: LogLevel = LogLevel.Info;
|
||||||
@ -46,6 +54,24 @@ class Logger {
|
|||||||
return Logger.fsDriver_;
|
return Logger.fsDriver_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static initializeGlobalLogger(logger: Logger) {
|
||||||
|
this.globalLogger_ = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static get globalLogger(): Logger {
|
||||||
|
if (!this.globalLogger_) throw new Error('Global logger has not been initialized!!');
|
||||||
|
return this.globalLogger_;
|
||||||
|
}
|
||||||
|
|
||||||
|
static create(prefix: string): LoggerWrapper {
|
||||||
|
return {
|
||||||
|
debug: (...object: any[]) => this.globalLogger.log(LogLevel.Debug, prefix, ...object),
|
||||||
|
info: (...object: any[]) => this.globalLogger.log(LogLevel.Info, prefix, ...object),
|
||||||
|
warn: (...object: any[]) => this.globalLogger.log(LogLevel.Warn, prefix, ...object),
|
||||||
|
error: (...object: any[]) => this.globalLogger.log(LogLevel.Error, prefix, ...object),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
setLevel(level: LogLevel) {
|
setLevel(level: LogLevel) {
|
||||||
this.level_ = level;
|
this.level_ = level;
|
||||||
}
|
}
|
||||||
@ -132,14 +158,12 @@ class Logger {
|
|||||||
return this.level();
|
return this.level();
|
||||||
}
|
}
|
||||||
|
|
||||||
log(level: LogLevel, ...object: any[]) {
|
public log(level: LogLevel, prefix: string, ...object: any[]) {
|
||||||
if (!this.targets_.length) return;
|
if (!this.targets_.length) return;
|
||||||
|
|
||||||
const timestamp = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
||||||
const line = `${timestamp}: `;
|
|
||||||
|
|
||||||
for (let i = 0; i < this.targets_.length; i++) {
|
for (let i = 0; i < this.targets_.length; i++) {
|
||||||
const target = this.targets_[i];
|
const target = this.targets_[i];
|
||||||
|
const targetPrefix = prefix ? prefix : target.prefix;
|
||||||
|
|
||||||
if (this.targetLevel(target) < level) continue;
|
if (this.targetLevel(target) < level) continue;
|
||||||
|
|
||||||
@ -149,26 +173,30 @@ class Logger {
|
|||||||
if (level == LogLevel.Warn) fn = 'warn';
|
if (level == LogLevel.Warn) fn = 'warn';
|
||||||
if (level == LogLevel.Info) fn = 'info';
|
if (level == LogLevel.Info) fn = 'info';
|
||||||
const consoleObj = target.console ? target.console : console;
|
const consoleObj = target.console ? target.console : console;
|
||||||
let items = [moment().format('HH:mm:ss')];
|
const prefixItems = [moment().format('HH:mm:ss')];
|
||||||
if (target.prefix) {
|
if (targetPrefix) prefixItems.push(targetPrefix);
|
||||||
items.push(target.prefix);
|
const items = [`${prefixItems.join(': ')}:`].concat(...object);
|
||||||
}
|
|
||||||
items = items.concat(...object);
|
|
||||||
consoleObj[fn](...items);
|
consoleObj[fn](...items);
|
||||||
} else if (target.type == 'file') {
|
} else if (target.type == 'file') {
|
||||||
const serializedObject = this.objectsToString(...object);
|
const timestamp = moment().format('YYYY-MM-DD HH:mm:ss');
|
||||||
|
const line = [timestamp];
|
||||||
|
if (targetPrefix) line.push(targetPrefix);
|
||||||
|
line.push(this.objectsToString(...object));
|
||||||
try {
|
try {
|
||||||
Logger.fsDriver().appendFileSync(target.path, `${line + serializedObject}\n`);
|
// TODO: Should log async
|
||||||
|
Logger.fsDriver().appendFileSync(target.path, `${line.join(': ')}\n`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Cannot write to log file:', error);
|
console.error('Cannot write to log file:', error);
|
||||||
}
|
}
|
||||||
} else if (target.type == 'database') {
|
} else if (target.type == 'database') {
|
||||||
const msg = this.objectsToString(...object);
|
const msg = [];
|
||||||
|
if (targetPrefix) msg.push(targetPrefix);
|
||||||
|
msg.push(this.objectsToString(...object));
|
||||||
|
|
||||||
const queries = [
|
const queries = [
|
||||||
{
|
{
|
||||||
sql: 'INSERT INTO logs (`source`, `level`, `message`, `timestamp`) VALUES (?, ?, ?, ?)',
|
sql: 'INSERT INTO logs (`source`, `level`, `message`, `timestamp`) VALUES (?, ?, ?, ?)',
|
||||||
params: [target.source, level, msg, time.unixMs()],
|
params: [target.source, level, msg.join(': '), time.unixMs()],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -188,16 +216,16 @@ class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
error(...object: any[]) {
|
error(...object: any[]) {
|
||||||
return this.log(LogLevel.Error, ...object);
|
return this.log(LogLevel.Error, null, ...object);
|
||||||
}
|
}
|
||||||
warn(...object: any[]) {
|
warn(...object: any[]) {
|
||||||
return this.log(LogLevel.Warn, ...object);
|
return this.log(LogLevel.Warn, null, ...object);
|
||||||
}
|
}
|
||||||
info(...object: any[]) {
|
info(...object: any[]) {
|
||||||
return this.log(LogLevel.Info, ...object);
|
return this.log(LogLevel.Info, null, ...object);
|
||||||
}
|
}
|
||||||
debug(...object: any[]) {
|
debug(...object: any[]) {
|
||||||
return this.log(LogLevel.Debug, ...object);
|
return this.log(LogLevel.Debug, null, ...object);
|
||||||
}
|
}
|
||||||
|
|
||||||
static levelStringToId(s: string) {
|
static levelStringToId(s: string) {
|
||||||
|
@ -6,6 +6,7 @@ const shim = require('./shim').default;
|
|||||||
class Database {
|
class Database {
|
||||||
constructor(driver) {
|
constructor(driver) {
|
||||||
this.debugMode_ = false;
|
this.debugMode_ = false;
|
||||||
|
this.sqlQueryLogEnabled_ = false;
|
||||||
this.driver_ = driver;
|
this.driver_ = driver;
|
||||||
this.logger_ = new Logger();
|
this.logger_ = new Logger();
|
||||||
this.logExcludedQueryTypes_ = [];
|
this.logExcludedQueryTypes_ = [];
|
||||||
@ -238,6 +239,8 @@ class Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logQuery(sql, params = null) {
|
logQuery(sql, params = null) {
|
||||||
|
if (!this.sqlQueryLogEnabled_) return;
|
||||||
|
|
||||||
if (this.logExcludedQueryTypes_.length) {
|
if (this.logExcludedQueryTypes_.length) {
|
||||||
const temp = sql.toLowerCase();
|
const temp = sql.toLowerCase();
|
||||||
for (let i = 0; i < this.logExcludedQueryTypes_.length; i++) {
|
for (let i = 0; i < this.logExcludedQueryTypes_.length; i++) {
|
||||||
|
@ -6,6 +6,8 @@ import { ContentScriptType } from './api/types';
|
|||||||
import Logger from '../../Logger';
|
import Logger from '../../Logger';
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
|
||||||
|
const logger = Logger.create('Plugin');
|
||||||
|
|
||||||
interface ViewControllers {
|
interface ViewControllers {
|
||||||
[key: string]: ViewController;
|
[key: string]: ViewController;
|
||||||
}
|
}
|
||||||
@ -24,18 +26,16 @@ export default class Plugin {
|
|||||||
private baseDir_: string;
|
private baseDir_: string;
|
||||||
private manifest_: PluginManifest;
|
private manifest_: PluginManifest;
|
||||||
private scriptText_: string;
|
private scriptText_: string;
|
||||||
private logger_: Logger = null;
|
|
||||||
private viewControllers_: ViewControllers = {};
|
private viewControllers_: ViewControllers = {};
|
||||||
private contentScripts_: ContentScripts = {};
|
private contentScripts_: ContentScripts = {};
|
||||||
private dispatch_: Function;
|
private dispatch_: Function;
|
||||||
private eventEmitter_: any;
|
private eventEmitter_: any;
|
||||||
private devMode_: boolean = false;
|
private devMode_: boolean = false;
|
||||||
|
|
||||||
constructor(baseDir: string, manifest: PluginManifest, scriptText: string, logger: Logger, dispatch: Function) {
|
constructor(baseDir: string, manifest: PluginManifest, scriptText: string, dispatch: Function) {
|
||||||
this.baseDir_ = shim.fsDriver().resolve(baseDir);
|
this.baseDir_ = shim.fsDriver().resolve(baseDir);
|
||||||
this.manifest_ = manifest;
|
this.manifest_ = manifest;
|
||||||
this.scriptText_ = scriptText;
|
this.scriptText_ = scriptText;
|
||||||
this.logger_ = logger;
|
|
||||||
this.dispatch_ = dispatch;
|
this.dispatch_ = dispatch;
|
||||||
this.eventEmitter_ = new EventEmitter();
|
this.eventEmitter_ = new EventEmitter();
|
||||||
}
|
}
|
||||||
@ -89,7 +89,7 @@ export default class Plugin {
|
|||||||
|
|
||||||
this.contentScripts_[type].push({ id, path: absolutePath });
|
this.contentScripts_[type].push({ id, path: absolutePath });
|
||||||
|
|
||||||
this.logger_.debug(`Plugin: ${this.id}: Registered content script: ${type}: ${id}: ${absolutePath}`);
|
logger.debug(`"${this.id}": Registered content script: ${type}: ${id}: ${absolutePath}`);
|
||||||
|
|
||||||
this.dispatch_({
|
this.dispatch_({
|
||||||
type: 'PLUGIN_CONTENT_SCRIPTS_ADD',
|
type: 'PLUGIN_CONTENT_SCRIPTS_ADD',
|
||||||
@ -117,7 +117,7 @@ export default class Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public deprecationNotice(goneInVersion: string, message: string) {
|
public deprecationNotice(goneInVersion: string, message: string) {
|
||||||
this.logger_.warn(`Plugin: ${this.id}: DEPRECATION NOTICE: ${message} This will stop working in version ${goneInVersion}.`);
|
logger.warn(`"${this.id}": DEPRECATION NOTICE: ${message} This will stop working in version ${goneInVersion}.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,13 @@ import BaseService from '../BaseService';
|
|||||||
import shim from '../../shim';
|
import shim from '../../shim';
|
||||||
import { filename, dirname, rtrimSlashes, basename } from '../../path-utils';
|
import { filename, dirname, rtrimSlashes, basename } from '../../path-utils';
|
||||||
import Setting from '../../models/Setting';
|
import Setting from '../../models/Setting';
|
||||||
|
import Logger from '../../Logger';
|
||||||
const compareVersions = require('compare-versions');
|
const compareVersions = require('compare-versions');
|
||||||
const uslug = require('uslug');
|
const uslug = require('uslug');
|
||||||
const md5File = require('md5-file/promise');
|
const md5File = require('md5-file/promise');
|
||||||
|
|
||||||
|
const logger = Logger.create('PluginService');
|
||||||
|
|
||||||
// Plugin data is split into two:
|
// Plugin data is split into two:
|
||||||
//
|
//
|
||||||
// - First there's the service `plugins` property, which contains the
|
// - First there's the service `plugins` property, which contains the
|
||||||
@ -213,7 +216,7 @@ export default class PluginService extends BaseService {
|
|||||||
distPath = `${path}/dist`;
|
distPath = `${path}/dist`;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger().info(`PluginService: Loading plugin from ${path}`);
|
logger.info(`Loading plugin from ${path}`);
|
||||||
|
|
||||||
const scriptText = await fsDriver.readFile(`${distPath}/index.js`);
|
const scriptText = await fsDriver.readFile(`${distPath}/index.js`);
|
||||||
const manifestText = await fsDriver.readFile(`${distPath}/manifest.json`);
|
const manifestText = await fsDriver.readFile(`${distPath}/manifest.json`);
|
||||||
@ -242,7 +245,7 @@ export default class PluginService extends BaseService {
|
|||||||
|
|
||||||
const manifest = manifestFromObject(manifestObj);
|
const manifest = manifestFromObject(manifestObj);
|
||||||
|
|
||||||
const plugin = new Plugin(baseDir, manifest, scriptText, this.logger(), (action: any) => this.store_.dispatch(action));
|
const plugin = new Plugin(baseDir, manifest, scriptText, (action: any) => this.store_.dispatch(action));
|
||||||
|
|
||||||
for (const msg of deprecationNotices) {
|
for (const msg of deprecationNotices) {
|
||||||
plugin.deprecationNotice('1.5', msg);
|
plugin.deprecationNotice('1.5', msg);
|
||||||
@ -274,7 +277,7 @@ export default class PluginService extends BaseService {
|
|||||||
|
|
||||||
for (const pluginPath of pluginPaths) {
|
for (const pluginPath of pluginPaths) {
|
||||||
if (pluginPath.indexOf('_') === 0) {
|
if (pluginPath.indexOf('_') === 0) {
|
||||||
this.logger().info(`PluginService: Plugin name starts with "_" and has not been loaded: ${pluginPath}`);
|
logger.info(`Plugin name starts with "_" and has not been loaded: ${pluginPath}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -289,7 +292,7 @@ export default class PluginService extends BaseService {
|
|||||||
this.setPluginAt(plugin.id, plugin);
|
this.setPluginAt(plugin.id, plugin);
|
||||||
|
|
||||||
if (!this.pluginEnabled(settings, plugin.id)) {
|
if (!this.pluginEnabled(settings, plugin.id)) {
|
||||||
this.logger().info(`PluginService: Not running disabled plugin: "${plugin.id}"`);
|
logger.info(`Not running disabled plugin: "${plugin.id}"`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,14 +300,14 @@ export default class PluginService extends BaseService {
|
|||||||
|
|
||||||
await this.runPlugin(plugin);
|
await this.runPlugin(plugin);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger().error(`PluginService: Could not load plugin: ${pluginPath}`, error);
|
logger.error(`Could not load plugin: ${pluginPath}`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async runPlugin(plugin: Plugin) {
|
public async runPlugin(plugin: Plugin) {
|
||||||
if (compareVersions(this.appVersion_, plugin.manifest.app_min_version) < 0) {
|
if (compareVersions(this.appVersion_, plugin.manifest.app_min_version) < 0) {
|
||||||
throw new Error(`PluginService: Plugin "${plugin.id}" was disabled because it requires Joplin version ${plugin.manifest.app_min_version} and current version is ${this.appVersion_}.`);
|
throw new Error(`Plugin "${plugin.id}" was disabled because it requires Joplin version ${plugin.manifest.app_min_version} and current version is ${this.appVersion_}.`);
|
||||||
} else {
|
} else {
|
||||||
this.store_.dispatch({
|
this.store_.dispatch({
|
||||||
type: 'PLUGIN_ADD',
|
type: 'PLUGIN_ADD',
|
||||||
@ -316,12 +319,12 @@ export default class PluginService extends BaseService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const pluginApi = new Global(this.logger(), this.platformImplementation_, plugin, this.store_);
|
const pluginApi = new Global(this.platformImplementation_, plugin, this.store_);
|
||||||
return this.runner_.run(plugin, pluginApi);
|
return this.runner_.run(plugin, pluginApi);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async installPlugin(jplPath: string): Promise<Plugin> {
|
public async installPlugin(jplPath: string): Promise<Plugin> {
|
||||||
this.logger().info(`PluginService: Installing plugin: "${jplPath}"`);
|
logger.info(`Installing plugin: "${jplPath}"`);
|
||||||
|
|
||||||
const destPath = `${Setting.value('pluginDir')}/${basename(jplPath)}`;
|
const destPath = `${Setting.value('pluginDir')}/${basename(jplPath)}`;
|
||||||
await shim.fsDriver().copy(jplPath, destPath);
|
await shim.fsDriver().copy(jplPath, destPath);
|
||||||
@ -343,12 +346,12 @@ export default class PluginService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async uninstallPlugin(pluginId: string) {
|
public async uninstallPlugin(pluginId: string) {
|
||||||
this.logger().info(`PluginService: Uninstalling plugin: "${pluginId}"`);
|
logger.info(`Uninstalling plugin: "${pluginId}"`);
|
||||||
|
|
||||||
const path = await this.pluginPath(pluginId);
|
const path = await this.pluginPath(pluginId);
|
||||||
if (!path) {
|
if (!path) {
|
||||||
// Plugin might have already been deleted
|
// Plugin might have already been deleted
|
||||||
this.logger().error(`PluginService: Could not find plugin path to uninstall - nothing will be done: ${pluginId}`);
|
logger.error(`Could not find plugin path to uninstall - nothing will be done: ${pluginId}`);
|
||||||
} else {
|
} else {
|
||||||
await shim.fsDriver().remove(path);
|
await shim.fsDriver().remove(path);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import Plugin from '../Plugin';
|
import Plugin from '../Plugin';
|
||||||
import Joplin from './Joplin';
|
import Joplin from './Joplin';
|
||||||
import Logger from '../../../Logger';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
@ -16,8 +15,8 @@ export default class Global {
|
|||||||
private requireWhiteList_: string[] = null;
|
private requireWhiteList_: string[] = null;
|
||||||
// private consoleWrapper_:any = null;
|
// private consoleWrapper_:any = null;
|
||||||
|
|
||||||
constructor(logger: Logger, implementation: any, plugin: Plugin, store: any) {
|
constructor(implementation: any, plugin: Plugin, store: any) {
|
||||||
this.joplin_ = new Joplin(logger, implementation.joplin, plugin, store);
|
this.joplin_ = new Joplin(implementation.joplin, plugin, store);
|
||||||
// this.consoleWrapper_ = this.createConsoleWrapper(plugin.id);
|
// this.consoleWrapper_ = this.createConsoleWrapper(plugin.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ import JoplinCommands from './JoplinCommands';
|
|||||||
import JoplinViews from './JoplinViews';
|
import JoplinViews from './JoplinViews';
|
||||||
import JoplinInterop from './JoplinInterop';
|
import JoplinInterop from './JoplinInterop';
|
||||||
import JoplinSettings from './JoplinSettings';
|
import JoplinSettings from './JoplinSettings';
|
||||||
import Logger from '../../../Logger';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the main entry point to the Joplin API. You can access various services using the provided accessors.
|
* This is the main entry point to the Joplin API. You can access various services using the provided accessors.
|
||||||
@ -35,9 +34,9 @@ export default class Joplin {
|
|||||||
private interop_: JoplinInterop = null;
|
private interop_: JoplinInterop = null;
|
||||||
private settings_: JoplinSettings = null;
|
private settings_: JoplinSettings = null;
|
||||||
|
|
||||||
constructor(logger: Logger, implementation: any, plugin: Plugin, store: any) {
|
constructor(implementation: any, plugin: Plugin, store: any) {
|
||||||
this.data_ = new JoplinData();
|
this.data_ = new JoplinData();
|
||||||
this.plugins_ = new JoplinPlugins(logger, plugin);
|
this.plugins_ = new JoplinPlugins(plugin);
|
||||||
this.workspace_ = new JoplinWorkspace(implementation.workspace, store);
|
this.workspace_ = new JoplinWorkspace(implementation.workspace, store);
|
||||||
this.filters_ = new JoplinFilters();
|
this.filters_ = new JoplinFilters();
|
||||||
this.commands_ = new JoplinCommands();
|
this.commands_ = new JoplinCommands();
|
||||||
|
@ -2,16 +2,16 @@ import Plugin from '../Plugin';
|
|||||||
import Logger from '../../../Logger';
|
import Logger from '../../../Logger';
|
||||||
import { ContentScriptType, Script } from './types';
|
import { ContentScriptType, Script } from './types';
|
||||||
|
|
||||||
|
const logger = Logger.create('joplin.plugins');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides access to plugin-related features.
|
* This class provides access to plugin-related features.
|
||||||
*/
|
*/
|
||||||
export default class JoplinPlugins {
|
export default class JoplinPlugins {
|
||||||
|
|
||||||
private logger: Logger;
|
|
||||||
private plugin: Plugin;
|
private plugin: Plugin;
|
||||||
|
|
||||||
public constructor(logger: Logger, plugin: Plugin) {
|
public constructor(plugin: Plugin) {
|
||||||
this.logger = logger;
|
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ export default class JoplinPlugins {
|
|||||||
if (script.onStart) {
|
if (script.onStart) {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
|
|
||||||
this.logger.info(`Starting plugin: ${this.plugin.id}`);
|
logger.info(`Starting plugin: ${this.plugin.id}`);
|
||||||
|
|
||||||
// We don't use `await` when calling onStart because the plugin might be awaiting
|
// We don't use `await` when calling onStart because the plugin might be awaiting
|
||||||
// in that call too (for example, when opening a dialog on startup) so we don't
|
// in that call too (for example, when opening a dialog on startup) so we don't
|
||||||
@ -42,9 +42,9 @@ export default class JoplinPlugins {
|
|||||||
// be handled correctly by loggers, etc.
|
// be handled correctly by loggers, etc.
|
||||||
const newError: Error = new Error(error.message);
|
const newError: Error = new Error(error.message);
|
||||||
newError.stack = error.stack;
|
newError.stack = error.stack;
|
||||||
this.logger.error(`Uncaught exception in plugin "${this.plugin.id}":`, newError);
|
logger.error(`Uncaught exception in plugin "${this.plugin.id}":`, newError);
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
this.logger.info(`Finished running onStart handler: ${this.plugin.id} (Took ${Date.now() - startTime}ms)`);
|
logger.info(`Finished running onStart handler: ${this.plugin.id} (Took ${Date.now() - startTime}ms)`);
|
||||||
this.plugin.emit('started');
|
this.plugin.emit('started');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user