mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-10 23:57:25 -05:00
ant to gradle. Removes linux and windows application launch scripts and produces an executable jar with all resources included.
140 lines
3.9 KiB
Java
140 lines
3.9 KiB
Java
/*******************************************************************************
|
|
* sdrtrunk
|
|
* Copyright (C) 2014-2017 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.module.log;
|
|
|
|
import io.github.dsheirer.module.Module;
|
|
import io.github.dsheirer.util.TimeStamp;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.OutputStreamWriter;
|
|
import java.io.Writer;
|
|
import java.nio.file.Path;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
public abstract class EventLogger extends Module
|
|
{
|
|
private final static Logger mLog = LoggerFactory.getLogger(EventLogger.class);
|
|
|
|
private static final String[] ILLEGAL_FILENAME_CHARACTERS = {"#", "%", "&", "{", "}", "\\", "<", ">",
|
|
"*", "?", "/", " ", "$", "!", "'", "\"", ":", "@", "+", "`", "|", "="};
|
|
|
|
private Path mLogDirectory;
|
|
private String mFileNameSuffix;
|
|
private String mLogFileName;
|
|
protected Writer mLogFile;
|
|
|
|
public EventLogger(Path logDirectory, String fileNameSuffix)
|
|
{
|
|
mLogDirectory = logDirectory;
|
|
mFileNameSuffix = fileNameSuffix;
|
|
}
|
|
|
|
public String toString()
|
|
{
|
|
if(mLogFileName != null)
|
|
{
|
|
return mLogFileName;
|
|
}
|
|
else
|
|
{
|
|
return "Unknown";
|
|
}
|
|
}
|
|
|
|
public abstract String getHeader();
|
|
|
|
@Override
|
|
public void start(ScheduledExecutorService executor)
|
|
{
|
|
if(mLogFile == null)
|
|
{
|
|
try
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(mLogDirectory);
|
|
sb.append(File.separator);
|
|
sb.append(TimeStamp.getTimeStamp("_"));
|
|
sb.append("_");
|
|
sb.append(replaceIllegalCharacters(mFileNameSuffix));
|
|
|
|
mLogFileName = sb.toString();
|
|
|
|
mLogFile = new OutputStreamWriter(new FileOutputStream(mLogFileName));
|
|
|
|
write(getHeader());
|
|
}
|
|
catch(FileNotFoundException e)
|
|
{
|
|
mLog.error("Couldn't create log file in directory:" + mLogDirectory);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Replaces any illegal filename characters in the proposed filename
|
|
*/
|
|
private String replaceIllegalCharacters(String filename)
|
|
{
|
|
for(String illegalCharacter : ILLEGAL_FILENAME_CHARACTERS)
|
|
{
|
|
filename = filename.replace(illegalCharacter, "_");
|
|
}
|
|
|
|
return filename;
|
|
}
|
|
|
|
public void stop()
|
|
{
|
|
if(mLogFile != null)
|
|
{
|
|
try
|
|
{
|
|
mLogFile.flush();
|
|
mLogFile.close();
|
|
mLogFile = null;
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
mLog.error("Couldn't close log file:" + mFileNameSuffix);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected void write(String eventLogEntry)
|
|
{
|
|
if(mLogFile != null)
|
|
{
|
|
try
|
|
{
|
|
mLogFile.write(eventLogEntry + "\n");
|
|
mLogFile.flush();
|
|
}
|
|
catch(IOException e)
|
|
{
|
|
mLog.error("Error writing entry to event log file", e);
|
|
}
|
|
}
|
|
}
|
|
}
|