Util: Change LogConsoleBuffer implementation (prep for ticket #2449)

This commit is contained in:
zzz
2020-05-04 15:26:50 +00:00
parent 26af857dc7
commit ea8e340895
2 changed files with 55 additions and 13 deletions

View File

@ -12,8 +12,8 @@ import net.i2p.I2PAppContext;
*/ */
public class LogConsoleBuffer { public class LogConsoleBuffer {
private final int lim; private final int lim;
private final LinkedBlockingQueue<String> _buffer; private final UIMessages _buffer;
private final LinkedBlockingQueue<String> _critBuffer; private final UIMessages _critBuffer;
/** /**
* Uses default limit from LogManager. * Uses default limit from LogManager.
@ -36,14 +36,12 @@ public class LogConsoleBuffer {
lim = Math.max(limit, 4); lim = Math.max(limit, 4);
// Add some extra room to minimize the chance of losing a message, // Add some extra room to minimize the chance of losing a message,
// since we are doing offer() below. // since we are doing offer() below.
_buffer = new LinkedBlockingQueue<String>(lim + 4); _buffer = new UIMessages(lim + 4);
_critBuffer = new LinkedBlockingQueue<String>(lim + 4); _critBuffer = new UIMessages(lim + 4);
} }
void add(String msg) { void add(String msg) {
while (_buffer.size() >= lim) _buffer.addMessageNoEscape(msg);
_buffer.poll();
_buffer.offer(msg);
} }
/** /**
@ -51,9 +49,7 @@ public class LogConsoleBuffer {
* *
*/ */
void addCritical(String msg) { void addCritical(String msg) {
while (_critBuffer.size() >= lim) _critBuffer.addMessageNoEscape(msg);
_critBuffer.poll();
_critBuffer.offer(msg);
} }
/** /**
@ -64,7 +60,7 @@ public class LogConsoleBuffer {
* @return oldest first * @return oldest first
*/ */
public List<String> getMostRecentMessages() { public List<String> getMostRecentMessages() {
return new ArrayList<String>(_buffer); return _buffer.getMessageStrings();
} }
/** /**
@ -75,7 +71,31 @@ public class LogConsoleBuffer {
* @return oldest first * @return oldest first
*/ */
public List<String> getMostRecentCriticalMessages() { public List<String> getMostRecentCriticalMessages() {
return new ArrayList<String>(_critBuffer); return _critBuffer.getMessageStrings();
}
/**
* Retrieve the currently buffered messages, earlier values were generated...
* earlier. All values are strings with no formatting (as they are written
* in the logs)
*
* @return oldest first
* @since 0.9.46
*/
public UIMessages getUIMessages() {
return _buffer;
}
/**
* Retrieve the currently buffered critical messages, earlier values were generated...
* earlier. All values are strings with no formatting (as they are written
* in the logs)
*
* @return oldest first
* @since 0.9.46
*/
public UIMessages getCriticalUIMessages() {
return _critBuffer;
} }
/** /**

View File

@ -69,11 +69,26 @@ public class UIMessages {
* @return a copy * @return a copy
*/ */
public synchronized List<Message> getMessages() { public synchronized List<Message> getMessages() {
if (_messages.isEmpty()) if (_messages.peekLast() == null)
return Collections.emptyList(); return Collections.emptyList();
return new ArrayList<Message>(_messages); return new ArrayList<Message>(_messages);
} }
/**
* Newest last, or empty list.
* @return a copy
* @since 0.9.46
*/
public synchronized List<String> getMessageStrings() {
if (_messages.peekLast() == null)
return Collections.emptyList();
List<String> rv = new ArrayList<String>(_messages.size());
for (Message m : _messages) {
rv.add(m.message);
}
return rv;
}
/** clear all */ /** clear all */
public synchronized void clear() { public synchronized void clear() {
_messages.clear(); _messages.clear();
@ -97,6 +112,13 @@ public class UIMessages {
} }
} }
/**
* @since 0.9.46
*/
public synchronized boolean isEmpty() {
return _messages.peekLast() == null;
}
public static class Message { public static class Message {
public final int id; public final int id;
public final String message; public final String message;