All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.awidesky.guiUtil.TaskBufferedLogger Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
/*
 * Copyright (c) 2023 Eugene Hong
 *
 * This software is distributed under license. Use of this software
 * implies agreement with all terms and conditions of the accompanying
 * software license.
 * Please refer to LICENSE
 * */

package io.github.awidesky.guiUtil;

import java.io.Flushable;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.function.Consumer;

/**
 * A TaskLogger that buffer all logs to StringWriter and does not actually prints it
 * before {@code TaskBufferedLogger#flush()} is called.
 * {@code TaskBufferedLogger#runLogTask(Consumer)} is not supported; this class is for buffered operation only.
 * */
public abstract class TaskBufferedLogger extends TaskLogger implements Flushable {

	private StringWriter buffer = new StringWriter();

	/**
	 * Creates a task based buffered logger.
	 * */
	public TaskBufferedLogger(boolean verbose, String prefix) {
		super(verbose, prefix);
	}

	/**
	 * A TaskBufferedLogger does not support immediate logging.
	 * */
	@Override
	protected final boolean runLogTask(Consumer logTask) {
		throw new UnsupportedOperationException("TaskBufferedLogger supports buffered operation only!");
	}
	
	/**
	 * A TaskBufferedLogger does not support immediate logging.
	 * */
	@Override
	public boolean logNow(String data) {
		throw new UnsupportedOperationException("TaskBufferedLogger supports buffered operation only!");
	}

	/**
	 * Log empty newLine.
	 * */
	@Override
	public void newLine() {
		buffer.append(System.lineSeparator());
	}
	
	/**
	 * Logs a String.
	 * */
	@Override
	public void log(String data) {
		buffer.append(data);
		newLine();
	}
	
	/**
	 * Empty the buffer and put logs into the queue. 
	 * */
	@Override
	public void flush() {
		queueLogTask(getLogTask(buffer.toString()));
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy