From c285338705800372ff996459047d64cc3f6a7419 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 18 Apr 2020 10:27:37 +0300 Subject: [PATCH] Add null logger --- src/Dashboard/Dashboard.ts | 6 +++--- src/Logger.ts | 19 ++++++++++++++----- src/Page/BuildPage.ts | 4 ++-- src/Scheduler.ts | 4 ++-- src/Storage/ActionQueue.ts | 4 ++-- src/Storage/DataStorage.ts | 7 ++++--- src/Storage/TaskQueue.ts | 6 +++--- 7 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/Dashboard/Dashboard.ts b/src/Dashboard/Dashboard.ts index bda42b5..6269608 100644 --- a/src/Dashboard/Dashboard.ts +++ b/src/Dashboard/Dashboard.ts @@ -13,7 +13,7 @@ import { import Vue from 'vue'; import DashboardApp from './Components/DashboardApp.vue'; import { ResourcesToLevel } from '../Task/ResourcesToLevel'; -import { Logger } from '../Logger'; +import { ConsoleLogger, Logger } from '../Logger'; import { VillageState } from '../State/VillageState'; import { StateGrabberManager } from '../State/StateGrabberManager'; @@ -25,14 +25,14 @@ interface QuickAction { export class Dashboard { private readonly version: string; private scheduler: Scheduler; - private readonly logger; private grabbers: StateGrabberManager; + private readonly logger: Logger; constructor(version: string, scheduler: Scheduler) { this.version = version; this.scheduler = scheduler; this.grabbers = new StateGrabberManager(); - this.logger = new Logger(this.constructor.name); + this.logger = new ConsoleLogger(this.constructor.name); } async run() { diff --git a/src/Logger.ts b/src/Logger.ts index 722df3b..66c76b8 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -1,6 +1,19 @@ -export class Logger { +export abstract class Logger { + abstract log(...args): void; + abstract warn(...args): void; + abstract error(...args): void; +} + +export class NullLogger extends Logger { + log(...args): void {} + warn(...args): void {} + error(...args): void {} +} + +export class ConsoleLogger extends Logger { private readonly name: string; constructor(name: string) { + super(); this.name = name.toUpperCase(); } @@ -15,8 +28,4 @@ export class Logger { error(...args): void { console.error(this.name + ':', ...args); } - - makeLogger(name: string) { - return new Logger(this.name + '.' + name); - } } diff --git a/src/Page/BuildPage.ts b/src/Page/BuildPage.ts index 7c6f329..e39769f 100644 --- a/src/Page/BuildPage.ts +++ b/src/Page/BuildPage.ts @@ -3,7 +3,7 @@ import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask'; import { Scheduler } from '../Scheduler'; import { TrainTroopTask } from '../Task/TrainTroopTask'; import { grabActiveVillageId } from './VillageBlock'; -import { Logger } from '../Logger'; +import { ConsoleLogger, Logger } from '../Logger'; import { createBuildButton, createUpgradeButton } from './BuildingPage'; import { BuildBuildingTask } from '../Task/BuildBuildingTask'; @@ -19,7 +19,7 @@ export class BuildPage { this.scheduler = scheduler; this.buildId = buildId; this.categoryId = categoryId; - this.logger = new Logger(this.constructor.name); + this.logger = new ConsoleLogger(this.constructor.name); } run() { diff --git a/src/Scheduler.ts b/src/Scheduler.ts index 6121c94..fb6dd22 100644 --- a/src/Scheduler.ts +++ b/src/Scheduler.ts @@ -9,7 +9,7 @@ import { createAction } from './Action/ActionController'; import { createTask } from './Task/TaskController'; import { SendOnAdventureTask } from './Task/SendOnAdventureTask'; import { BalanceHeroResourcesTask } from './Task/BalanceHeroResourcesTask'; -import { Logger } from './Logger'; +import { ConsoleLogger, Logger } from './Logger'; import { BuildBuildingTask } from './Task/BuildBuildingTask'; import { GrabVillageState } from './Task/GrabVillageState'; import { StateGrabberManager } from './State/StateGrabberManager'; @@ -26,7 +26,7 @@ export class Scheduler { this.taskQueue = new TaskQueue(); this.actionQueue = new ActionQueue(); this.grabbers = new StateGrabberManager(); - this.logger = new Logger(this.constructor.name); + this.logger = new ConsoleLogger(this.constructor.name); } async run() { diff --git a/src/Storage/ActionQueue.ts b/src/Storage/ActionQueue.ts index 7197b3e..8682a08 100644 --- a/src/Storage/ActionQueue.ts +++ b/src/Storage/ActionQueue.ts @@ -1,5 +1,5 @@ import { Command } from '../Common'; -import { Logger } from '../Logger'; +import { ConsoleLogger, Logger } from '../Logger'; const QUEUE_NAME = 'action_queue:v2'; @@ -22,7 +22,7 @@ export class ActionQueue { private readonly logger; constructor() { - this.logger = new Logger(this.constructor.name); + this.logger = new ConsoleLogger(this.constructor.name); } pop(): Command | undefined { diff --git a/src/Storage/DataStorage.ts b/src/Storage/DataStorage.ts index 6a60e21..659ae2b 100644 --- a/src/Storage/DataStorage.ts +++ b/src/Storage/DataStorage.ts @@ -1,4 +1,4 @@ -import { Logger } from '../Logger'; +import { ConsoleLogger, Logger, NullLogger } from '../Logger'; const NAMESPACE = 'travian:v1'; @@ -7,12 +7,13 @@ function join(...parts: Array) { } export class DataStorage { - private readonly logger; + private readonly logger: Logger; private readonly name: string; constructor(name: string) { this.name = name; - this.logger = new Logger(this.constructor.name); + // this.logger = new ConsoleLogger(this.constructor.name); + this.logger = new NullLogger(); } get(key: string): any { diff --git a/src/Storage/TaskQueue.ts b/src/Storage/TaskQueue.ts index 5c90bce..c9fdd2d 100644 --- a/src/Storage/TaskQueue.ts +++ b/src/Storage/TaskQueue.ts @@ -1,6 +1,6 @@ import { Args } from '../Common'; import { uniqId } from '../utils'; -import { Logger } from '../Logger'; +import { ConsoleLogger, Logger } from '../Logger'; const QUEUE_NAME = 'task_queue:v4'; @@ -30,10 +30,10 @@ export class Task { export type TaskList = Array; export class TaskQueue { - private readonly logger; + private readonly logger: Logger; constructor() { - this.logger = new Logger(this.constructor.name); + this.logger = new ConsoleLogger(this.constructor.name); } push(name: string, args: Args, ts: number): Task {