mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-13 09:07:27 -05:00
Co-authored-by: Dennis Sheirer <dsheirer@github.com>
This commit is contained in:
139
src/main/java/io/github/dsheirer/log/LoggingSuppressor.java
Normal file
139
src/main/java/io/github/dsheirer/log/LoggingSuppressor.java
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
/*
|
||||||
|
* *****************************************************************************
|
||||||
|
* Copyright (C) 2014-2023 Dennis Sheirer
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* 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 General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||||
|
* ****************************************************************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.github.dsheirer.log;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logging suppressor that squelches log messages after they have been logged and continue to occur.
|
||||||
|
*/
|
||||||
|
public class LoggingSuppressor
|
||||||
|
{
|
||||||
|
private Map<String,Integer> mSuppressionCountMap = new HashMap<>();
|
||||||
|
private Logger mLogger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an instance
|
||||||
|
* @param logger to receive suppressed log messages.
|
||||||
|
*/
|
||||||
|
public LoggingSuppressor(Logger logger)
|
||||||
|
{
|
||||||
|
mLogger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs the information message.
|
||||||
|
* @param key for suppression.
|
||||||
|
* @param maxCount of the logged instance.
|
||||||
|
* @param message to log.
|
||||||
|
*/
|
||||||
|
public void info(String key, int maxCount, String message)
|
||||||
|
{
|
||||||
|
if(canLog(key, maxCount))
|
||||||
|
{
|
||||||
|
mLogger.info(message + getTag(key, maxCount));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs the information message.
|
||||||
|
* @param key for suppression.
|
||||||
|
* @param maxCount of the logged instance.
|
||||||
|
* @param message to log.
|
||||||
|
* @param t stack trace to include.
|
||||||
|
*/
|
||||||
|
public void info(String key, int maxCount, String message, Throwable t)
|
||||||
|
{
|
||||||
|
if(canLog(key, maxCount))
|
||||||
|
{
|
||||||
|
mLogger.info(message + getTag(key, maxCount), t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs the error message.
|
||||||
|
* @param key for suppression.
|
||||||
|
* @param maxCount of the logged instance.
|
||||||
|
* @param message to log.
|
||||||
|
*/
|
||||||
|
public void error(String key, int maxCount, String message)
|
||||||
|
{
|
||||||
|
if(canLog(key, maxCount))
|
||||||
|
{
|
||||||
|
mLogger.error(message + getTag(key, maxCount));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs the error message.
|
||||||
|
* @param key for suppression.
|
||||||
|
* @param maxCount of the logged instance.
|
||||||
|
* @param message to log.
|
||||||
|
* @param t stack trace to include.
|
||||||
|
*/
|
||||||
|
public void error(String key, int maxCount, String message, Throwable t)
|
||||||
|
{
|
||||||
|
if(canLog(key, maxCount))
|
||||||
|
{
|
||||||
|
mLogger.error(message + getTag(key, maxCount), t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a logging tag that logs the suppression statistics.
|
||||||
|
* @param key for retrieving current count.
|
||||||
|
* @param maxCount to log.
|
||||||
|
* @return logging tag
|
||||||
|
*/
|
||||||
|
private String getTag(String key, int maxCount)
|
||||||
|
{
|
||||||
|
int count = mSuppressionCountMap.get(key);
|
||||||
|
return " [Log Suppress " + count + "/" + maxCount + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates if the operation can be logged without exceeding the max logging count specified by the user.
|
||||||
|
* @param key for tracking by count
|
||||||
|
* @param maxCount of the key to log
|
||||||
|
* @return true if can log or false.
|
||||||
|
*/
|
||||||
|
private boolean canLog(String key, int maxCount)
|
||||||
|
{
|
||||||
|
if(mSuppressionCountMap.containsKey(key))
|
||||||
|
{
|
||||||
|
int count = mSuppressionCountMap.get(key);
|
||||||
|
|
||||||
|
if(count >= maxCount)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mSuppressionCountMap.put(key, count + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mSuppressionCountMap.put(key, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,10 +18,10 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.dsheirer.sample;
|
package io.github.dsheirer.sample;
|
||||||
|
|
||||||
|
import io.github.dsheirer.log.LoggingSuppressor;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,11 +29,15 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
public class Broadcaster<T> implements Listener<T>
|
public class Broadcaster<T> implements Listener<T>
|
||||||
{
|
{
|
||||||
private final static Logger mLog = LoggerFactory.getLogger(Broadcaster.class);
|
private static LoggingSuppressor sLoggingSuppressor;
|
||||||
private List<Listener<T>> mListeners = new CopyOnWriteArrayList<>();
|
private List<Listener<T>> mListeners = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an instance
|
||||||
|
*/
|
||||||
public Broadcaster()
|
public Broadcaster()
|
||||||
{
|
{
|
||||||
|
sLoggingSuppressor = new LoggingSuppressor(LoggerFactory.getLogger(Broadcaster.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,16 +121,25 @@ public class Broadcaster<T> implements Listener<T>
|
|||||||
*/
|
*/
|
||||||
public void broadcast(T t)
|
public void broadcast(T t)
|
||||||
{
|
{
|
||||||
try
|
for(Listener<T> listener: mListeners)
|
||||||
{
|
{
|
||||||
for(Listener<T> listener: mListeners)
|
try
|
||||||
{
|
{
|
||||||
listener.receive(t);
|
listener.receive(t);
|
||||||
}
|
}
|
||||||
}
|
catch(Exception e)
|
||||||
catch(Exception e)
|
{
|
||||||
{
|
if(t != null)
|
||||||
mLog.error("Error while broadcasting [" + t.getClass() + "] to listeners");
|
{
|
||||||
|
sLoggingSuppressor.error(t.getClass().toGenericString(), 5,
|
||||||
|
"Error while broadcasting [" + t.getClass() + "] to listeners", e);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sLoggingSuppressor.error("null broadcast object", 5, "Can't broadcast null " +
|
||||||
|
"object to listener [" + listener.getClass() + "]", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user