You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Logs.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2020 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.sonarsource.scanner.cli;
  21. import java.io.PrintStream;
  22. import java.time.LocalTime;
  23. import java.time.format.DateTimeFormatter;
  24. public class Logs {
  25. private DateTimeFormatter timeFormatter;
  26. private boolean debugEnabled = false;
  27. private PrintStream stdOut;
  28. private PrintStream stdErr;
  29. public Logs(PrintStream stdOut, PrintStream stdErr) {
  30. this.stdErr = stdErr;
  31. this.stdOut = stdOut;
  32. this.timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
  33. }
  34. public void setDebugEnabled(boolean debugEnabled) {
  35. this.debugEnabled = debugEnabled;
  36. }
  37. public boolean isDebugEnabled() {
  38. return debugEnabled;
  39. }
  40. public void debug(String message) {
  41. if (isDebugEnabled()) {
  42. LocalTime currentTime = LocalTime.now();
  43. String timestamp = currentTime.format(timeFormatter);
  44. stdOut.println(timestamp + " DEBUG: " + message);
  45. }
  46. }
  47. public void info(String message) {
  48. print(stdOut, "INFO: " + message);
  49. }
  50. public void warn(String message) {
  51. print(stdOut, "WARN: " + message);
  52. }
  53. public void error(String message) {
  54. print(stdErr, "ERROR: " + message);
  55. }
  56. public void error(String message, Throwable t) {
  57. print(stdErr, "ERROR: " + message);
  58. t.printStackTrace(stdErr);
  59. }
  60. private void print(PrintStream stream, String msg) {
  61. if (debugEnabled) {
  62. LocalTime currentTime = LocalTime.now();
  63. String timestamp = currentTime.format(timeFormatter);
  64. stream.println(timestamp + " " + msg);
  65. } else {
  66. stream.println(msg);
  67. }
  68. }
  69. }