microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/log/LogHelper.ts
71lines · 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 { |
f872f4d5RedMickey6 years ago | 10 | None = 0, |
| 11 | Trace = 1, | |
| 12 | Debug = 2, | |
| 13 | Info = 3, | |
| 14 | Warning = 4, | |
| 15 | Error = 5, | |
| 16 | Verbose = 6, | |
a324603aRedMickey6 years ago | 17 | Custom = 7, |
0a68f8dbArtem Egorov8 years ago | 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; | |
4f8669f9JiglioNero6 years ago | 26 | logStream: (data: Buffer | string, stream?: NodeJS.WritableStream) => void; |
0a68f8dbArtem Egorov8 years ago | 27 | } |
| 28 | | |
| 29 | export class LogHelper { | |
| 30 | public static get LOG_LEVEL(): LogLevel { | |
| 31 | return getLogLevel(); | |
| 32 | } | |
| 33 | } | |
| 34 | | |
08722261Yuri Skorokhodov7 years ago | 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 | | |
0a68f8dbArtem Egorov8 years ago | 63 | function getLogLevel() { |
| 64 | try { | |
| 65 | const SettingsHelper = require("../settingsHelper").SettingsHelper; | |
| 66 | return SettingsHelper.getLogLevel(); | |
34472878RedMickey5 years ago | 67 | } catch (err) { |
| 68 | // Debugger context | |
0a68f8dbArtem Egorov8 years ago | 69 | return LogLevel.Info; // Default |
| 70 | } | |
| 71 | } |