Skip to content

Commit

Permalink
feat: Improve logging (#989)
Browse files Browse the repository at this point in the history
* feat: Improve logging

* feat: Improve logging

* Finer log levels for base classes.

* Lower z2m dups to `debug`. Fixup GP. Fix tests.

* Lower a few more z2m dups to `debug`.

* Refactored to use namespacing directly.

* change prefix

* updates

* fix tests

* Reduce `ember` NCP counters logging footprint.

* Add tests for unset logger.

---------

Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
  • Loading branch information
Koenkk and Nerivec authored Apr 2, 2024
1 parent 25b9918 commit 0cf7b45
Show file tree
Hide file tree
Showing 53 changed files with 1,434 additions and 1,522 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@
"scripts": {
"build": "./node_modules/.bin/tsc",
"start": "./node_modules/.bin/tsc -w",
"test": "jest test",
"test-with-coverage": "jest test --coverage --collectCoverageFrom 'src/**/*.ts'",
"test-watch": "jest test --watch",
"test": "jest test --silent",
"test-with-coverage": "jest test --silent --coverage --collectCoverageFrom 'src/**/*.ts'",
"test-watch": "jest test --silent --watch",
"eslint": "node_modules/.bin/eslint . --ext .ts --max-warnings=0",
"docs": "typedoc --tsconfig typedoc-tsconfig.json",
"clean": "rimraf temp coverage dist",
Expand Down
36 changes: 15 additions & 21 deletions src/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@ import * as TsType from './tstype';
import {ZclDataPayload} from './events';
import events from 'events';
import {ZclFrame, FrameType, Direction} from '../zcl';
import Debug from "debug";
import {LoggerStub} from "../controller/logger-stub";
import * as Models from "../models";
import Bonjour, {Service} from 'bonjour-service';
import {logger} from '../utils/logger';

const debug = Debug("zigbee-herdsman:adapter");
const NS = 'zh:adapter';

abstract class Adapter extends events.EventEmitter {
public readonly greenPowerGroup = 0x0b84;
protected networkOptions: TsType.NetworkOptions;
protected adapterOptions: TsType.AdapterOptions;
protected serialPortOptions: TsType.SerialPortOptions;
protected backupPath: string;
protected logger?: LoggerStub;

protected constructor(
networkOptions: TsType.NetworkOptions, serialPortOptions: TsType.SerialPortOptions, backupPath: string,
adapterOptions: TsType.AdapterOptions, logger?: LoggerStub)
{
protected constructor(networkOptions: TsType.NetworkOptions, serialPortOptions: TsType.SerialPortOptions, backupPath: string,
adapterOptions: TsType.AdapterOptions) {
super();
this.networkOptions = networkOptions;
this.adapterOptions = adapterOptions;
this.serialPortOptions = serialPortOptions;
this.backupPath = backupPath;
this.logger = logger;
}

/**
Expand All @@ -38,7 +33,6 @@ abstract class Adapter extends events.EventEmitter {
serialPortOptions: TsType.SerialPortOptions,
backupPath: string,
adapterOptions: TsType.AdapterOptions,
logger?: LoggerStub,
): Promise<Adapter> {
const {ZStackAdapter} = await import('./z-stack/adapter');
const {DeconzAdapter} = await import('./deconz/adapter');
Expand Down Expand Up @@ -68,11 +62,11 @@ abstract class Adapter extends events.EventEmitter {
let adapter: AdapterImplementation = adapters[0];

if (!serialPortOptions.path) {
debug('No path provided, auto detecting path');
logger.debug('No path provided, auto detecting path', NS);
for (const candidate of adapters) {
const path = await candidate.autoDetectPath();
if (path) {
debug(`Auto detected path '${path}' from adapter '${candidate.name}'`);
logger.debug(`Auto detected path '${path}' from adapter '${candidate.name}'`, NS);
serialPortOptions.path = path;
adapter = candidate;
break;
Expand All @@ -93,7 +87,7 @@ abstract class Adapter extends events.EventEmitter {
const bj = new Bonjour();
const mdnsTimeout = 2000; // timeout for mdns scan

logger.info(`Starting mdns discovery for coordinator: ${mdnsDevice}`);
logger.info(`Starting mdns discovery for coordinator: ${mdnsDevice}`, NS);
await new Promise((resolve, reject) => {
bj.findOne({type: mdnsDevice}, mdnsTimeout, function (service: Service) {
if (service) {
Expand All @@ -103,10 +97,10 @@ abstract class Adapter extends events.EventEmitter {
const mdnsAdapter = (service.txt.radio_type == 'znp' ?
'zstack' : service.txt.radio_type) as TsType.SerialPortOptions['adapter'];
const mdnsBaud = parseInt(service.txt.baud_rate);
logger.info(`Coordinator Ip: ${mdnsIp}`);
logger.info(`Coordinator Port: ${mdnsPort}`);
logger.info(`Coordinator Radio: ${mdnsAdapter}`);
logger.info(`Coordinator Baud: ${mdnsBaud}\n`);
logger.info(`Coordinator Ip: ${mdnsIp}`, NS);
logger.info(`Coordinator Port: ${mdnsPort}`, NS);
logger.info(`Coordinator Radio: ${mdnsAdapter}`, NS);
logger.info(`Coordinator Baud: ${mdnsBaud}\n`, NS);
bj.destroy();
serialPortOptions.path = `tcp://${mdnsIp}:${mdnsPort}`;
serialPortOptions.adapter = mdnsAdapter;
Expand All @@ -116,7 +110,7 @@ abstract class Adapter extends events.EventEmitter {
&& serialPortOptions.adapter !== 'auto') {
adapter = adapterLookup[serialPortOptions.adapter];
resolve(
new adapter(networkOptions, serialPortOptions, backupPath, adapterOptions, logger)
new adapter(networkOptions, serialPortOptions, backupPath, adapterOptions)
);
} else {
reject(new Error(`Adapter ${serialPortOptions.adapter} is not supported.`));
Expand All @@ -142,17 +136,17 @@ abstract class Adapter extends events.EventEmitter {
// Determine adapter to use
for (const candidate of adapters) {
if (await candidate.isValidPath(serialPortOptions.path)) {
debug(`Path '${serialPortOptions.path}' is valid for '${candidate.name}'`);
logger.debug(`Path '${serialPortOptions.path}' is valid for '${candidate.name}'`, NS);
adapter = candidate;
break;
}
}
} catch (error) {
debug(`Failed to validate path: '${error}'`);
logger.debug(`Failed to validate path: '${error}'`, NS);
}
}

return new adapter(networkOptions, serialPortOptions, backupPath, adapterOptions, logger);
return new adapter(networkOptions, serialPortOptions, backupPath, adapterOptions);
}

public abstract start(): Promise<TsType.StartResult>;
Expand Down
Loading

0 comments on commit 0cf7b45

Please sign in to comment.