1 2 24 25 package com.lutris.logging; 26 27 import java.io.PrintWriter ; 28 import java.io.Writer ; 29 30 36 public class LogWriter extends PrintWriter { 37 40 private LogChannel channel; 41 private int level; 42 43 46 private boolean enabled; 47 48 53 protected LogWriter(LogChannel logChannel, 54 String logLevel) { 55 this(logChannel, logChannel.getLevel(logLevel)); 56 } 57 58 63 protected LogWriter(LogChannel logChannel, 64 int logLevel) { 65 super(new ChannelWriter(logChannel, logLevel), true); 67 channel = logChannel; 68 level = logLevel; 69 enabled = logChannel.isEnabled(level); 70 } 71 72 75 public LogChannel getChannel() { 76 return channel; 77 } 78 79 82 public int getLevel() { 83 return level; 84 } 85 86 94 public boolean isEnabled() { 95 return enabled; 96 } 97 98 104 public void println(String msg, Throwable throwable) { 105 if (enabled) { 106 flush(); 107 channel.write(level, msg, throwable); 108 } 109 } 110 } 111 112 113 118 class ChannelWriter extends Writer { 119 private StringBuffer buffer = new StringBuffer (); 121 122 private LogChannel channel; 124 private int level; 125 private boolean enabled; 126 127 130 public ChannelWriter(LogChannel logChannel, 131 int logLevel) { 132 channel = logChannel; 133 level = logLevel; 134 enabled = logChannel.isEnabled(level); 135 } 136 137 140 public void write(char[] cbuf, int off, int len) { 141 buffer.append(cbuf, off, len); 142 } 143 144 147 public void flush() { 148 if (enabled && (buffer.length() > 0)) { 149 synchronized(lock) { 150 channel.write(level, buffer.toString()); 151 buffer.setLength(0); 152 } 153 } 154 } 155 156 159 public void close() { 160 } 161 } 162 163 | Popular Tags |