Skip to content

Commit 8ff963c

Browse files
committed
added defaults
1 parent 55f0f5b commit 8ff963c

File tree

3 files changed

+89
-126
lines changed

3 files changed

+89
-126
lines changed

src/util/configuration/config.ts

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,96 +5,10 @@
55
* Licensed under the MIT License. See License.md in the project root for license information.
66
* -------------------------------------------------------------------------------------------- */
77
'use strict';
8-
import {
9-
ConfigurationChangeEvent,
10-
ConfigurationScope,
11-
Event,
12-
EventEmitter,
13-
ExtensionContext,
14-
extensions,
15-
StatusBarAlignment,
16-
workspace,
17-
} from 'vscode';
8+
import { ConfigurationChangeEvent, ConfigurationScope, Event, EventEmitter, ExtensionContext, workspace } from 'vscode';
189
import { constants } from '../constants';
19-
import { Logger, TraceLevel } from '../logger';
20-
import { LineNumberFrequency } from '../lineNumberer';
21-
22-
import { GCodeUnits } from '../../gcodeUnits';
23-
24-
export interface GCodeConfiguration {
25-
general: {
26-
machineType: 'Mill' | 'Lathe' | '3D Printer';
27-
28-
hovers: {
29-
enabled: boolean;
30-
};
31-
32-
statusBars: {
33-
enabled: boolean;
34-
alignment: StatusBarAlignment;
35-
};
36-
37-
units: GCodeUnits;
38-
39-
outputLevel: TraceLevel;
40-
};
41-
42-
lineNumberer: {
43-
addSpaceAfter: boolean;
44-
frequency: LineNumberFrequency;
45-
ignoreBlank: boolean;
46-
ignoreProgramNumbers: boolean;
47-
};
48-
49-
views: {
50-
maxAutoRefresh: number;
51-
52-
navTree: {
53-
autoRefresh: boolean;
54-
};
55-
56-
stats: {
57-
autoRefresh: boolean;
58-
};
59-
};
60-
61-
webviews: {
62-
enabled: boolean;
63-
};
64-
}
65-
66-
export const defaults: GCodeConfiguration = {
67-
general: {
68-
machineType: 'Mill',
69-
hovers: {
70-
enabled: true,
71-
},
72-
statusBars: {
73-
enabled: true,
74-
alignment: StatusBarAlignment.Left,
75-
},
76-
units: GCodeUnits.Auto,
77-
outputLevel: TraceLevel.Verbose,
78-
},
79-
lineNumberer: {
80-
addSpaceAfter: true,
81-
frequency: LineNumberFrequency.EveryLine,
82-
ignoreBlank: true,
83-
ignoreProgramNumbers: true,
84-
},
85-
views: {
86-
maxAutoRefresh: 10000,
87-
navTree: {
88-
autoRefresh: true,
89-
},
90-
stats: {
91-
autoRefresh: false,
92-
},
93-
},
94-
webviews: {
95-
enabled: true,
96-
},
97-
};
10+
import { Logger } from '../logger';
11+
import { GCodeConfiguration } from './defaults';
9812

