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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * SonarScanner CLI
  3. * Copyright (C) 2011-2024 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. import org.sonarsource.scanner.lib.LogOutput;
  25. public class Logs {
  26. private DateTimeFormatter timeFormatter;
  27. private boolean debugEnabled = false;
  28. private PrintStream stdOut;
  29. private PrintStream stdErr;
  30. public Logs(PrintStream stdOut, PrintStream stdErr) {
  31. this.stdErr = stdErr;
  32. this.stdOut = stdOut;
  33. this.timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
  34. }
  35. public void setDebugEnabled(boolean debugEnabled) {
  36. this.debugEnabled = debugEnabled;
  37. }
  38. public boolean isDebugEnabled() {
  39. return debugEnabled;
  40. }
  41. public void debug(String message) {
  42. if (isDebugEnabled()) {
  43. LocalTime currentTime = LocalTime.now();
  44. String timestamp = currentTime.format(timeFormatter);
  45. stdOut.println(timestamp + " DEBUG: " + message);
  46. }
  47. }
  48. public void info(String message) {
  49. print(stdOut, "INFO: " + message);
  50. }
  51. public void warn(String message) {
  52. print(stdOut, "WARN: " + message);
  53. }
  54. public void error(String message) {
  55. print(stdErr, "ERROR: " + message);
  56. }
  57. public void error(String message, Throwable t) {
  58. print(stdErr, "ERROR: " + message);
  59. t.printStackTrace(stdErr);
  60. }
  61. private void print(PrintStream stream, String msg) {
  62. if (debugEnabled) {
  63. LocalTime currentTime = LocalTime.now();
  64. String timestamp = currentTime.format(timeFormatter);
  65. stream.println(timestamp + " " + msg);
  66. } else {
  67. stream.println(msg);
  68. }
  69. }
  70. /**
  71. * Adapter for the scanner library.
  72. */
  73. public LogOutput getLogOutputAdapter() {
  74. return new LogOutputAdapter(this);
  75. }
  76. static class LogOutputAdapter implements LogOutput {
  77. private final Logs logs;
  78. public LogOutputAdapter(Logs logs) {
  79. this.logs = logs;
  80. }
  81. @Override
  82. public void log(String formattedMessage, Level level) {
  83. switch (level) {
  84. case TRACE, DEBUG:
  85. logs.debug(formattedMessage);
  86. break;
  87. case ERROR:
  88. logs.error(formattedMessage);
  89. break;
  90. case WARN:
  91. logs.warn(formattedMessage);
  92. break;
  93. case INFO:
  94. default:
  95. logs.info(formattedMessage);
  96. }
  97. }
  98. }
  99. }