microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
improve-configuration

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/log/LogHelper.ts

73lines · modeblame

0a68f8dbArtem Egorov8 years ago1// 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*/
08722261Yuri Skorokhodov7 years ago7import * as path from "path";
8import * as mkdirp from "mkdirp";
09f6024fHeniker4 years ago9
0a68f8dbArtem Egorov8 years ago10export enum LogLevel {
f872f4d5RedMickey6 years ago11None = 0,
12Trace = 1,
13Debug = 2,
14Info = 3,
15Warning = 4,
16Error = 5,
17Verbose = 6,
a324603aRedMickey6 years ago18Custom = 7,
0a68f8dbArtem Egorov8 years ago19}
20
21export interface ILogger {
22log: (message: string, level: LogLevel) => void;
23info: (message: string) => void;
24warning: (message: string) => void;
25error: (errorMessage: string, error?: Error, stack?: boolean) => void;
26debug: (message: string) => void;
4f8669f9JiglioNero6 years ago27logStream: (data: Buffer | string, stream?: NodeJS.WritableStream) => void;
0a68f8dbArtem Egorov8 years ago28}
29
30export class LogHelper {
31public static get LOG_LEVEL(): LogLevel {
32return getLogLevel();
33}
34}
35
08722261Yuri Skorokhodov7 years ago36export interface DevLogToFileSettings {
37LogsDirectory: string | undefined;
38}
39
40export function getLoggingOptions(): DevLogToFileSettings {
41return {
42LogsDirectory: 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 {
52const loggingOptions = getLoggingOptions();
53if (loggingOptions.LogsDirectory) {
09f6024fHeniker4 years ago54const dirPath = loggingOptions.LogsDirectory;
08722261Yuri Skorokhodov7 years ago55if (!path.isAbsolute(dirPath)) {
56return null;
57}
58mkdirp.sync(dirPath);
59return dirPath;
60}
61return null;
62}
63
0a68f8dbArtem Egorov8 years ago64function getLogLevel() {
65try {
09f6024fHeniker4 years ago66// eslint-disable-next-line @typescript-eslint/no-var-requires
0a68f8dbArtem Egorov8 years ago67const SettingsHelper = require("../settingsHelper").SettingsHelper;
68return SettingsHelper.getLogLevel();
34472878RedMickey5 years ago69} catch (err) {
70// Debugger context
0a68f8dbArtem Egorov8 years ago71return LogLevel.Info; // Default
72}
73}