microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
smoke-actionbar-use-packager-helper

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/log/LogHelper.ts

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