microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f16656e963fb76d68e7e5310a3661326540b54f2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/log/loggers.ts

138lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4/**
5 * Formatter for the Output channel.
6 */
7import {LogHelper, LogLevel} from "./logHelper";
8
9export interface ILogger {
10 logMessage: (message: string, formatMessage?: boolean) => void;
11 logError: (errorMessage: string, error?: any, logStack?: boolean) => void;
12 logStreamData: (data: Buffer, stream: NodeJS.WritableStream) => void;
13 logInternalMessage: (logLevel: LogLevel, message: string) => void;
14 setFocusOnLogChannel: () => void;
15}
16
17export class ConsoleLogger implements ILogger {
18 public logMessage(message: string, formatMessage: boolean = true) {
19 console.log(formatMessage ?
20 this.getFormattedMessage(message) :
21 message);
22 }
23
24 public logError(errorMessage: string, error?: any, logStack: boolean = true) {
25 console.error(this.getFormattedMessage(errorMessage));
26
27 // Print the error stack if necessary
28 if (logStack && error && (<Error>error).stack) {
29 console.error(`Stack: ${(<Error>error).stack}`);
30 }
31 }
32
33 public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
34 stream.write(data.toString());
35 }
36
37 public logInternalMessage(logLevel: LogLevel, message: string) {
38 this.logMessage(this.getFormattedInternalMessage(logLevel, message), /* formatMessage */ false);
39 }
40
41 public setFocusOnLogChannel() {
42 // Do nothing - console takes focus automatically upon logging
43 return;
44 }
45
46 private getFormattedMessage(message: string) {
47 return `${LogHelper.MESSAGE_TAG} ${message}\n`;
48 }
49
50 private getFormattedInternalMessage(logLevel: LogLevel, message: string) {
51 return (`${LogHelper.INTERNAL_TAG} [${LogLevel[logLevel]}] ${message}\n`);
52 }
53}
54
55export class StreamLogger implements ILogger {
56 private stream: NodeJS.WritableStream;
57 constructor(stream: NodeJS.WritableStream) {
58 this.stream = stream;
59 }
60 public logMessage(message: string, formatMessage: boolean = true) {
61 this.stream.write(formatMessage ?
62 this.getFormattedMessage(message) :
63 message);
64 }
65
66 public logError(errorMessage: string, error?: any, logStack: boolean = true) {
67 this.logMessage(errorMessage);
68
69 if (logStack && error && (<Error>error).stack) {
70 this.logMessage(`Stack: ${(<Error>error).stack}`, /* formatMessage */ false);
71 }
72 }
73
74 public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
75 stream.write(data.toString());
76 }
77
78 public logInternalMessage(logLevel: LogLevel, message: string) {
79 this.logMessage(this.getFormattedInternalMessage(logLevel, message), /* formatMessage */ false);
80 }
81
82 public getFormattedMessage(message: string) {
83 return `${LogHelper.MESSAGE_TAG} ${message}\n`;
84 }
85
86 public getFormattedInternalMessage(logLevel: LogLevel, message: string) {
87 return (`${LogHelper.INTERNAL_TAG} [${logLevel}] ${message}\n`);
88 }
89
90 public setFocusOnLogChannel() {
91 // Do nothing
92 return;
93 }
94}
95
96export class NodeDebugAdapterLogger implements ILogger {
97 private debugSession: NodeDebugSession;
98 private debugAdapterPackage: typeof VSCodeDebugAdapter;
99
100 public constructor(adapterPackage: typeof VSCodeDebugAdapter, debugSession: NodeDebugSession) {
101 this.debugAdapterPackage = adapterPackage;
102 this.debugSession = debugSession;
103 }
104
105 public logMessage(message: string, formatMessage: boolean = true, destination: string = "stdout") {
106 const outputEventMessage = formatMessage ? this.getFormattedMessage(message) : message;
107 this.debugSession.sendEvent(new this.debugAdapterPackage.OutputEvent(outputEventMessage, destination));
108 }
109
110 public logError(errorMessage: string, error?: any, logStack: boolean = true) {
111 this.logMessage(`${LogHelper.ERROR_TAG} ${errorMessage}\n`, false, "stderr");
112
113 if (logStack && error && (<Error>error).stack) {
114 this.logMessage(`Stack: ${(<Error>error).stack}`, false);
115 }
116 }
117
118 public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
119 this.logMessage(data.toString(), false);
120 }
121
122 public logInternalMessage(logLevel: LogLevel, message: string) {
123 this.logMessage(this.getFormattedInternalMessage(logLevel, message), false);
124 }
125
126 public getFormattedMessage(message: string) {
127 return `${LogHelper.MESSAGE_TAG} ${message}\n`;
128 }
129
130 public getFormattedInternalMessage(logLevel: LogLevel, message: string) {
131 return (`${LogHelper.INTERNAL_TAG} [${logLevel}] ${message}\n`);
132 }
133
134 public setFocusOnLogChannel() {
135 // Do nothing
136 return;
137 }
138}