1package com.github.javaparser.utils; 2 3import java.io.IOException; 4import java.io.PrintWriter; 5import java.io.StringWriter; 6 7import static com.github.javaparser.utils.CodeGenerationUtils.f; 8 9/** 10 * To avoid dependencies on logging frameworks, we have invented yet another logging framework :-) 11 */ 12public class Log { 13 /** 14 * This adapter logs to standard out and standard error. 15 */ 16 public static class StandardOutStandardErrorAdapter implements Adapter { 17 @Override 18 public void info(String message) { 19 System.out.println(message); 20 } 21 22 @Override 23 public void trace(String message) { 24 System.out.println(message); 25 } 26 27 @Override 28 public void error(Throwable throwable, String message) { 29 if (message == null) { 30 System.err.println(throwable.getMessage()); 31 printStackTrace(throwable); 32 } else if (throwable == null) { 33 System.err.println(message); 34 } else { 35 System.err.println(message + ":" + throwable.getMessage()); 36 printStackTrace(throwable); 37 } 38 } 39 40 private void printStackTrace(Throwable throwable) { 41 try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) { 42 throwable.printStackTrace(pw); 43 trace(sw.toString()); 44 } catch (IOException e) { 45 throw new AssertionError("Error in logging library"); 46 } 47 } 48 } 49 50 /** 51 * This adapter logs nothing. 52 */ 53 public static class SilentAdapter implements Adapter { 54 @Override 55 public void info(String message) { 56 } 57 58 @Override 59 public void trace(String message) { 60 } 61 62 @Override 63 public void error(Throwable throwable, String f) { 64 } 65 } 66 67 public interface Adapter { 68 69 void info(String message); 70 71 void trace(String message); 72 73 /** 74 * Both can be null. 75 */ 76 void error(Throwable throwable, String f); 77 } 78 79 private static Adapter CURRENT_ADAPTER = new SilentAdapter(); 80 81 /** 82 * Change how logging is handled. You can set your own implementation that forwards to your logging library. 83 */ 84 public static void setAdapter(Adapter adapter) { 85 CURRENT_ADAPTER = adapter; 86 } 87 88 /** 89 * For logging information that may help solving a problem. 90 */ 91 public static void trace(String format, Object... args) { 92 CURRENT_ADAPTER.trace(f(format, args)); 93 } 94 95 /** 96 * For logging things that are nice to see scrolling by. 97 */ 98 public static void info(String format, Object... args) { 99 CURRENT_ADAPTER.info(f(format, args)); 100 } 101 102 /** 103 * For drawing attention to an error. 104 */ 105 public static void error(Throwable throwable) { 106 CURRENT_ADAPTER.error(throwable, null); 107 } 108 109 /** 110 * For drawing attention to an error that you don't have an exception for. 111 */ 112 public static void error(Throwable throwable, String format, Object... args) { 113 CURRENT_ADAPTER.error(throwable, f(format, args)); 114 } 115 116 /** 117 * For drawing attention to an error that you don't have an exception for. 118 */ 119 public static void error(String format, Object... args) { 120 CURRENT_ADAPTER.error(null, f(format, args)); 121 } 122} 123