microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.11.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/log/LogHelper.ts

68lines · 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 * Logging utility class.
6 */
7import * as path from "path";
8import * as mkdirp from "mkdirp";
9export enum LogLevel {
10 Trace = 0,
11 Debug = 1,
12 Info = 2,
13 Warning = 3,
14 Error = 4,
15 None = 5,
16}
17
18export interface ILogger {
19 log: (message: string, level: LogLevel) => void;
20 info: (message: string) => void;
21 warning: (message: string) => void;
22 error: (errorMessage: string, error?: Error, stack?: boolean) => void;
23 debug: (message: string) => void;
24 logStream: (data: Buffer | String, stream?: NodeJS.WritableStream) => void;
25}
26
27export class LogHelper {
28 public static get LOG_LEVEL(): LogLevel {
29 return getLogLevel();
30 }
31}
32
33export interface DevLogToFileSettings {
34 LogsDirectory: string | undefined;
35}
36
37export function getLoggingOptions(): DevLogToFileSettings {
38 return {
39 LogsDirectory: process.env.REACT_NATIVE_TOOLS_LOGS_DIR,
40 };
41}
42/**
43 * Returns directory in which the extension's log files will be saved
44 * if `env` variables `REACT_NATIVE_TOOLS_LOGS_DIR` is defined.
45 * Also, checks that path is a correct absolute path. Creates new folder if not exists yet.
46 * @returns Path to the logs folder or null
47 */
48export function getLoggingDirectory(): string | null {
49 const loggingOptions = getLoggingOptions();
50 if (loggingOptions.LogsDirectory) {
51 let dirPath = loggingOptions.LogsDirectory;
52 if (!path.isAbsolute(dirPath)) {
53 return null;
54 }
55 mkdirp.sync(dirPath);
56 return dirPath;
57 }
58 return null;
59}
60
61function getLogLevel() {
62 try {
63 const SettingsHelper = require("../settingsHelper").SettingsHelper;
64 return SettingsHelper.getLogLevel();
65 } catch (err) { // Debugger context
66 return LogLevel.Info; // Default
67 }
68}
69