9913
export class Config {
10014
private static _defaults: GCodeConfiguration;
@@ -134,28 +48,6 @@ export class Config {
13448
changed(e: ConfigurationChangeEvent, section: string, scope?: ConfigurationScope): boolean {
13549
return e.affectsConfiguration(`${constants.configId}.${section}`, scope) ?? true;
13650
}
137-
138-
static get defaults() {
139-
return this._defaults;
140-
}
141-
142-
private buildDefaults() {
143-
const gcode = extensions.getExtension(constants.extensionQualifiedId);
144-
145-
if (gcode) {
146-
const pkgCfg = gcode.packageJSON.contrubutes.configuration;
147-
}
148-
}
14951
}
15052

15153
export const configuration = new Config();
152-
153-
export const cfg = new Proxy(defaults, {
154-
get: function (name: GCodeConfiguration, prop: string): string | undefined {
155-
if (!(prop in name)) {
156-
return undefined;
157-
}
158-
},
159-
});
160-
161-
cfg.general.machineType;

src/util/configuration/defaults.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
31
/* ---------------------------------------------------------------------------------------------
42
* Copyright (c) Applied Eng & Design All rights reserved.
53
* Licensed under the MIT License. See License.md in the project root for license information.
64
* -------------------------------------------------------------------------------------------- */
75

86
'use strict';
97

10-
import { extensions, StatusBarAlignment } from 'vscode';
8+
import { StatusBarAlignment } from 'vscode';
119
import { GCodeUnits } from '../../gcodeUnits';
12-
import { constants } from '../constants';
1310
import { LineNumberFrequency } from '../lineNumberer';
14-
import { TraceLevel } from '../logger';
11+
12+
export enum TraceLevel {
13+
Silent = 'silent',
14+
Errors = 'errors',
15+
Verbose = 'verbose',
16+
Debug = 'debug',
17+
}
1518

1619
export interface GCodeConfiguration {
1720
general: {
@@ -55,8 +58,6 @@ export interface GCodeConfiguration {
5558
};
5659
}
5760

58-
const pkgCfg = extensions.getExtension(constants.extensionQualifiedId)!.packageJSON.contrubutes.configuration;
59-
6061
export const defaults: GCodeConfiguration = {
6162
general: {
6263
machineType: 'Mill',

src/util/logger.ts

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,44 @@
44
* -------------------------------------------------------------------------------------------- */
55

66
'use strict';
7-
import { ExtensionContext, OutputChannel, window } from 'vscode';
7+
import { ConfigurationChangeEvent, ExtensionContext, OutputChannel, window } from 'vscode';
8+
import { configuration } from './configuration/config';
9+
import { defaults, TraceLevel } from './configuration/defaults';
810
import { constants } from './constants';
911

12+
const configTraceLevel = 'general.outputLevel';
13+
1014
export class Logger {
11-
static output: OutputChannel | undefined;
15+
private static output: OutputChannel | undefined;
16+
private static _level: TraceLevel = TraceLevel.Verbose;
1217

13-
static initialize(_context: ExtensionContext) {
18+
static initialize(context: ExtensionContext, level?: TraceLevel) {
19+
this.level = level ?? this._level;
1420
this.output = this.output || window.createOutputChannel(constants.extensionOutputChannelName);
21+
22+
context.subscriptions.push(configuration.onDidChange(this.onConfigurationChanged, this));
23+
}
24+
25+
private static onConfigurationChanged(e: ConfigurationChangeEvent) {
26+
if (configuration.changed(e, configTraceLevel)) {
27+
this.level = configuration.getParam(configTraceLevel) ?? defaults.general.outputLevel;
28+
}
29+
}
30+
31+
static get level() {
32+
return this._level;
33+
}
34+
35+
static set level(value: TraceLevel) {
36+
this._level = value;
37+
38+
if (value === TraceLevel.Silent) {
39+
if (this.output != null) {
40+
this.close();
41+
}
42+
} else {
43+
this.output = this.output ?? window.createOutputChannel(constants.extensionOutputChannelName);
44+
}
1545
}
1646

1747
static enable() {
@@ -22,7 +52,47 @@ export class Logger {
2252
this.output.show();
2353
}
2454

55+
static debug(message: string): void {
56+
if (this.level !== TraceLevel.Debug) {
57+
return;
58+
}
59+
60+
if (this.output !== undefined) {
61+
this.output.appendLine(message);
62+
}
63+
}
64+
65+
static error(err: Error, message?: string, handled = false): void {
66+
console.error(`${handled ? 'H' : 'Unh'}andled Error: ${err.stack || err.message || err.toString()}`);
67+
68+
if (this.level === TraceLevel.Silent) {
69+
return;
70+
}
71+
72+
if (!err) {
73+
return;
74+
}
75+
76+
if (this.output !== undefined) {
77+
this.output.appendLine(
78+
`${handled ? 'H' : 'Unh'}andled Error: ${err.stack || err.message || err.toString()}`,
79+
);
80+
}
81+
}
82+
2583
static log(message: string): void {
84+
if (this.level === TraceLevel.Verbose || this.level === TraceLevel.Debug) {
85+
if (this.output !== undefined) {
86+
this.output.appendLine(`${this.level === TraceLevel.Debug ? this.timestamp : ''} ${message}`);
87+
}
88+
}
89+
}
90+
91+
static warn(message: string): void {
92+
if (this.level === TraceLevel.Silent) {
93+
return;
94+
}
95+
2696
if (this.output !== undefined) {
2797
this.output.appendLine(message);
2898
}
@@ -35,10 +105,10 @@ export class Logger {
35105
}
36106
}
37107

38-
static error(err: Error, handled = false): void {
39-
if (!err) {
40-
return;
41-
}
42-
console.error(`${handled ? 'H' : 'Unh'}andled Error: ${err.stack || err.message || err.toString()}`);
108+
private static get timestamp(): string {
109+
const now = new Date();
110+
return `[${now.toISOString().replace(/T/, ' ').replace(/\..+/, '')}:${`00${now.getUTCMilliseconds()}`.slice(
111+
-3,
112+
)}]`;
43113
}
44114
}

0 commit comments

Comments
 (0)