1 2 24 25 package com.lutris.logging; 26 27 import java.io.PrintWriter ; 28 import java.io.StringWriter ; 29 import java.text.DateFormat ; 30 import java.text.FieldPosition ; 31 import java.text.SimpleDateFormat ; 32 import java.util.Date ; 33 34 44 public class StandardLogChannel implements LogChannel { 45 48 private String facility; 49 50 53 private static final SimpleDateFormat dateFormatter = 54 new SimpleDateFormat ("yyyy.MM.dd HH:mm:ss"); 55 56 59 private StandardLogger logger; 60 61 62 69 protected StandardLogChannel(String chanFacility, 70 StandardLogger loggerObj) { 71 facility = chanFacility; 72 logger = loggerObj; 73 74 } 75 76 79 public int getLevel(String level) { 80 return logger.getLevel(level); 81 } 82 83 86 public LogWriter getLogWriter(String level) { 87 return new LogWriter(this, level); 88 } 89 90 93 public LogWriter getLogWriter(int level) { 94 return new LogWriter(this, level); 95 } 96 97 100 public boolean isEnabled(int level) { 101 boolean[] enabledLevelFlags = logger.enabledLevelFlags; 103 return (enabledLevelFlags != null) 104 && (level >= 0 && level < enabledLevelFlags.length) 105 && enabledLevelFlags[level]; 106 } 107 108 111 public boolean isEnabled(String level) { 112 return isEnabled(logger.getLevel(level)); 113 } 114 115 126 private void doWrite(PrintWriter out, Date date, int level, String msg) { 127 try { 128 StringBuffer msgBuf = new StringBuffer (); 130 dateFormatter.format(date, msgBuf, 131 new FieldPosition (DateFormat.YEAR_FIELD)); 132 msgBuf.append(": "); 133 msgBuf.append(facility); 134 msgBuf.append(','); 135 136 msgBuf.append(logger.levelNames[level]); 137 138 int nextIdx = 0; 140 int newlineIdx; 141 142 synchronized (out) { 143 while (nextIdx < msg.length()) { 144 out.print(msgBuf); 145 if (nextIdx == 0) { 146 out.print(": "); 147 } else { 148 out.print("+ "); 149 } 150 newlineIdx = msg.indexOf('\n', nextIdx); 151 if (newlineIdx < 0) { 152 newlineIdx = msg.length(); 153 } 154 out.write(msg, nextIdx, (newlineIdx - nextIdx)); 155 out.println(); 156 nextIdx = newlineIdx + 1; 157 } 158 out.flush(); 159 } 160 } catch (Throwable ignore) { 161 System.err.println("StandardLogChannel.doWrite ignored exception:"); 163 ignore.printStackTrace(); 164 } 166 } 167 168 169 172 public void write(int level, String msg) { 173 if (isEnabled(level)) { 174 Date date = new Date (); 175 if (logger.logFileLevelFlags[level]) { 176 doWrite(logger.logFileStream, date, level, msg); 177 } 178 if (logger.stderrLevelFlags[level]) { 179 doWrite(logger.stderrStream, date, level, msg); 180 } 181 } 182 } 183 184 187 public synchronized void write(String level, String msg) { 188 write(getLevel(level), msg); 189 } 190 191 194 public synchronized void write(int level, String msg, Throwable throwable) { 195 if (isEnabled(level)) { 196 Date date = new Date (); 197 StringWriter stackBuf = new StringWriter (); 198 throwable.printStackTrace(new PrintWriter (stackBuf)); 199 stackBuf.flush(); 200 201 String errMsg = msg + ":" + " " + throwable.getMessage() + '\n' 202 + stackBuf; 203 204 if (logger.logFileLevelFlags[level]) { 205 doWrite(logger.logFileStream, date, level, errMsg); 206 } 207 if (logger.stderrLevelFlags[level]) { 208 doWrite(logger.stderrStream, date, level, errMsg); 209 } 210 } 211 } 212 213 216 public synchronized void write(String level, String msg, Throwable throwable) { 217 write(getLevel(level), msg, throwable); 218 } 219 } 220 | Popular Tags |