mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-28 09:35:38 -05:00
Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973 (#6381)
* Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973 * disable strict null check
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IChannel, IServerChannel } from 'vs/base/parts/ipc/common/ipc';
|
||||
import { LogLevel, ILogService, DelegatedLogService } from 'vs/platform/log/common/log';
|
||||
import { Event } from 'vs/base/common/event';
|
||||
|
||||
export class LogLevelSetterChannel implements IServerChannel {
|
||||
|
||||
onDidChangeLogLevel: Event<LogLevel>;
|
||||
|
||||
constructor(private service: ILogService) {
|
||||
this.onDidChangeLogLevel = Event.buffer(service.onDidChangeLogLevel, true);
|
||||
}
|
||||
|
||||
listen(_: unknown, event: string): Event<any> {
|
||||
switch (event) {
|
||||
case 'onDidChangeLogLevel': return this.onDidChangeLogLevel;
|
||||
}
|
||||
|
||||
throw new Error(`Event not found: ${event}`);
|
||||
}
|
||||
|
||||
call(_: unknown, command: string, arg?: any): Promise<any> {
|
||||
switch (command) {
|
||||
case 'setLevel': this.service.setLevel(arg); return Promise.resolve();
|
||||
}
|
||||
|
||||
throw new Error(`Call not found: ${command}`);
|
||||
}
|
||||
}
|
||||
|
||||
export class LogLevelSetterChannelClient {
|
||||
|
||||
constructor(private channel: IChannel) { }
|
||||
|
||||
get onDidChangeLogLevel(): Event<LogLevel> {
|
||||
return this.channel.listen('onDidChangeLogLevel');
|
||||
}
|
||||
|
||||
setLevel(level: LogLevel): void {
|
||||
this.channel.call('setLevel', level);
|
||||
}
|
||||
}
|
||||
|
||||
export class FollowerLogService extends DelegatedLogService implements ILogService {
|
||||
_serviceBrand: any;
|
||||
|
||||
constructor(private master: LogLevelSetterChannelClient, logService: ILogService) {
|
||||
super(logService);
|
||||
this._register(master.onDidChangeLogLevel(level => logService.setLevel(level)));
|
||||
}
|
||||
|
||||
setLevel(level: LogLevel): void {
|
||||
this.master.setLevel(level);
|
||||
}
|
||||
}
|
||||
@@ -4,24 +4,20 @@
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as path from 'vs/base/common/path';
|
||||
import { ILogService, LogLevel, NullLogService, AbstractLogService } from 'vs/platform/log/common/log';
|
||||
import { ILogService, LogLevel, AbstractLogService } from 'vs/platform/log/common/log';
|
||||
import * as spdlog from 'spdlog';
|
||||
import { BufferLogService } from 'vs/platform/log/common/bufferLog';
|
||||
|
||||
export async function createSpdLogService(processName: string, logLevel: LogLevel, logsFolder: string): Promise<ILogService> {
|
||||
async function createSpdLogLogger(processName: string, logsFolder: string): Promise<spdlog.RotatingLogger | null> {
|
||||
// Do not crash if spdlog cannot be loaded
|
||||
try {
|
||||
const _spdlog: typeof spdlog = require.__$__nodeRequire('spdlog');
|
||||
const _spdlog = await import('spdlog');
|
||||
_spdlog.setAsyncMode(8192, 500);
|
||||
const logfilePath = path.join(logsFolder, `${processName}.log`);
|
||||
const logger = await _spdlog.createRotatingLoggerAsync(processName, logfilePath, 1024 * 1024 * 5, 6);
|
||||
logger.setLevel(0);
|
||||
|
||||
return new SpdLogService(logger, logLevel);
|
||||
return _spdlog.createRotatingLoggerAsync(processName, logfilePath, 1024 * 1024 * 5, 6);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return new NullLogService();
|
||||
return null;
|
||||
}
|
||||
|
||||
export function createRotatingLogger(name: string, filename: string, filesize: number, filecount: number): spdlog.RotatingLogger {
|
||||
@@ -29,45 +25,88 @@ export function createRotatingLogger(name: string, filename: string, filesize: n
|
||||
return _spdlog.createRotatingLogger(name, filename, filesize, filecount);
|
||||
}
|
||||
|
||||
export function createBufferSpdLogService(processName: string, logLevel: LogLevel, logsFolder: string): ILogService {
|
||||
const bufferLogService = new BufferLogService();
|
||||
createSpdLogService(processName, logLevel, logsFolder).then(logger => bufferLogService.logger = logger);
|
||||
return bufferLogService;
|
||||
interface ILog {
|
||||
level: LogLevel;
|
||||
message: string;
|
||||
}
|
||||
|
||||
class SpdLogService extends AbstractLogService implements ILogService {
|
||||
function log(logger: spdlog.RotatingLogger, level: LogLevel, message: string): void {
|
||||
switch (level) {
|
||||
case LogLevel.Trace: return logger.trace(message);
|
||||
case LogLevel.Debug: return logger.debug(message);
|
||||
case LogLevel.Info: return logger.info(message);
|
||||
case LogLevel.Warning: return logger.warn(message);
|
||||
case LogLevel.Error: return logger.error(message);
|
||||
case LogLevel.Critical: return logger.critical(message);
|
||||
default: throw new Error('Invalid log level');
|
||||
}
|
||||
}
|
||||
|
||||
export class SpdLogService extends AbstractLogService implements ILogService {
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
constructor(
|
||||
private readonly logger: spdlog.RotatingLogger,
|
||||
level: LogLevel = LogLevel.Error
|
||||
) {
|
||||
private buffer: ILog[] = [];
|
||||
private _loggerCreationPromise: Promise<void> | undefined = undefined;
|
||||
private _logger: spdlog.RotatingLogger | undefined;
|
||||
|
||||
constructor(private readonly name: string, private readonly logsFolder: string, level: LogLevel) {
|
||||
super();
|
||||
this.setLevel(level);
|
||||
this._createSpdLogLogger();
|
||||
this._register(this.onDidChangeLogLevel(level => {
|
||||
if (this._logger) {
|
||||
this._logger.setLevel(level);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private _createSpdLogLogger(): Promise<void> {
|
||||
if (!this._loggerCreationPromise) {
|
||||
this._loggerCreationPromise = createSpdLogLogger(this.name, this.logsFolder)
|
||||
.then(logger => {
|
||||
if (logger) {
|
||||
this._logger = logger;
|
||||
this._logger.setLevel(this.getLevel());
|
||||
for (const { level, message } of this.buffer) {
|
||||
log(this._logger, level, message);
|
||||
}
|
||||
this.buffer = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
return this._loggerCreationPromise;
|
||||
}
|
||||
|
||||
private _log(level: LogLevel, message: string): void {
|
||||
if (this._logger) {
|
||||
log(this._logger, level, message);
|
||||
} else if (this.getLevel() <= level) {
|
||||
this.buffer.push({ level, message });
|
||||
}
|
||||
}
|
||||
|
||||
trace(): void {
|
||||
if (this.getLevel() <= LogLevel.Trace) {
|
||||
this.logger.trace(this.format(arguments));
|
||||
this._log(LogLevel.Trace, this.format(arguments));
|
||||
}
|
||||
}
|
||||
|
||||
debug(): void {
|
||||
if (this.getLevel() <= LogLevel.Debug) {
|
||||
this.logger.debug(this.format(arguments));
|
||||
this._log(LogLevel.Debug, this.format(arguments));
|
||||
}
|
||||
}
|
||||
|
||||
info(): void {
|
||||
if (this.getLevel() <= LogLevel.Info) {
|
||||
this.logger.info(this.format(arguments));
|
||||
this._log(LogLevel.Info, this.format(arguments));
|
||||
}
|
||||
}
|
||||
|
||||
warn(): void {
|
||||
if (this.getLevel() <= LogLevel.Warning) {
|
||||
this.logger.warn(this.format(arguments));
|
||||
this._log(LogLevel.Warning, this.format(arguments));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,21 +117,33 @@ class SpdLogService extends AbstractLogService implements ILogService {
|
||||
if (arg instanceof Error) {
|
||||
const array = Array.prototype.slice.call(arguments) as any[];
|
||||
array[0] = arg.stack;
|
||||
this.logger.error(this.format(array));
|
||||
this._log(LogLevel.Error, this.format(array));
|
||||
} else {
|
||||
this.logger.error(this.format(arguments));
|
||||
this._log(LogLevel.Error, this.format(arguments));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
critical(): void {
|
||||
if (this.getLevel() <= LogLevel.Critical) {
|
||||
this.logger.critical(this.format(arguments));
|
||||
this._log(LogLevel.Critical, this.format(arguments));
|
||||
}
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.logger.drop();
|
||||
if (this._logger) {
|
||||
this.disposeLogger();
|
||||
} else if (this._loggerCreationPromise) {
|
||||
this._loggerCreationPromise.then(() => this.disposeLogger());
|
||||
}
|
||||
this._loggerCreationPromise = undefined;
|
||||
}
|
||||
|
||||
private disposeLogger(): void {
|
||||
if (this._logger) {
|
||||
this._logger.drop();
|
||||
this._logger = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
private format(args: any): string {
|
||||
|
||||
Reference in New Issue
Block a user