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