1 2 12 package com.versant.core.logging; 13 14 import com.versant.core.common.BindingSupportImpl; 15 16 20 public final class LogEventStore { 21 22 private int size = 1000; 23 private LogEvent[] buf = new LogEvent[size]; 24 private int pos; private int count; 26 private int eventsLogged; 27 28 public static final String LOG_EVENTS_NONE = "none"; 29 public static final String LOG_EVENTS_ERRORS = "errors"; 30 public static final String LOG_EVENTS_NORMAL = "normal"; 31 public static final String LOG_EVENTS_VERBOSE = "verbose"; 32 public static final String LOG_EVENTS_ALL = "all"; 33 34 private String logEvents; 35 36 private boolean severe; 37 private boolean warning; 38 private boolean info; 39 private boolean config; 40 private boolean fine; 41 private boolean finer; 42 private boolean finest; 43 44 private boolean logEventsToSysOut = true; 45 46 public LogEventStore() { 47 setLogEvents(LOG_EVENTS_NORMAL); 48 } 49 50 public synchronized void log(LogEvent ev) { 51 synchronized (this) { 52 buf[pos] = ev; 53 pos = (pos + 1) % size; 54 if (count < size) count++; 55 } 56 eventsLogged++; 57 if (logEventsToSysOut) System.out.println(ev); 58 } 59 60 public int getMaxEvents() { 61 return size; 62 } 63 64 67 public int getEventsLogged() { 68 return eventsLogged; 69 } 70 71 74 public synchronized void setMaxEvents(int max) { 75 LogEvent[] old = copyEvents(0); 76 buf = new LogEvent[size = max]; 77 if (old != null) { 78 int n = old.length; 79 if (n > max) n = max; 80 System.arraycopy(old, old.length - n, buf, 0, n); 81 count = n; 82 pos = n % size; 83 } else { 84 count = 0; 85 pos = 0; 86 } 87 } 88 89 public synchronized LogEvent[] copyEvents(int id) { 90 if (count < size) { 91 int first; 92 for (first = pos - 1; first >= 0; first--) { 93 if (buf[first].getId() == id) break; 94 } 95 first++; 96 int n = pos - first; 97 if (n == 0) return null; 98 LogEvent[] ans = new LogEvent[n]; 99 System.arraycopy(buf, first, ans, 0, n); 100 return ans; 101 } else { 102 if (buf[(pos + size - 1) % size].getId() == id) return null; 103 int first = pos; 104 int c = size; 105 for (; c > 0; first = (first + 1) % size, c--) { 106 if (buf[first].getId() == id) break; 107 } 108 first = (first + 1) % size; 109 if (first >= pos) { 110 int h1 = size - first; 111 int h2 = pos; 112 LogEvent[] ans = new LogEvent[h1 + h2]; 113 System.arraycopy(buf, first, ans, 0, h1); 114 System.arraycopy(buf, 0, ans, h1, h2); 115 return ans; 116 } else { 117 int n = pos - first; 118 LogEvent[] ans = new LogEvent[n]; 119 System.arraycopy(buf, first, ans, 0, n); 120 return ans; 121 } 122 } 123 } 124 125 public boolean isSevere() { 126 return severe; 127 } 128 129 public boolean isWarning() { 130 return warning; 131 } 132 133 public boolean isInfo() { 134 return info; 135 } 136 137 public boolean isConfig() { 138 return config; 139 } 140 141 public boolean isFine() { 142 return fine; 143 } 144 145 public boolean isFiner() { 146 return finer; 147 } 148 149 public boolean isFinest() { 150 return finest; 151 } 152 153 157 public Object getStatusBean() { 158 return null; 159 } 160 161 public String getLogEvents() { 162 return logEvents; 163 } 164 165 public void setLogEvents(String logEvents) { 166 if (logEvents.equals(LOG_EVENTS_NONE)) { 167 severe = false; 168 warning = false; 169 info = false; 170 config = false; 171 fine = false; 172 finer = false; 173 finest = false; 174 } else if (logEvents.equals(LOG_EVENTS_ERRORS)) { 175 severe = true; 176 warning = false; 177 info = false; 178 config = false; 179 fine = false; 180 finer = false; 181 finest = false; 182 } else if (logEvents.equals(LOG_EVENTS_NORMAL)) { 183 severe = true; 184 warning = true; 185 info = true; 186 config = false; 187 fine = false; 188 finer = false; 189 finest = false; 190 } else if (logEvents.equals(LOG_EVENTS_VERBOSE)) { 191 severe = true; 192 warning = true; 193 info = true; 194 config = true; 195 fine = true; 196 finer = false; 197 finest = false; 198 } else if (logEvents.equals(LOG_EVENTS_ALL)) { 199 severe = true; 200 warning = true; 201 info = true; 202 config = true; 203 fine = true; 204 finer = true; 205 finest = true; 206 } else { 207 throw BindingSupportImpl.getInstance().illegalArgument( 208 "Invalid group: '" + logEvents + "'"); 209 } 210 this.logEvents = logEvents; 211 } 212 213 public boolean isLogEventsToSysOut() { 214 return logEventsToSysOut; 215 } 216 217 public void setLogEventsToSysOut(boolean logEventsToSysOut) { 218 this.logEventsToSysOut = logEventsToSysOut; 219 } 220 221 } 222 | Popular Tags |