microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/log/loggers.ts

151lines · 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 logString: (data: string) => void;
14 logInternalMessage: (logLevel: LogLevel, message: string) => void;
15 setFocusOnLogChannel: () => void;
16}
17
18export class ConsoleLogger implements ILogger {
19 public logMessage(message: string, formatMessage: boolean = true) {
20 console.log(formatMessage ?
21 this.getFormattedMessage(message) :
22 message);
23 }
24
25 public logError(errorMessage: string, error?: any, logStack: boolean = true) {
26 console.error(this.getFormattedMessage(errorMessage));
27
28 // Print the error stack if necessary
29 if (logStack && error && (<Error>error).stack) {
30 console.error(`Stack: ${(<Error>error).stack}`);
31 }
32 }
33
34 public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
35 stream.write(data.toString());
36 }
37
38 public logString(data: string) {
39 this.logMessage(data, false);
40 }
41
42 public logInternalMessage(logLevel: LogLevel, message: string) {
43 this.logMessage(this.getFormattedInternalMessage(logLevel, message), /* formatMessage */ false);
44 }
45
46 public setFocusOnLogChannel() {
47 // Do nothing - console takes focus automatically upon logging
48 return;
49 }
50
51 private getFormattedMessage(message: string) {
52 return `${LogHelper.MESSAGE_TAG} ${message}\n`;
53 }
54
55 private getFormattedInternalMessage(logLevel: LogLevel, message: string) {
56 return (`${LogHelper.INTERNAL_TAG} [${LogLevel[logLevel]}] ${message}\n`);
57 }
58}
59
60export class StreamLogger implements ILogger {
61 private stream: NodeJS.WritableStream;
62 constructor(stream: NodeJS.WritableStream) {
63 this.stream = stream;
64 }
65 public logMessage(message: string, formatMessage: boolean = true) {
66 this.stream.write(formatMessage ?
67 this.getFormattedMessage(message) :
68 message);
69 }
70
71 public logError(errorMessage: string, error?: any, logStack: boolean = true) {
72 this.logMessage(errorMessage);
73
74 if (logStack && error && (<Error>error).stack) {
75 this.logMessage(`Stack: ${(<Error>error).stack}`, /* formatMessage */ false);
76 }
77 }
78
79 public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
80 stream.write(data.toString());
81 }
82
83 public logString(data: string) {
84 this.logMessage(data, false);
85 }
86
87 public logInternalMessage(logLevel: LogLevel, message: string) {
88 this.logMessage(this.getFormattedInternalMessage(logLevel, message), /* formatMessage */ false);
89 }
90
91 public getFormattedMessage(message: string) {
92 return `${LogHelper.MESSAGE_TAG} ${message}\n`;
93 }
94
95 public getFormattedInternalMessage(logLevel: LogLevel, message: string) {
96 return (`${LogHelper.INTERNAL_TAG} [${logLevel}] ${message}\n`);
97 }
98
99 public setFocusOnLogChannel() {
100 // Do nothing
101 return;
102 }
103}
104
105export class NodeDebugAdapterLogger implements ILogger {
106 private debugSession: ChromeDebuggerCorePackage.ChromeDebugSession;
107 private debugAdapterPackage: typeof VSCodeDebugAdapterPackage;
108
109 public constructor(adapterPackage: typeof VSCodeDebugAdapterPackage, debugSession: ChromeDebuggerCorePackage.ChromeDebugSession) {
110 this.debugAdapterPackage = adapterPackage;
111 this.debugSession = debugSession;
112 }
113
114 public logMessage(message: string, formatMessage: boolean = true, destination: string = "stdout") {
115 const outputEventMessage = formatMessage ? this.getFormattedMessage(message) : message;
116 this.debugSession.sendEvent(new this.debugAdapterPackage.OutputEvent(outputEventMessage, destination));
117 }
118
119 public logError(errorMessage: string, error?: any, logStack: boolean = true) {
120 this.logMessage(`${LogHelper.ERROR_TAG} ${errorMessage}\n`, false, "stderr");
121
122 if (logStack && error && (<Error>error).stack) {
123 this.logMessage(`Stack: ${(<Error>error).stack}`, false);
124 }
125 }
126
127 public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
128 this.logMessage(data.toString(), false);
129 }
130
131 public logString(data: string) {
132 this.logMessage(data, false);
133 }
134
135 public logInternalMessage(logLevel: LogLevel, message: string) {
136 this.logMessage(this.getFormattedInternalMessage(logLevel, message), false);
137 }
138
139 public getFormattedMessage(message: string) {
140 return `${LogHelper.MESSAGE_TAG} ${message}\n`;
141 }
142
143 public getFormattedInternalMessage(logLevel: LogLevel, message: string) {
144 return (`${LogHelper.INTERNAL_TAG} [${logLevel}] ${message}\n`);
145 }
146
147 public setFocusOnLogChannel() {
148 // Do nothing
149 return;
150 }
151}
152