Files
sdrtrunk/src/log/Log.java
dennis.sheirer fe8612b68f R820T Tuner Controller - expanded tunable frequency range to cover 3.18 MHz to 1782.03 MHz, although I don't currently have an antenna to test the lower HF frequencies.
- Resolved major issue where the tuner was only tuning half of the frequencies in a non-contiguous manner.
2014-09-19 00:22:58 +00:00

281 lines
6.8 KiB
Java

/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 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 log;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import properties.SystemProperties;
import util.TimeStamp;
public class Log
{
private static Writer mLogFile;
public Log()
{
}
private static void log( String msg )
{
log( msg, null);
}
/**
* Logs just the raw message, without a preceeding timestamp
*/
public void logRaw( String rawmsg )
{
try
{
mLogFile.write( rawmsg );
}
catch ( IOException e )
{
System.out.println(
"Couldn't write warning msg to log file: " + rawmsg );
}
}
private static void log( String msg, String label)
{
if( mLogFile != null )
{
try
{
mLogFile.write( TimeStamp.getTimeStamp( " " ) );
mLogFile.write( " " );
if( label != null )
{
mLogFile.write( label + ":" );
}
mLogFile.write( msg + "\n" );
mLogFile.flush();
}
catch (IOException e)
{
System.out.println(
"Couldn't write warning msg to log file: " + msg );
}
}
//echo to console
System.out.println( ( label == null ? "" : label + ": " ) + msg );
}
private static void log( String msg, String label, Throwable throwable )
{
StringBuilder sb = new StringBuilder();
if( msg != null )
{
sb.append( msg );
sb.append( "\n" );
}
sb.append( "---- STACK TRACE ----\n" );
sb.append( "\tERROR: " );
sb.append( "\tCause: " + throwable.getCause().toString() );
sb.append( "\t" + throwable.getMessage() );
sb.append( "\t" + throwable.getLocalizedMessage() + "\n" );
StackTraceElement[] stack = throwable.getStackTrace();
for( StackTraceElement element: stack )
{
sb.append( "\t" );
sb.append( element.toString() );
sb.append( ":" );
sb.append( element.getLineNumber() );
sb.append( "\n" );
}
sb.append( "\n" );
log( sb.toString(), label );
for( Throwable t: throwable.getSuppressed() )
{
log( "Suppressed", label, t );
}
}
public static void header( String name )
{
StringBuilder sb = new StringBuilder();
sb.append( "********** " );
sb.append( name );
sb.append( " **********" );
log( sb.toString() );
}
public static void info( String msg )
{
log( msg );
}
public static void info( byte[] bytes )
{
StringBuilder sb = new StringBuilder();
for( int x = 0; x < bytes.length; x++ )
{
sb.append( String.format( "%02X ", bytes[ x ] ) );
}
info( sb.toString() );
}
public static void info( String header, byte[] bytes )
{
StringBuilder sb = new StringBuilder();
sb.append( header );
for( int x = 0; x < bytes.length; x++ )
{
sb.append( String.format( "%02X ", bytes[ x ] ) );
}
info( sb.toString() );
}
public static void warning( String msg )
{
log( msg, "WARNING" );
}
public static void error( String msg )
{
log( msg, "ERROR" );
}
public static void error( String msg, Throwable throwable )
{
log( msg, "ERROR", throwable );
}
public static void debug( String msg )
{
log( msg, "DEBUG" );
}
public static void debug( String msg, Throwable throwable )
{
log( msg, "DEBUG", throwable );
}
/**
* Initializes logging to the specified filename
*/
public static void init( String filename )
{
try
{
mLogFile = new OutputStreamWriter(
new FileOutputStream( filename ) );
Log.info( "SDRTrunk Multi-Channel Trunking Decoder Application Log" );
Log.info( "Log - application logging started [" + filename + "]" );
logUserEnvironment();
}
catch (FileNotFoundException e)
{
System.out.println("Couldn't open log file:" + filename );
}
}
/**
* Initializes the log file using system properties settings
*/
public static void init()
{
SystemProperties props = SystemProperties.getInstance();
String logBaseFileName = props.get( "log.basefilename",
"_SDRTrunk_application" );
Path logPath = props.getApplicationFolder( "logs" );
if( !Files.exists( logPath ) )
{
try
{
Files.createDirectory( logPath );
}
catch ( IOException e )
{
Log.error( "Log - couldn't create 'logs' subdirectory in the " +
"SDRTrunk application directory" );
}
}
String logFileName = TimeStamp.getTimeStamp( "_" ) + logBaseFileName +
".log";
Path logFile = logPath.resolve( logFileName );
init( logFile.toString() );
}
public static void logUserEnvironment()
{
Log.header( "User Environment" );
Log.info( "OS:\t\t\t" + System.getProperty( "os.name" ) );
Log.info( "OS Version:\t\t" + System.getProperty( "os.version" ) );
Log.info( "OS Arch:\t\t" + System.getProperty( "os.arch" ) );
Log.info( "Java Vendor:\t\t" + System.getProperty( "java.vendor" ) );
Log.info( "Java Vendor URL:\t" + System.getProperty( "java.vendor.url" ) );
Log.info( "Java Version:\t\t" + System.getProperty( "java.version" ) );
Log.info( "User:\t\t\t" + System.getProperty( "user.name" ) );
Log.info( "User Home:\t\t" + System.getProperty( "user.home" ) );
Log.info( "User Directory:\t\t" + System.getProperty( "user.dir" ) );
Log.header( "Java Class Path" );
Log.info( System.getProperty( "java.class.path" ) );
}
public static void close()
{
if( mLogFile != null )
{
try
{
mLogFile.flush();
mLogFile.close();
}
catch( Exception e )
{
System.out.println( "Couldn't close log file" );
}
}
}
}