microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/log/LogHelper.ts
68lines · modeblame
0a68f8dbArtem Egorov8 years ago | 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 | */ | |
08722261Yuri Skorokhodov7 years ago | 7 | import * as path from "path"; |
| 8 | import * as mkdirp from "mkdirp"; | |
0a68f8dbArtem Egorov8 years ago | 9 | export enum LogLevel { |
| 10 | Trace = 0, | |
| 11 | Debug = 1, | |
| 12 | Info = 2, | |
| 13 | Warning = 3, | |
| 14 | Error = 4, | |
| 15 | None = 5, | |
| 16 | } | |
| 17 | | |
| 18 | export 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 | | |
| 27 | export class LogHelper { | |
| 28 | public static get LOG_LEVEL(): LogLevel { | |
| 29 | return getLogLevel(); | |
| 30 | } | |
| 31 | } | |
| 32 | | |
08722261Yuri Skorokhodov7 years ago | 33 | export interface DevLogToFileSettings { |
| 34 | LogsDirectory: string | undefined; | |
| 35 | } | |
| 36 | | |
| 37 | export 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 | */ | |
| 48 | export 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 | | |
0a68f8dbArtem Egorov8 years ago | 61 | function 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 | } |