1 8 9 package com.sleepycat.je.utilint; 10 11 import java.io.PrintWriter ; 12 import java.io.StringWriter ; 13 import java.nio.ByteBuffer ; 14 import java.sql.Timestamp ; 15 import java.util.Calendar ; 16 import java.util.logging.Level ; 17 18 import com.sleepycat.je.DatabaseException; 19 import com.sleepycat.je.config.ConfigParam; 20 import com.sleepycat.je.dbi.EnvironmentImpl; 21 import com.sleepycat.je.log.LogEntryType; 22 import com.sleepycat.je.log.LogReadable; 23 import com.sleepycat.je.log.LogUtils; 24 import com.sleepycat.je.log.LoggableObject; 25 26 32 public class Tracer implements LoggableObject, LogReadable { 33 34 38 public static final String INFO_FILES = "je.info"; 39 40 43 private Timestamp time; 44 private String msg; 45 46 49 public Tracer(String msg) { 50 this.time = getCurrentTimestamp(); 51 this.msg = msg; 52 } 53 54 57 public Tracer() { 58 } 59 60 64 65 68 public static void trace(Level logLevel, 69 EnvironmentImpl envImpl, 70 String msg) { 71 envImpl.getLogger().log(logLevel, msg); 72 } 73 74 77 public static void trace(EnvironmentImpl envImpl, 78 String sourceClass, 79 String sourceMethod, 80 String msg, 81 Throwable t) { 82 83 87 envImpl.getLogger().logp(Level.SEVERE, 88 sourceClass, 89 sourceMethod, 90 msg + "\n" + Tracer.getStackTrace(t)); 91 } 92 93 97 public static Level parseLevel(EnvironmentImpl envImpl, 98 ConfigParam configParam) 99 throws DatabaseException { 100 101 Level level = null; 102 try { 103 String levelVal = envImpl.getConfigManager().get(configParam); 104 level = Level.parse(levelVal); 105 } catch (IllegalArgumentException e) { 106 throw new DatabaseException("Problem parsing parameter " + 107 configParam.getName() + 108 ": " + e.getMessage(), e); 109 } 110 return level; 111 } 112 113 116 public String getMessage() { 117 return msg; 118 } 119 120 123 private Timestamp getCurrentTimestamp() { 124 Calendar cal = Calendar.getInstance(); 125 return new Timestamp (cal.getTime().getTime()); 126 } 127 128 131 public static String getStackTrace(Throwable t) { 132 StringWriter s = new StringWriter (); 133 t.printStackTrace(new PrintWriter (s)); 134 String stackTrace = s.toString(); 135 stackTrace = stackTrace.replaceAll("<", "<"); 136 stackTrace = stackTrace.replaceAll(">", ">"); 137 return stackTrace; 138 } 139 140 143 144 147 public LogEntryType getLogType() { 148 return LogEntryType.LOG_TRACE; 149 } 150 151 155 public boolean marshallOutsideWriteLatch() { 156 return true; 157 } 158 159 162 public boolean countAsObsoleteWhenLogged() { 163 return false; 164 } 165 166 169 public void postLogWork(long justLoggedLsn) { 170 } 171 172 175 public int getLogSize() { 176 return (LogUtils.getTimestampLogSize() + 177 LogUtils.getStringLogSize(msg)); 178 } 179 180 183 public void writeToLog(ByteBuffer logBuffer) { 184 185 LogUtils.writeTimestamp(logBuffer, time); 186 LogUtils.writeString(logBuffer, msg); 187 } 188 189 192 public void readFromLog(ByteBuffer itemBuffer, byte entryTypeVersion) { 193 194 time = LogUtils.readTimestamp(itemBuffer); 195 msg = LogUtils.readString(itemBuffer); 196 } 197 198 201 public void dumpLog(StringBuffer sb, boolean verbose) { 202 sb.append("<Dbg time=\""); 203 sb.append(time); 204 sb.append("\">"); 205 sb.append("<msg val=\""); 206 sb.append(msg); 207 sb.append("\"/>"); 208 sb.append("</Dbg>"); 209 } 210 211 214 public boolean logEntryIsTransactional() { 215 return false; 216 } 217 218 221 public long getTransactionId() { 222 return 0; 223 } 224 225 public String toString() { 226 return (time + "/" + msg); 227 } 228 229 232 233 236 public int hashCode() { 237 return toString().hashCode(); 238 } 239 240 243 public boolean equals(Object obj) { 244 245 if (this == obj) { 246 return true; 247 } 248 249 250 if (!(obj instanceof Tracer)) { 251 return false; 252 } 253 254 259 return (toString().equals(obj.toString())); 260 } 261 } 262 | Popular Tags |