|
| 1 | +/* |
| 2 | + * SonarLint CLI |
| 3 | + * Copyright (C) 2016-2017 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonarlint.cli.util; |
| 21 | + |
| 22 | +import java.io.PrintStream; |
| 23 | + |
| 24 | +public class Logger { |
| 25 | + private static volatile Logger instance; |
| 26 | + private boolean debugEnabled = false; |
| 27 | + private boolean displayStackTrace = false; |
| 28 | + private PrintStream stdOut; |
| 29 | + private PrintStream stdErr; |
| 30 | + |
| 31 | + private Logger() { |
| 32 | + this.stdErr = System.err; |
| 33 | + this.stdOut = System.out; |
| 34 | + } |
| 35 | + |
| 36 | + public Logger(PrintStream stdOut, PrintStream stdErr) { |
| 37 | + this.stdErr = stdErr; |
| 38 | + this.stdOut = stdOut; |
| 39 | + } |
| 40 | + |
| 41 | + public static Logger get() { |
| 42 | + if (instance == null) { |
| 43 | + instance = new Logger(); |
| 44 | + } |
| 45 | + return instance; |
| 46 | + } |
| 47 | + |
| 48 | + public static void set(PrintStream stdOut, PrintStream stdErr) { |
| 49 | + get().stdOut = stdOut; |
| 50 | + get().stdErr = stdErr; |
| 51 | + } |
| 52 | + |
| 53 | + public void setDebugEnabled(boolean debugEnabled) { |
| 54 | + this.debugEnabled = debugEnabled; |
| 55 | + } |
| 56 | + |
| 57 | + public void setDisplayStackTrace(boolean displayStackTrace) { |
| 58 | + this.displayStackTrace = displayStackTrace; |
| 59 | + } |
| 60 | + |
| 61 | + public boolean isDebugEnabled() { |
| 62 | + return debugEnabled; |
| 63 | + } |
| 64 | + |
| 65 | + public void debug(String message) { |
| 66 | + if (isDebugEnabled()) { |
| 67 | + stdErr.println("DEBUG: " + message); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public void debug(String message, Throwable t) { |
| 72 | + if (isDebugEnabled()) { |
| 73 | + stdErr.println("DEBUG: " + message); |
| 74 | + if (displayStackTrace) { |
| 75 | + t.printStackTrace(stdErr); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public void info(String message) { |
| 81 | + stdErr.println("INFO: " + message); |
| 82 | + } |
| 83 | + |
| 84 | + public void warn(String message) { |
| 85 | + stdErr.println("WARN: " + message); |
| 86 | + } |
| 87 | + |
| 88 | + public void error(String message) { |
| 89 | + stdErr.println("ERROR: " + message); |
| 90 | + } |
| 91 | + |
| 92 | + public void error(String message, Throwable t) { |
| 93 | + stdErr.println("ERROR: " + message); |
| 94 | + if (displayStackTrace) { |
| 95 | + t.printStackTrace(stdErr); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments