
ste.xtest.logging.ListLogHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xtest Show documentation
Show all versions of xtest Show documentation
A TDD framework to make coding drven by specifications easier
/*
* xTest
* Copyright (C) 2013 Stefano Fornari
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY Stefano Fornari, Stefano Fornari
* DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
package ste.xtest.logging;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
/**
*
* @author ste
*/
public class ListLogHandler extends Handler {
/**
* The list of collected log records
*/
private final List records;
public ListLogHandler() {
records = new ArrayList<>();
}
/**
* Returns the registered records
*
* @return the registered records
*/
public synchronized List getRecords() {
return records;
}
/**
* Publishes the given log record
*
* @param record the log record - NOT NULL
*
* @throws IllegalArgumentException if record is null
*/
@Override
public synchronized void publish(LogRecord record) throws IllegalArgumentException {
if (record == null) {
throw new IllegalArgumentException("record cannot be null");
}
if (isLoggable(record)) {
records.add(record);
}
}
@Override
public synchronized void flush() {
records.clear();
}
@Override
public void close() throws SecurityException {
//
// Nothing to do
//
}
/**
* Returns the message given the index
*
* @param index the index in the list
*
* @return the indexth message
*
* @throws IllegalArgumentException if index is out of the valid range
*/
public synchronized String getMessage(int index) throws IllegalArgumentException {
if ((index < 0) || (index >= records.size())) {
throw new IllegalArgumentException(
String.format("index cannot be < 0 or > %d (it was %d)", records.size(), index)
);
}
return records.get(index).getMessage();
}
/**
* Returns the number of records logged
*
* @return number of records logged
*/
public synchronized int size() {
return records.size();
}
/**
* Returns the record messages as a List
*
* @return the record messages as a List
*/
public synchronized List getMessages() {
return getMessages(Level.ALL);
}
/**
* Returns the record messages logged at the given level as a List
*
* @return the record messages logged at the given level as a List
*/
public synchronized List getMessages(Level level) {
List messages = new ArrayList<>();
for (LogRecord r: records) {
if ((level == Level.ALL) || (level == r.getLevel())) {
messages.add(r.getMessage());
}
}
return messages;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy