From ac9717dc3d628f082a0f8193f96e45c44465f8d2 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Mon, 11 Jul 2016 19:44:23 -0700 Subject: [PATCH] use Logger directly --- src/server/editorServices.ts | 38 ++++++++++++++++-------------------- src/server/project.ts | 4 ++-- src/server/server.ts | 8 ++++---- src/server/session.ts | 14 ++++++------- src/server/utilities.ts | 12 +++++++++++- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 0568e72a93..44c28dbb5a 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -86,7 +86,7 @@ namespace ts.server { // if the ref count for this directory watcher drops to 0, it's time to close it this.directoryWatchersRefCount[directory]--; if (this.directoryWatchersRefCount[directory] === 0) { - this.projectService.log(`Close directory watcher for: ${directory}`); + this.projectService.logger.info(`Close directory watcher for: ${directory}`); this.directoryWatchersForTsconfig[directory].close(); delete this.directoryWatchersForTsconfig[directory]; } @@ -97,7 +97,7 @@ namespace ts.server { let parentPath = getDirectoryPath(currentPath); while (currentPath != parentPath) { if (!this.directoryWatchersForTsconfig[currentPath]) { - this.projectService.log(`Add watcher for: ${currentPath}`); + this.projectService.logger.info(`Add watcher for: ${currentPath}`); this.directoryWatchersForTsconfig[currentPath] = this.projectService.host.watchDirectory(currentPath, callback); this.directoryWatchersRefCount[currentPath] = 1; } @@ -279,7 +279,7 @@ namespace ts.server { return; } - this.log(`Detected source file changes: ${fileName}`); + this.logger.info(`Detected source file changes: ${fileName}`); this.throttledOperations.schedule( project.configFileName, /*delay*/250, @@ -306,7 +306,7 @@ namespace ts.server { } private onConfigChangedForConfiguredProject(project: ConfiguredProject) { - this.log(`Config file changed: ${project.configFileName}`); + this.logger.info(`Config file changed: ${project.configFileName}`); this.updateConfiguredProject(project); this.refreshInferredProjects(); } @@ -317,11 +317,11 @@ namespace ts.server { private onConfigFileAddedForInferredProject(fileName: string) { // TODO: check directory separators if (getBaseFileName(fileName) != "tsconfig.json") { - this.log(`${fileName} is not tsconfig.json`); + this.logger.info(`${fileName} is not tsconfig.json`); return; } - this.log(`Detected newly added tsconfig file: ${fileName}`); + this.logger.info(`Detected newly added tsconfig file: ${fileName}`); this.reloadProjects(); } @@ -331,7 +331,7 @@ namespace ts.server { } private removeProject(project: Project) { - this.log(`remove project: ${project.getRootFiles().toString()}`); + this.logger.info(`remove project: ${project.getRootFiles().toString()}`); project.close(); @@ -460,16 +460,16 @@ namespace ts.server { */ private openOrUpdateConfiguredProjectForFile(fileName: NormalizedPath): OpenConfiguredProjectResult { const searchPath = getDirectoryPath(fileName); - this.log(`Search path: ${searchPath}`, "Info"); + this.logger.info(`Search path: ${searchPath}`); // check if this file is already included in one of external projects const configFileName = this.findConfigFile(asNormalizedPath(searchPath)); if (!configFileName) { - this.log("No config files found."); + this.logger.info("No config files found."); return {}; } - this.log(`Config file name: ${configFileName}`, "Info"); + this.logger.info(`Config file name: ${configFileName}`); const project = this.findConfiguredProjectByProjectName(configFileName); if (!project) { @@ -480,7 +480,7 @@ namespace ts.server { // even if opening config file was successful, it could still // contain errors that were tolerated. - this.log(`Opened configuration file ${configFileName}`, "Info"); + this.logger.info(`Opened configuration file ${configFileName}`); if (errors && errors.length > 0) { return { configFileName, configFileErrors: errors }; } @@ -737,7 +737,7 @@ namespace ts.server { private updateConfiguredProject(project: ConfiguredProject) { if (!this.host.fileExists(project.configFileName)) { - this.log("Config file deleted"); + this.logger.info("Config file deleted"); this.removeProject(project); return; } @@ -835,26 +835,22 @@ namespace ts.server { return this.filenameToScriptInfo.get(fileName); } - log(msg: string, type = "Err") { - this.logger.msg(msg, type); - } - setHostConfiguration(args: protocol.ConfigureRequestArguments) { if (args.file) { const info = this.getScriptInfoForNormalizedPath(toNormalizedPath(args.file)); if (info) { info.setFormatOptions(args.formatOptions); - this.log(`Host configuration update for file ${args.file}`, "Info"); + this.logger.info(`Host configuration update for file ${args.file}`); } } else { if (args.hostInfo !== undefined) { this.hostConfiguration.hostInfo = args.hostInfo; - this.log(`Host information ${args.hostInfo}`, "Info"); + this.logger.info(`Host information ${args.hostInfo}`); } if (args.formatOptions) { mergeMaps(this.hostConfiguration.formatCodeOptions, args.formatOptions); - this.log("Format host information updated", "Info"); + this.logger.info("Format host information updated"); } } } @@ -867,7 +863,7 @@ namespace ts.server { * This function rebuilds the project for every file opened by the client */ reloadProjects() { - this.log("reload projects."); + this.logger.info("reload projects."); // try to reload config file for all open files for (const info of this.openFiles) { this.openOrUpdateConfiguredProjectForFile(info.fileName); @@ -881,7 +877,7 @@ namespace ts.server { * up to date. */ refreshInferredProjects() { - this.log("updating project structure from ...", "Info"); + this.logger.info("updating project structure from ..."); this.printProjects(); const orphantedFiles: ScriptInfo[] = []; diff --git a/src/server/project.ts b/src/server/project.ts index f53c3653e6..92f1d097b3 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -406,7 +406,7 @@ namespace ts.server { } const directoryToWatch = getDirectoryPath(this.configFileName); - this.projectService.log(`Add recursive watcher for: ${directoryToWatch}`); + this.projectService.logger.info(`Add recursive watcher for: ${directoryToWatch}`); this.directoryWatcher = this.projectService.host.watchDirectory(directoryToWatch, path => callback(this, path), /*recursive*/ true); } @@ -418,7 +418,7 @@ namespace ts.server { this.directoriesWatchedForWildcards = reduceProperties(this.wildcardDirectories, (watchers, flag, directory) => { if (comparePaths(configDirectoryPath, directory, ".", !this.projectService.host.useCaseSensitiveFileNames) !== Comparison.EqualTo) { const recursive = (flag & WatchDirectoryFlags.Recursive) !== 0; - this.projectService.log(`Add ${recursive ? "recursive " : ""}watcher for: ${directory}`); + this.projectService.logger.info(`Add ${recursive ? "recursive " : ""}watcher for: ${directory}`); watchers[directory] = this.projectService.host.watchDirectory( directory, path => callback(this, path), diff --git a/src/server/server.ts b/src/server/server.ts index b072b2516a..1a101f9d03 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -44,11 +44,11 @@ namespace ts.server { } perftrc(s: string) { - this.msg(s, "Perf"); + this.msg(s, Msg.Perf); } info(s: string) { - this.msg(s, "Info"); + this.msg(s, Msg.Info); } startGroup() { @@ -71,7 +71,7 @@ namespace ts.server { } - msg(s: string, type = "Err") { + msg(s: string, type: Msg.Types = Msg.Err) { if (this.fd < 0) { if (this.logFilename) { this.fd = fs.openSync(this.logFilename, "w"); @@ -105,7 +105,7 @@ namespace ts.server { } exit() { - this.projectService.log("Exiting...", "Info"); + this.logger.info("Exiting..."); this.projectService.closeLog(); process.exit(0); } diff --git a/src/server/session.ts b/src/server/session.ts index a9648bd8d9..88676aae6a 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -164,7 +164,7 @@ namespace ts.server { private maxUncompressedMessageSize: number, private compress: (s: string) => CompressedData, private hrtime: (start?: number[]) => number[], - private logger: Logger) { + protected logger: Logger) { this.projectService = new ProjectService(host, logger, cancellationToken, useSingleInferredProject, (eventName, project, fileName) => { this.handleEvent(eventName, project, fileName); @@ -173,7 +173,7 @@ namespace ts.server { private handleEvent(eventName: string, project: Project, fileName: NormalizedPath) { if (eventName == "context") { - this.projectService.log("got context event, updating diagnostics for" + fileName, "Info"); + this.logger.info("got context event, updating diagnostics for" + fileName); this.updateErrorCheck([{ fileName, project }], this.changeSeq, (n) => n === this.changeSeq, 100); } @@ -187,7 +187,7 @@ namespace ts.server { msg += "\n" + (err).stack; } } - this.projectService.log(msg); + this.logger.msg(msg, Msg.Err); } public send(msg: protocol.Message, canCompressResponse: boolean) { @@ -334,7 +334,7 @@ namespace ts.server { if (!projects) { return; } - this.projectService.log(`cleaning ${caption}`); + this.logger.info(`cleaning ${caption}`); for (const p of projects) { p.languageService.cleanupSemanticCache(); } @@ -345,7 +345,7 @@ namespace ts.server { this.cleanProjects("configured projects", this.projectService.configuredProjects); this.cleanProjects("external projects", this.projectService.externalProjects); if (typeof global !== "undefined" && global.gc) { - this.projectService.log(`global.gc()`); + this.logger.info(`global.gc()`); global.gc(); global.gc(); global.gc(); @@ -1451,8 +1451,8 @@ namespace ts.server { return handler(request); } else { - this.projectService.log("Unrecognized JSON command: " + JSON.stringify(request)); - this.output(undefined, CommandNames.Unknown, /*canCompressResponse*/ false, request.seq, "Unrecognized JSON command: " + request.command); + this.logger.msg(`Unrecognized JSON command: ${JSON.stringify(request)}`, Msg.Err); + this.output(undefined, CommandNames.Unknown, /*canCompressResponse*/ false, request.seq, `Unrecognized JSON command: ${request.command}`); return { responseRequired: false }; } } diff --git a/src/server/utilities.ts b/src/server/utilities.ts index 79a8edc0f8..cf38324343 100644 --- a/src/server/utilities.ts +++ b/src/server/utilities.ts @@ -9,7 +9,17 @@ namespace ts.server { info(s: string): void; startGroup(): void; endGroup(): void; - msg(s: string, type?: string): void; + msg(s: string, type?: Msg.Types): void; + } + + export namespace Msg { + export type Err = "Err"; + export const Err: Err = "Err"; + export type Info = "Info"; + export const Info: Info = "Info"; + export type Perf = "Perf"; + export const Perf: Perf = "Perf"; + export type Types = Err | Info | Perf; } export function getDefaultFormatCodeSettings(host: ServerHost): FormatCodeSettings {