-
Notifications
You must be signed in to change notification settings - Fork 471
/
Copy pathsignale.ts
155 lines (147 loc) · 5.35 KB
/
signale.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Signale } from 'signale'
import chalk from 'chalk'
import { inspect } from 'util'
import getErrorReason from './mqttErrorReason'
const option = {
config: {
displayLabel: false,
displayDate: true,
displayTimestamp: true,
},
}
const signale = new Signale(option)
const msgLog = (msg: Record<string, unknown>[]) => {
let chalkString = ''
msg.forEach((item) => {
if (typeof item.value === 'object') {
chalkString += `${chalk.green(item.label)}: ${inspect(item.value, false, null, true)}\n`
} else {
chalkString += `${chalk.green(item.label)}: ${item.value}\n`
}
})
signale.log(`${chalkString}`)
}
const basicLog = {
connecting: (config: boolean | string | undefined, host: string, port = 1883, topic?: string, message?: string) => {
if (!config) {
signale.await('Connecting...')
} else {
signale.await(
`Connecting using configuration file, host: ${host}, port: ${port}${topic ? `, topic: ${topic}` : ''}${
message ? `, message: ${message}` : ''
}`,
)
}
},
connected: () => signale.success('Connected'),
subscribing: (t: string) => signale.await(`Subscribing to ${t}...`),
subscribed: (t: string) => signale.success(`Subscribed to ${t}`),
subscriptionNegated: (sub: { topic: string; qos: number }) =>
signale.error(`Subscription negated to ${sub.topic} with code ${sub.qos}`),
publishing: () => signale.await('Message publishing...'),
published: () => signale.success('Message published'),
enterToPublish: () => signale.success('Connected, press Enter to publish, press Ctrl+C to exit'),
error: (err: Error) => signale.error(err),
close: () => signale.error('Connection closed'),
reconnecting: () => signale.await('Reconnecting...'),
reconnectTimesLimit: () => signale.error('Exceed the maximum reconnect times limit, stop retry'),
disconnect: (packet: IDisconnectPacket, clientId?: string) => {
const { reasonCode } = packet
const reason = reasonCode === 0 ? 'Normal disconnection' : getErrorReason(reasonCode)
signale.warn(
`${
clientId ? `Client ID: ${clientId}, ` : ''
}The Broker has actively disconnected, Reason: ${reason} (Code: ${reasonCode})`,
)
},
fileReading: () => signale.await('Reading file...'),
fileReadSuccess: () => signale.success('Read file successfully'),
}
const benchLog = {
start: {
conn: (config: boolean | string | undefined, count: number, interval: number, host: string, port = 1883) => {
if (!config) {
signale.info(`Start the connect benchmarking, connections: ${count}, req interval: ${interval}ms`)
} else {
signale.info(
`Start the connect benchmarking, connections: ${count}, req interval: ${interval}ms, host: ${host}, port: ${port}`,
)
}
},
sub: (
config: boolean | string | undefined,
count: number,
interval: number,
host: string,
port = 1883,
topic: string,
) => {
if (!config) {
signale.info(
`Start the subscribe benchmarking, connections: ${count}, req interval: ${interval}ms, topic: ${topic}`,
)
} else {
signale.info(
`Start the subscribe benchmarking, connections: ${count}, req interval: ${interval}ms, host: ${host}, port: ${port}, topic: ${topic}`,
)
}
},
pub: (
config: boolean | string | undefined,
count: number,
interval: number,
messageInterval: number,
host: string,
port = 1883,
topic: string,
message: string,
) => {
if (!config) {
signale.info(
`Start the publish benchmarking, connections: ${count}, req interval: ${interval}ms, message interval: ${messageInterval}ms`,
)
} else {
signale.info(
`Start the publish benchmarking, connections: ${count}, req interval: ${interval}ms, message interval: ${messageInterval}ms, host: ${host}, port: ${port}, topic: ${topic}, message: ${message}`,
)
}
},
},
error: (count: number, total: number, id: string, err: Error) => {
signale.error(`[${count}/${total}] - Client ID: ${id}, ${err}`)
},
close: (count: number, total: number, id: string) => {
signale.error(`[${count}/${total}] - Client ID: ${id}, Connection closed`)
},
reconnecting: (count: number, total: number, id: string) => {
signale.await(`[${count}/${total}] - Client ID: ${id}, Reconnecting...`)
},
reconnected: (count: number, total: number, id: string) => {
signale.success(`[${count}/${total}] - Client ID: ${id}, Reconnected`)
},
reconnectTimesLimit: (count: number, total: number, id: string) => {
signale.error(`[${count}/${total}] - Client ID: ${id}, Exceed the maximum reconnect times limit, stop retry`)
},
}
const simulateLog = {
start: {
pub: (
config: boolean | string | undefined,
count: number,
interval: number,
messageInterval: number,
host: string,
port = 1883,
topic: string,
scenario: string,
) => {
let message = `Start simulation publishing, scenario: ${scenario}, connections: ${count}, req interval: ${interval}ms, message interval: ${messageInterval}ms`
if (config) {
message += `, host: ${host}, port: ${port}, topic: ${topic}`
}
signale.info(message)
},
},
}
export { Signale, signale, msgLog, basicLog, benchLog, simulateLog }
export default signale