Skip to content

Commit

Permalink
refactor(logging): export logging API
Browse files Browse the repository at this point in the history
1. Rename the `Logging` interface to `Logger`
2. Explicity set the type for the logger field on RP2040 and GDBServer to the Logger interface, making it easy to override the logger with a custom implementation of the interface.
3. Export the Logger interface, ConsoleLogger implementation, and LogLevel enum
  • Loading branch information
urish committed May 31, 2021
1 parent 94d6a73 commit 541cc0c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
11 changes: 7 additions & 4 deletions src/gdbserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
gdbChecksum,
gdbMessage,
} from './utils/gdb';
import { ConsoleLogger, LogLevel } from './utils/logging';
import { ConsoleLogger, Logger, LogLevel } from './utils/logging';

const STOP_REPLY_SIGINT = 'S02';
const STOP_REPLY_TRAP = 'S05';
Expand Down Expand Up @@ -52,13 +52,13 @@ const targetXML = `<?xml version="1.0"?>
</feature>
</target>`;

const LOG_NAME = "GDBSERVER";
const LOG_NAME = 'GDBServer';

export class GDBTCPServer {
private socketServer = createServer();

// Console logger
public logger = new ConsoleLogger(LogLevel.Warn, true);
public logger: Logger = new ConsoleLogger(LogLevel.Warn, true);

constructor(readonly rp2040: RP2040, readonly port: number = 3333) {
this.socketServer.listen(port);
Expand Down Expand Up @@ -204,7 +204,10 @@ export class GDBTCPServer {
const length = parseInt(params[1], 16);
const data = decodeHexBuf(params[2].substr(0, length * 2));
for (let i = 0; i < data.length; i++) {
this.logger.debug(LOG_NAME, `write ${data[i].toString(16)} to ${(address + i).toString(16)}`);
this.logger.debug(
LOG_NAME,
`Write ${data[i].toString(16)} to ${(address + i).toString(16)}`
);
rp2040.writeUint8(address + i, data[i]);
}
return gdbMessage('OK');
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { GDBTCPServer } from './gdbserver';
export { GPIOPin, GPIOPinState } from './gpio-pin';
export { RP2040 } from './rp2040';
export { ConsoleLogger, Logger, LogLevel } from './utils/logging';
5 changes: 2 additions & 3 deletions src/rp2040.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { RPSIO } from './sio';
import { RPReset } from './peripherals/reset';
import { RPIO } from './peripherals/io';
import { RPPADS } from './peripherals/pads';
import { ConsoleLogger, LogLevel } from './utils/logging';
import { ConsoleLogger, Logger, LogLevel } from './utils/logging';

export const FLASH_START_ADDRESS = 0x10000000;
export const FLASH_END_ADDRESS = 0x14000000;
Expand Down Expand Up @@ -166,8 +166,7 @@ export class RP2040 {

private stopped = false;

// Console logger
public logger = new ConsoleLogger(LogLevel.Debug, true);
public logger: Logger = new ConsoleLogger(LogLevel.Debug, true);

// APSR fields
public N: boolean = false;
Expand Down
39 changes: 18 additions & 21 deletions src/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { formatTime } from './time';

export interface Logging {
debug(name: string, msg: string): void;
warn(name: string, msg: string): void;
error(name: string, msg: string): void;
info(name: string, msg: string): void;
export interface Logger {
debug(componentName: string, message: string): void;
warn(componentName: string, message: string): void;
error(componentName: string, message: string): void;
info(componentName: string, message: string): void;
}

export enum LogLevel {
Expand All @@ -14,45 +14,42 @@ export enum LogLevel {
Error,
}

export class ConsoleLogger implements Logging {
constructor(public currentLogLevel: LogLevel, private throwOnError: boolean) {
this.currentLogLevel = currentLogLevel;
this.throwOnError = throwOnError;
}
export class ConsoleLogger implements Logger {
constructor(public currentLogLevel: LogLevel, private throwOnError = true) {}

private aboveLogLevel(logLevel: LogLevel): boolean {
return logLevel >= this.currentLogLevel ? true : false;
}

private formatMessage(name: string, msg: string) {
private formatMessage(componentName: string, message: string) {
const currentTime = formatTime(new Date());
return `${currentTime} [${name}] ${msg}`;
return `${currentTime} [${componentName}] ${message}`;
}

debug(name: string, msg: string): void {
debug(componetName: string, message: string): void {
if (this.aboveLogLevel(LogLevel.Debug)) {
console.debug(this.formatMessage(name, msg));
console.debug(this.formatMessage(componetName, message));
}
}

warn(name: string, msg: string): void {
warn(componetName: string, message: string): void {
if (this.aboveLogLevel(LogLevel.Warn)) {
console.warn(this.formatMessage(name, msg));
console.warn(this.formatMessage(componetName, message));
}
}

error(name: string, msg: string): void {
error(componentName: string, message: string): void {
if (this.aboveLogLevel(LogLevel.Error)) {
console.error(this.formatMessage(name, msg));
console.error(this.formatMessage(componentName, message));
if (this.throwOnError) {
throw new Error(msg);
throw new Error(`[${componentName}] ${message}`);
}
}
}

info(name: string, msg: string): void {
info(componentName: string, message: string): void {
if (this.aboveLogLevel(LogLevel.Info)) {
console.info(this.formatMessage(name, msg));
console.info(this.formatMessage(componentName, message));
}
}
}

0 comments on commit 541cc0c

Please sign in to comment.