1 package org.robolectric.util; 2 3 /** 4 * Logger for Robolectric. For now, it simply prints messages to stdout. 5 * 6 * Logging can be enabled by setting the property: {@code robolectric.logging.enabled = true}. 7 */ 8 public class Logger { 9 private static final String LOGGING_ENABLED = "robolectric.logging.enabled"; 10 strict(String message, Throwable e)11 public static void strict(String message, Throwable e) { 12 if (loggingEnabled()) { 13 System.out.print("WARNING: "); 14 System.out.println(message); 15 e.printStackTrace(); 16 } 17 } 18 strict(String message, Object... args)19 public static void strict(String message, Object... args) { 20 if (loggingEnabled()) { 21 System.out.print("WARNING: "); 22 System.out.println(String.format(message, args)); 23 } 24 } 25 26 /** 27 * Log an info message. 28 * 29 * @param message Message text. 30 * @param args Message arguments. 31 */ info(String message, Object... args)32 public static void info(String message, Object... args) { 33 if (loggingEnabled()) { 34 System.out.print("INFO: "); 35 System.out.println(String.format(message, args)); 36 } 37 } 38 39 /** 40 * Log a warning message. 41 * 42 * @param message Message text. 43 * @param args Message arguments. 44 */ warn(String message, Object... args)45 public static void warn(String message, Object... args) { 46 if (loggingEnabled()) { 47 System.out.print("WARN: "); 48 System.out.println(String.format(message, args)); 49 } 50 } 51 52 /** 53 * Log an error message. 54 * 55 * @param message Message text. 56 * @param e The exception. 57 */ error(String message, Throwable e)58 public static void error(String message, Throwable e) { 59 System.err.print("ERROR: "); 60 System.err.println(message); 61 e.printStackTrace(); 62 } 63 64 /** 65 * Log an error message. 66 * 67 * @param message Message text. 68 * @param args Message arguments. 69 */ error(String message, Object... args)70 public static void error(String message, Object... args) { 71 System.err.print("ERROR: "); 72 System.err.println(String.format(message, args)); 73 } 74 75 /** 76 * Log a debug message. 77 * 78 * @param message Message text. 79 * @param args Message arguments. 80 */ debug(String message, Object... args)81 public static void debug(String message, Object... args) { 82 if (loggingEnabled()) { 83 System.out.print("DEBUG: "); 84 System.out.println(String.format(message, args)); 85 } 86 } 87 loggingEnabled()88 public static boolean loggingEnabled() { 89 return Boolean.getBoolean(LOGGING_ENABLED); 90 } 91 } 92