• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rokucommunity / brighterscript / #12676

13 Jun 2024 11:41AM UTC coverage: 87.939%. Remained the same
#12676

push

web-flow
fix: conform bsconfig.schema.json to strict types (#1205)

* fix: conform bsconfig.schema.json to strict types

* chore: move deprecate message to description

* Restore deprecationMessage, add unit test

---------

Co-authored-by: Bart van den Ende <bart.van.den.ende@wbd.com>
Co-authored-by: Bronley Plumb <bronley@gmail.com>

6068 of 7374 branches covered (82.29%)

Branch coverage included in aggregate %.

8791 of 9523 relevant lines covered (92.31%)

1741.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.18
/src/Logger.ts
1
import chalk from 'chalk';
1✔
2
import * as moment from 'moment';
1✔
3
import { EventEmitter } from 'eventemitter3';
1✔
4
import { Stopwatch } from './Stopwatch';
1✔
5
import { LogLevelNumeric as LogLevel } from '@rokucommunity/logger';
1✔
6
/**
7
 * @deprecated use the `createLogger` function instead
8
 */
9
export class Logger {
1✔
10

11
    public static subscribe(callback) {
12
        this.emitter.on('log', callback);
×
13
        return () => {
×
14
            this.emitter.off('log', callback);
×
15
        };
16
    }
17
    private static emitter = new EventEmitter();
1✔
18

19
    /**
20
     * A string with whitespace used for indenting all messages
21
     */
22
    private indent = '';
×
23

24
    constructor(
25
        logLevel?: LogLevel,
26
        public prefix?: string
×
27
    ) {
28
        this.logLevel = logLevel ?? LogLevel.log;
×
29
    }
30

31
    public get logLevel() {
32
        return this._logLevel;
×
33
    }
34
    public set logLevel(value: LogLevel) {
35
        //cast the string version to the numberic version
36
        if (typeof (value) === 'string') {
×
37
            value = LogLevel[value] as any;
×
38
        }
39
        this._logLevel = value ?? LogLevel.log;
×
40
    }
41
    private _logLevel = LogLevel.log;
×
42

43
    public getTimestamp() {
44
        return '[' + chalk.grey(moment().format(`hh:mm:ss:SSSS A`)) + ']';
×
45
    }
46

47
    private writeToLog(method: (...consoleArgs: any[]) => void, ...args: any[]) {
48
        if (this._logLevel === LogLevel.trace) {
×
49
            method = console.trace;
×
50
        }
51
        let finalArgs: any[] = [];
×
52
        //evaluate any functions to get their values.
53
        //This allows more complicated values to only be evaluated if this log level is active
54
        for (let arg of args) {
×
55
            if (arg instanceof Function) {
×
56
                arg = arg();
×
57
            }
58
            finalArgs.push(arg);
×
59
        }
60
        const allArgs = [
×
61
            this.getTimestamp()
62
        ];
63
        if (this.prefix) {
×
64
            allArgs.push(this.prefix);
×
65
        }
66
        allArgs.push(this.indent);
×
67

68
        method.call(console, ...allArgs, ...finalArgs);
×
69
        if (Logger.emitter.listenerCount('log') > 0) {
×
70
            Logger.emitter.emit('log', finalArgs.join(' '));
×
71
        }
72
    }
73

74
    /**
75
     * Log an error message to the console
76
     */
77
    error(...messages) {
78
        if (this._logLevel >= LogLevel.error) {
×
79
            this.writeToLog(console.error, ...messages);
×
80
        }
81
    }
82

83
    /**
84
     * Log a warning message to the console
85
     */
86
    warn(...messages) {
87
        if (this._logLevel >= LogLevel.warn) {
×
88
            this.writeToLog(console.warn, ...messages);
×
89
        }
90
    }
91

92
    /**
93
     * Log a standard log message to the console
94
     */
95
    log(...messages) {
96
        if (this._logLevel >= LogLevel.log) {
×
97
            this.writeToLog(console.log, ...messages);
×
98
        }
99
    }
100
    /**
101
     * Log an info message to the console
102
     */
103
    info(...messages) {
104
        if (this._logLevel >= LogLevel.info) {
×
105
            this.writeToLog(console.info, ...messages);
×
106
        }
107
    }
108

109
    /**
110
     * Log a debug message to the console
111
     */
112
    debug(...messages) {
113
        if (this._logLevel >= LogLevel.debug) {
×
114
            this.writeToLog(console.debug, ...messages);
×
115
        }
116
    }
117

118
    /**
119
     * Log a debug message to the console
120
     */
121
    trace(...messages) {
122
        if (this._logLevel >= LogLevel.trace) {
×
123
            this.writeToLog(console.trace, ...messages);
×
124
        }
125
    }
126

127
    /**
128
     * Writes to the log (if logLevel matches), and also times how long the action took to occur.
129
     * `action` is called regardless of logLevel, so this function can be used to nicely wrap
130
     * pieces of functionality.
131
     * The action function also includes two parameters, `pause` and `resume`, which can be used to improve timings by focusing only on
132
     * the actual logic of that action.
133
     */
134
    time<T>(logLevel: LogLevel, messages: any[], action: (pause: () => void, resume: () => void) => T): T {
135
        //call the log if loglevel is in range
136
        if (this._logLevel >= logLevel) {
×
137
            const stopwatch = new Stopwatch();
×
138
            const logLevelString = LogLevel[logLevel];
×
139

140
            //write the initial log
141
            this[logLevelString](...messages ?? []);
×
142
            this.indent += '  ';
×
143

144
            stopwatch.start();
×
145
            //execute the action
146
            const result = action(stopwatch.stop.bind(stopwatch), stopwatch.start.bind(stopwatch)) as any;
×
147

148
            //return a function to call when the timer is complete
149
            const done = () => {
×
150
                stopwatch.stop();
×
151
                this.indent = this.indent.substring(2);
×
152
                this[logLevelString](...messages ?? [], `finished. (${chalk.blue(stopwatch.getDurationText())})`);
×
153
            };
154

155
            //if this is a promise, wait for it to resolve and then return the original result
156
            if (typeof result?.then === 'function') {
×
157
                return Promise.resolve(result).then(done).then(() => {
×
158
                    return result;
×
159
                }) as any;
160
            } else {
161
                //this was not a promise. finish the timer now
162
                done();
×
163
                return result;
×
164
            }
165
        } else {
166
            return action(noop, noop);
×
167
        }
168
    }
169
}
170

171
export function noop() {
1✔
172

173
}
174

175
export { LogLevelNumeric as LogLevel } from '@rokucommunity/logger';
12,904✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc