1 package org.sapia.ubik.rmi.server; 2 3 import java.io.PrintStream ; 4 5 import org.sapia.ubik.rmi.Consts; 6 import org.sapia.ubik.util.Debug; 7 8 9 19 public class Log { 20 static final int DEBUG = 0; 21 static final int INFO = 1; 22 static final int WARNING = 2; 23 static final int ERROR = 3; 24 static final int REPORT = 4; 25 private static int _lvl = ERROR; 26 private static final String [] LABELS = new String [] { 27 "debug", "info", "warning", "error" 28 }; 29 30 static { 31 String label = System.getProperty(Consts.LOG_LEVEL); 32 33 if (label != null) { 34 for (int i = 0; i < LABELS.length; i++) { 35 if (LABELS[i].equals(label)) { 36 _lvl = i; 37 38 break; 39 } 40 } 41 } 42 } 43 44 public static final void setDebug() { 45 _lvl = DEBUG; 46 } 47 48 public static final void setInfo() { 49 _lvl = INFO; 50 } 51 52 public static final void setWarning() { 53 _lvl = WARNING; 54 } 55 56 public static final void setError() { 57 _lvl = ERROR; 58 } 59 60 public static final void debug(Class caller, Object msg) { 61 if (_lvl <= DEBUG) { 62 debug(caller.getName(), msg); 63 } 64 } 65 66 public static final void debug(String caller, Object msg) { 67 if (_lvl <= DEBUG) { 68 display(caller, msg); 69 } 70 } 71 72 public static final void info(Class caller, Object msg) { 73 if (_lvl <= INFO) { 74 info(caller.getName(), msg); 75 } 76 } 77 78 public static final void info(String caller, Object msg) { 79 if (_lvl <= INFO) { 80 display(caller, msg); 81 } 82 } 83 84 public static final void warning(Class caller, Object msg) { 85 if (_lvl <= WARNING) { 86 warning(caller.getName(), msg); 87 } 88 } 89 90 public static final void warning(String caller, Object msg) { 91 if (_lvl <= WARNING) { 92 display(caller, msg); 93 } 94 } 95 96 public static final void error(Class caller, Object msg) { 97 if (_lvl <= ERROR) { 98 error(caller.getName(), msg); 99 } 100 } 101 102 public static final void error(String caller, Object msg) { 103 if (_lvl <= ERROR) { 104 display(caller, msg); 105 } 106 } 107 108 public static final void error(Class caller, Object msg, Throwable t) { 109 if (_lvl <= ERROR) { 110 error(caller.getName(), msg, t); 111 } 112 } 113 114 public static final void error(String caller, Object msg, Throwable t) { 115 if (_lvl <= ERROR) { 116 display(caller, msg, t); 117 } 118 } 119 120 public static final void report(Class caller, Object msg){ 121 display(caller.getName(), msg); 122 } 123 124 public static final void report(String caller, Object msg){ 125 display(caller, msg); 126 } 127 128 public static boolean isDebug() { 129 return _lvl <= DEBUG; 130 } 131 132 public static boolean isInfo() { 133 return _lvl <= INFO; 134 } 135 136 public static boolean isWarning() { 137 return _lvl <= WARNING; 138 } 139 140 public static boolean isError() { 141 return _lvl <= ERROR; 142 } 143 144 public static int getLevel() { 145 return _lvl; 146 } 147 148 public static Debug getDebugImpl(){ 149 return new RmiDebug(); 150 } 151 152 private static void display(String caller, Object msg) { 153 if (msg instanceof Throwable ) { 154 System.out.println("[" + new java.util.Date ().toString() + "][" + caller + 155 "] " + ((Throwable ) msg).getMessage()); 156 ((Throwable ) msg).printStackTrace(); 157 } else { 158 System.out.println("[" + new java.util.Date ().toString() + "][" + caller + 159 "] " + msg); 160 } 161 } 162 163 private static void display(String caller, Object msg, Throwable t) { 164 System.out.println("[" + new java.util.Date ().toString() + "][" + caller + 165 "] " + msg + " - " + t.getMessage()); 166 167 t.printStackTrace(); 168 } 169 170 172 static final class RmiDebug implements Debug{ 173 174 public void out(Class caller, String msg){ 175 Log.debug(caller, msg); 176 } 177 178 public void out(Class caller, String msg, Throwable err){ 179 Log.error(caller, msg, err); 180 } 181 182 public PrintStream out(){ 183 return System.out; 184 } 185 186 public boolean on(){ 187 return true; 188 } 189 190 public void on(boolean on){ 191 } 192 } 193 } 194 | Popular Tags |