Welcome to mirror list, hosted at ThFree Co, Russian Federation.

log.ts « src « git « extensions - github.com/microsoft/vscode.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0f96ff0216a7287cb0c46b1c5a8d15ffee51df6 (plain)
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
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();

import { commands, Disposable, Event, EventEmitter, OutputChannel, window, workspace } from 'vscode';
import { dispose } from './util';

/**
 * The severity level of a log message
 */
export enum LogLevel {
	Trace = 1,
	Debug = 2,
	Info = 3,
	Warning = 4,
	Error = 5,
	Critical = 6,
	Off = 7
}

/**
 * Output channel logger
 */
export class OutputChannelLogger {

	private _onDidChangeLogLevel = new EventEmitter<LogLevel>();
	readonly onDidChangeLogLevel: Event<LogLevel> = this._onDidChangeLogLevel.event;

	private _currentLogLevel!: LogLevel;
	get currentLogLevel(): LogLevel {
		return this._currentLogLevel;
	}
	set currentLogLevel(value: LogLevel) {
		if (this._currentLogLevel === value) {
			return;
		}

		this._currentLogLevel = value;
		this._onDidChangeLogLevel.fire(value);

		this.log(localize('gitLogLevel', "Log level: {0}", LogLevel[value]));
	}

	private _defaultLogLevel!: LogLevel;
	get defaultLogLevel(): LogLevel {
		return this._defaultLogLevel;
	}

	private _outputChannel: OutputChannel;
	private _disposables: Disposable[] = [];

	constructor() {
		// Output channel
		this._outputChannel = window.createOutputChannel('Git');
		commands.registerCommand('git.showOutput', () => this.showOutputChannel());
		this._disposables.push(this._outputChannel);

		this._disposables.push(workspace.onDidChangeConfiguration(e => {
			if (e.affectsConfiguration('git.logLevel')) {
				this.onLogLevelChange();
			}
		}));
		this.onLogLevelChange();
	}

	private onLogLevelChange(): void {
		const config = workspace.getConfiguration('git');
		const logLevel: keyof typeof LogLevel = config.get('logLevel', 'Info');
		this.currentLogLevel = this._defaultLogLevel = LogLevel[logLevel] ?? LogLevel.Info;
	}

	log(message: string, logLevel?: LogLevel): void {
		if (logLevel && logLevel < this._currentLogLevel) {
			return;
		}

		this._outputChannel.appendLine(`[${new Date().toISOString()}]${logLevel ? ` [${LogLevel[logLevel].toLowerCase()}]` : ''} ${message}`);
	}

	logCritical(message: string): void {
		this.log(message, LogLevel.Critical);
	}

	logDebug(message: string): void {
		this.log(message, LogLevel.Debug);
	}

	logError(message: string): void {
		this.log(message, LogLevel.Error);
	}

	logInfo(message: string): void {
		this.log(message, LogLevel.Info);
	}

	logTrace(message: string): void {
		this.log(message, LogLevel.Trace);
	}

	logWarning(message: string): void {
		this.log(message, LogLevel.Warning);
	}

	showOutputChannel(): void {
		this._outputChannel.show();
	}

	dispose(): void {
		this._disposables = dispose(this._disposables);
	}
}