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.

RecordingLogger.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.junit.http;
  11. import java.text.MessageFormat;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.List;
  15. import org.eclipse.jetty.util.log.Logger;
  16. /**
  17. * Log warnings into an array for later inspection.
  18. */
  19. public class RecordingLogger implements Logger {
  20. private static List<Warning> warnings = new ArrayList<>();
  21. /**
  22. * Clear the warnings, automatically done by
  23. * {@link org.eclipse.jgit.junit.http.AppServer#setUp()}
  24. */
  25. public static void clear() {
  26. synchronized (warnings) {
  27. warnings.clear();
  28. }
  29. }
  30. /**
  31. * Get the <code>warnings</code>.
  32. *
  33. * @return the warnings (if any) from the last execution
  34. */
  35. public static List<Warning> getWarnings() {
  36. synchronized (warnings) {
  37. ArrayList<Warning> copy = new ArrayList<>(warnings);
  38. return Collections.unmodifiableList(copy);
  39. }
  40. }
  41. @SuppressWarnings("serial")
  42. public static class Warning extends Exception {
  43. public Warning(String msg) {
  44. super(msg);
  45. }
  46. public Warning(String msg, Throwable cause) {
  47. super(msg, cause);
  48. }
  49. public Warning(Throwable thrown) {
  50. super(thrown);
  51. }
  52. }
  53. private final String name;
  54. /**
  55. * Constructor for <code>RecordingLogger</code>.
  56. */
  57. public RecordingLogger() {
  58. this("");
  59. }
  60. /**
  61. * Constructor for <code>RecordingLogger</code>.
  62. *
  63. * @param name
  64. */
  65. public RecordingLogger(String name) {
  66. this.name = name;
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public Logger getLogger(@SuppressWarnings("hiding") String name) {
  71. return new RecordingLogger(name);
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public String getName() {
  76. return name;
  77. }
  78. /**
  79. * Warning
  80. *
  81. * @param msg
  82. * @param arg0
  83. * @param arg1
  84. */
  85. public void warn(String msg, Object arg0, Object arg1) {
  86. synchronized (warnings) {
  87. warnings.add(new Warning(MessageFormat.format(msg, arg0, arg1)));
  88. }
  89. }
  90. /** {@inheritDoc} */
  91. @Override
  92. public void warn(String msg, Throwable th) {
  93. synchronized (warnings) {
  94. warnings.add(new Warning(msg, th));
  95. }
  96. }
  97. /**
  98. * Warning
  99. *
  100. * @param msg
  101. * warning message
  102. */
  103. public void warn(String msg) {
  104. synchronized (warnings) {
  105. warnings.add(new Warning(msg));
  106. }
  107. }
  108. /**
  109. * Debug log
  110. *
  111. * @param msg
  112. * @param arg0
  113. * @param arg1
  114. */
  115. public void debug(String msg, Object arg0, Object arg1) {
  116. // Ignore (not relevant to test failures)
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public void debug(String msg, Throwable th) {
  121. // Ignore (not relevant to test failures)
  122. }
  123. /**
  124. * Debug log
  125. *
  126. * @param msg
  127. * debug message
  128. */
  129. public void debug(String msg) {
  130. // Ignore (not relevant to test failures)
  131. }
  132. /**
  133. * Info
  134. *
  135. * @param msg
  136. * @param arg0
  137. * @param arg1
  138. */
  139. public void info(String msg, Object arg0, Object arg1) {
  140. // Ignore (not relevant to test failures)
  141. }
  142. /**
  143. * Info
  144. *
  145. * @param msg
  146. */
  147. public void info(String msg) {
  148. // Ignore (not relevant to test failures)
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public boolean isDebugEnabled() {
  153. return false;
  154. }
  155. /** {@inheritDoc} */
  156. @Override
  157. public void setDebugEnabled(boolean enabled) {
  158. // Ignore (not relevant to test failures)
  159. }
  160. /** {@inheritDoc} */
  161. @Override
  162. public void warn(String msg, Object... args) {
  163. synchronized (warnings) {
  164. int i = 0;
  165. int index = msg.indexOf("{}");
  166. while (index >= 0) {
  167. msg = msg.replaceFirst("\\{\\}", "{" + i++ + "}");
  168. index = msg.indexOf("{}");
  169. }
  170. warnings.add(new Warning(MessageFormat.format(msg, args)));
  171. }
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public void warn(Throwable thrown) {
  176. synchronized (warnings) {
  177. warnings.add(new Warning(thrown));
  178. }
  179. }
  180. /** {@inheritDoc} */
  181. @Override
  182. public void info(String msg, Object... args) {
  183. // Ignore (not relevant to test failures)
  184. }
  185. /** {@inheritDoc} */
  186. @Override
  187. public void info(Throwable thrown) {
  188. // Ignore (not relevant to test failures)
  189. }
  190. /** {@inheritDoc} */
  191. @Override
  192. public void info(String msg, Throwable thrown) {
  193. // Ignore (not relevant to test failures)
  194. }
  195. /** {@inheritDoc} */
  196. @Override
  197. public void debug(String msg, Object... args) {
  198. // Ignore (not relevant to test failures)
  199. }
  200. /** {@inheritDoc} */
  201. @Override
  202. public void debug(Throwable thrown) {
  203. // Ignore (not relevant to test failures)
  204. }
  205. /** {@inheritDoc} */
  206. @Override
  207. public void ignore(Throwable arg0) {
  208. // Ignore (not relevant to test failures)
  209. }
  210. /** {@inheritDoc} */
  211. @Override
  212. public void debug(String msg, long value) {
  213. // Ignore (not relevant to test failures)
  214. }
  215. }