Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization of log string generation #2292

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions lib/statescontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
} else {
states = stateModel.states;
}

Check failure on line 196 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
if (typeof states === 'function' && !states.prototype) {
const entity = await this.adapter.zbController.resolveEntity(deviceId);
if (entity) {
Expand Down Expand Up @@ -597,28 +598,39 @@
}

async publishToState(devId, model, payload) {
const devStates = await this.getDevStates(`0x${devId}`, model);
let has_debug = false;
if (this.checkDebugDevice(devId))
{
if (!payload.hasOwnProperty('msg_from_zigbee')) {
this.warn(`ELEVATED I1: message received '${JSON.stringify(payload)}' from device ${devId} type '${model}'`);
has_debug = true;
}

Check failure on line 601 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
var isMsg_from_zigbee = payload.hasOwnProperty('msg_from_zigbee');

Check warning on line 602 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Unexpected var, use let or const instead
var isCommonState = isMsg_from_zigbee||

Check warning on line 603 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Unexpected var, use let or const instead
payload.hasOwnProperty('linkquality');

let has_debug = ! isMsg_from_zigbee && this.checkDebugDevice(devId);

Check warning on line 606 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

'has_debug' is never reassigned. Use 'const' instead

if (has_debug){
this.warn(`ELEVATED I1(publishToState): message received '${JSON.stringify(payload)}' from device ${devId} type '${model}'`);

Check failure on line 609 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Expected indentation of 12 spaces but found 13
}
if (!devStates) {
if (has_debug) this.error(`ELEVATED IE2: no device states for device ${devId} type '${model}'`)
return;

let states = undefined;

Check failure on line 612 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
if (isCommonState){

Check failure on line 613 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
states = statesMapping.commonStates;

Check failure on line 614 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Expected indentation of 12 spaces but found 13
}
// find states for payload
let has_published = false;
else{

Check failure on line 616 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
const devStates = await this.getDevStates(`0x${devId}`, model);

if (devStates.states !== undefined) {
try {
const states = statesMapping.commonStates.concat(
devStates.states.filter(statedesc => payload.hasOwnProperty(statedesc.prop || statedesc.id))
);
if (!devStates) {
if (has_debug) this.error(`ELEVATED IE2 (publishToState): no device states for device ${devId} type '${model}'`)

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (96% of all statements in
the enclosing function
have an explicit semicolon).
return;
}

Check failure on line 623 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
states = statesMapping.commonStates.concat(
devStates.states.filter(statedesc => payload.hasOwnProperty(statedesc.prop || statedesc.id))
);
}

let has_published = false;

Check failure on line 630 in lib/statescontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
// find states for payload
if (states!== undefined) {
try {
for (const stateInd in states) {
const statedesc = states[stateInd];
let value;
Expand All @@ -634,8 +646,8 @@

let stateID = statedesc.id;

if (has_debug && statedesc.id !== 'msg_from_zigbee') {
this.warn(`ELEVATED I2: value generated '${JSON.stringify(value)}' from device ${devId} for '${statedesc.name}'`);
if (has_debug) {
this.warn(`ELEVATED I2 (publishToState): value generated '${JSON.stringify(value)}' from device ${devId} for '${statedesc.name}'`);
}

const common = {
Expand Down
9 changes: 4 additions & 5 deletions lib/zigbeecontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@
}

async handleDeviceJoined(message) {
this.debug('handleDeviceJoined', message);
this.warn('handleDeviceJoined', message);
//const entity = await this.resolveEntity(message.device || message.ieeeAddr);
//this.emit('new', entity);
}
Expand Down Expand Up @@ -697,13 +697,12 @@
}

async handleMessage(data) {
this.debug(`handleMessage`, data);
//this.debug(`handleMessage`, data);
const entity = await this.resolveEntity(data.device || data.ieeeAddr);
const name = (entity && entity._modelID) ? entity._modelID : data.device.ieeeAddr;
this.debug(
`Received Zigbee message from '${name}', type '${data.type}', cluster '${data.cluster}'` +
`, data '${JSON.stringify(data.data)}' from endpoint ${data.endpoint.ID}` +
(data.hasOwnProperty('groupID') ? ` with groupID ${data.groupID}` : ``)
`HandleMessage. Received Zigbee message from '${name}', type '${data.type}', cluster '${data.cluster}', from endpoint ${data.endpoint.ID}` +
(data.hasOwnProperty('groupID') ? ` with groupID ${data.groupID}` : ``) + ` data:`,data.data

Check failure on line 705 in lib/zigbeecontroller.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
);
this.event(data.type, entity, data);

Expand Down
37 changes: 30 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@
}

async onZigbeeEvent(type, entity, message) {
this.log.debug(`Type ${type} device ${safeJsonStringify(entity)} incoming event: ${safeJsonStringify(message)}`);
//Optimization: Use checkDebugDevice as entity and message objects can be very large (ie PTVO 8chanels relay safeJsonStringify(entity) -> 90 kb size)
//this.log.debug(`Type ${type} device ${safeJsonStringify(entity)} incoming event: ${safeJsonStringify(message)}`);

const device = entity.device;
const mappedModel = entity.mapped;
Expand All @@ -470,15 +471,22 @@
const has_elevated_debug = this.stController.checkDebugDevice(devId);

if (has_elevated_debug) {
//Full log:
//this.log.debug(`ELEVATED: Zigbee Event of Type "${type}" from device: ${safeJsonStringify(entity)}`);
//this.log.debug(`ELEVATED: incoming event:${safeJsonStringify(message)}`);
const shortMessage = {};
for(const propertyName in message) {
shortMessage[propertyName] = message[propertyName];
}
shortMessage.device = device.ieeeAddr;
shortMessage.meta = undefined;
shortMessage.endpoint = (message.endpoint.ID ? message.endpoint.ID: -1);
this.log.warn(`ELEVATED I0: Zigbee Event of Type ${type} from device ${safeJsonStringify(device.ieeeAddr)}, incoming event: ${safeJsonStringify(shortMessage)}`);
}
this.log.warn(`ELEVATED I0: Zigbee Event of Type "${type}" from device "${safeJsonStringify(device.ieeeAddr)}" incoming event: ${safeJsonStringify(shortMessage)}`);
}else
{
this.onLog('debug',`Zigbee Event of Type ${type} from device ${devId}. incoming event:`, message);
};

// this assigment give possibility to use iobroker logger in code of the converters, via meta.logger
meta.logger = this.log;

Expand Down Expand Up @@ -580,7 +588,11 @@
async processConverters(converters, devId, model, mappedModel, message, meta) {
for (const converter of converters) {
const publish = (payload) => {
this.log.debug(`Publish ${safeJsonStringify(payload)} to ${safeJsonStringify(devId)}`);

if (this.stController.checkDebugDevice(devId))
this.log.warn(`ELEVATED: ProcessConverters devId:${devId} payload: ${safeJsonStringify(payload)}`);
else this.onLog('debug', `Publish to ${devId}. payload:`, payload);

if (typeof payload === 'object') {
this.publishToState(devId, model, payload);
}
Expand Down Expand Up @@ -629,15 +641,21 @@
let isGroup = false;
const has_elevated_debug = this.stController.checkDebugDevice(deviceId)

this.log.debug(`publishFromState : ${deviceId} ${model} ${safeJsonStringify(stateList)}`);

if(has_elevated_debug)
this.log.warn(`ELEVATED: PublishFromState : ${deviceId} ${model} stateList: ${safeJsonStringify(stateList)}`)
else this.onLog('debug',`PublishFromState : ${deviceId} ${model}. stateList:`, stateList);

if (model === 'group') {
isGroup = true;
deviceId = parseInt(deviceId);
}
try {
const entity = await this.zbController.resolveEntity(deviceId);

this.log.debug(`entity: ${deviceId} ${model} ${safeJsonStringify(entity)}`);
if (has_elevated_debug)
this.log.warn(`ELEVATED: PublishFromState. ${deviceId} ${model} entity: ${safeJsonStringify(entity)}`)
else this.onLog('debug',`PublishFromState : ${deviceId} ${model} entity:`, entity);

const mappedModel = entity.mapped;

Expand Down Expand Up @@ -1090,7 +1108,12 @@
if (typeof data === 'string') {
logger(`${msg}. ${data}`);
} else {
logger(`${msg}. ${safeJsonStringify(data)}`);
if ((this.log.level==level) ||
((this.log.level='warn')&&(level='error')) ||

Check warning

Code scanning / CodeQL

Useless conditional Warning

This expression always evaluates to true.
This logical 'and' expression can be replaced with a comma expression.

Check warning

Code scanning / CodeQL

Useless conditional Warning

This expression always evaluates to true.

Check warning

Code scanning / CodeQL

Useless conditional Warning

This expression always evaluates to true.
((this.log.level='info')&&(level='error' || level=='warn'))

Check warning

Code scanning / CodeQL

Useless conditional Warning

This logical 'and' expression can be replaced with a comma expression.

Check warning

Code scanning / CodeQL

Useless conditional Warning

This expression always evaluates to true.
)
logger(`${msg}. ${safeJsonStringify(data)}`)
Fixed Show fixed Hide fixed

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (93% of all statements in
the enclosing function
have an explicit semicolon).
else logger(`${msg}. DATA unavailable. Check log level. MsgLevel:${level} AdaptorLevel:${this.log.level}`);
}
} else {
logger(msg);
Expand Down
Loading