use logging instead of printing errors to System.err

This commit is contained in:
Karl Tauber
2019-12-23 11:29:17 +01:00
parent 32ceb168d5
commit ad82c591cc
6 changed files with 31 additions and 21 deletions

View File

@@ -17,6 +17,7 @@ FlatLaf Change Log
- Replaced prefix `@@` with `$` in `.properties` files.
- Fixed link color (in HTML text) and separator color in IntelliJ platform
themes.
- Use logging instead of printing errors to `System.err`.
## 0.22

View File

@@ -30,6 +30,8 @@ import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
@@ -55,6 +57,8 @@ import com.formdev.flatlaf.util.UIScale;
public abstract class FlatLaf
extends BasicLookAndFeel
{
static final Logger LOG = Logger.getLogger( FlatLaf.class.getName() );
private BasicLookAndFeel base;
private String desktopPropertyName;
@@ -67,11 +71,11 @@ public abstract class FlatLaf
public static boolean install( LookAndFeel newLookAndFeel ) {
try {
UIManager.setLookAndFeel( newLookAndFeel );
return true;
UIManager.setLookAndFeel( newLookAndFeel );
return true;
} catch( Exception ex ) {
System.err.println( "Failed to initialize look and feel " + newLookAndFeel.getClass().getName() );
return false;
LOG.log( Level.SEVERE, "FlatLaf: Failed to initialize look and feel '" + newLookAndFeel.getClass().getName() + "'.", ex );
return false;
}
}
@@ -182,10 +186,11 @@ public abstract class FlatLaf
if( base == null ) {
if( SystemInfo.IS_MAC ) {
// use Mac Aqua LaF as base
String aquaLafClassName = "com.apple.laf.AquaLookAndFeel";
try {
base = (BasicLookAndFeel) Class.forName( "com.apple.laf.AquaLookAndFeel" ).newInstance();
base = (BasicLookAndFeel) Class.forName( aquaLafClassName ).newInstance();
} catch( Exception ex ) {
ex.printStackTrace();
LOG.log( Level.SEVERE, "FlatLaf: Failed to initialize base look and feel '" + aquaLafClassName + "'.", ex );
throw new IllegalStateException();
}
} else
@@ -332,9 +337,9 @@ public abstract class FlatLaf
private static void reSetLookAndFeel() {
EventQueue.invokeLater( () -> {
LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
try {
// re-set current LaF
LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
UIManager.setLookAndFeel( lookAndFeel );
// must fire property change events ourself because old and new LaF are the same
@@ -345,7 +350,7 @@ public abstract class FlatLaf
// update UI
updateUI();
} catch( UnsupportedLookAndFeelException ex ) {
ex.printStackTrace();
LOG.log( Level.SEVERE, "FlatLaf: Failed to reinitialize look and feel '" + lookAndFeel.getClass().getName() + "'.", ex );
}
} );
}

View File

@@ -29,6 +29,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import javax.swing.UIDefaults;
import javax.swing.plaf.ColorUIResource;
import com.formdev.flatlaf.json.Json;
@@ -72,8 +73,7 @@ public class IntelliJTheme
try {
return FlatLaf.install( createLaf( in ) );
} catch( Exception ex ) {
System.err.println( "Failed to load IntelliJ theme" );
ex.printStackTrace();
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to load IntelliJ theme", ex );
return false;
}
}

View File

@@ -27,6 +27,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import javax.swing.text.StyleContext;
import com.formdev.flatlaf.util.StringUtils;
import com.formdev.flatlaf.util.SystemInfo;
@@ -161,7 +162,7 @@ class LinuxFontPolicy
if( "1".equals( strs.get( 5 ) ) )
style |= Font.ITALIC;
} catch( RuntimeException ex ) {
ex.printStackTrace();
FlatLaf.LOG.log( Level.CONFIG, "FlatLaf: Failed to parse 'font=" + generalFont + "'.", ex );
}
}
@@ -175,7 +176,7 @@ class LinuxFontPolicy
if( dpi < 50 )
dpi = 50;
} catch( NumberFormatException ex ) {
ex.printStackTrace();
FlatLaf.LOG.log( Level.CONFIG, "FlatLaf: Failed to parse 'forceFontDPI=" + forceFontDPI + "'.", ex );
}
}
@@ -214,7 +215,7 @@ class LinuxFontPolicy
while( (line = reader.readLine()) != null )
lines.add( line );
} catch( IOException ex ) {
ex.printStackTrace();
FlatLaf.LOG.log( Level.CONFIG, "FlatLaf: Failed to read '" + filename + "'.", ex );
}
return lines;
}

View File

@@ -30,6 +30,7 @@ import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.function.Function;
import java.util.logging.Level;
import javax.swing.UIDefaults;
import javax.swing.UIDefaults.ActiveValue;
import javax.swing.UIDefaults.LazyValue;
@@ -173,13 +174,12 @@ class UIDefaultsLoader
}
}
} catch( IOException ex ) {
ex.printStackTrace();
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to load properties files.", ex );
}
}
static void logParseError( String key, String value, RuntimeException ex ) {
System.err.println( "FlatLaf: Failed to parse: '" + key + '=' + value + '\'' );
System.err.println( " " + ex.getMessage() );
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to parse: '" + key + '=' + value + '\'', ex );
}
private static String resolveValue( Properties properties, String value ) {
@@ -190,7 +190,7 @@ class UIDefaultsLoader
// for compatibility
if( value.startsWith( REF_PREFIX ) ) {
System.err.println( "FlatLaf: Usage of '@@' in .properties files is deprecated. Use '$' instead." );
FlatLaf.LOG.log( Level.WARNING, "FlatLaf: Usage of '@@' in .properties files is deprecated. Use '$' instead." );
value = value.substring( REF_PREFIX.length() );
}
@@ -318,7 +318,7 @@ class UIDefaultsLoader
try {
return findClass( value, addonClassLoaders ).newInstance();
} catch( InstantiationException | IllegalAccessException | ClassNotFoundException ex ) {
ex.printStackTrace();
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to instantiate '" + value + "'.", ex );
return null;
}
};
@@ -329,7 +329,7 @@ class UIDefaultsLoader
try {
return findClass( value, addonClassLoaders );
} catch( ClassNotFoundException ex ) {
ex.printStackTrace();
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to find class '" + value + "'.", ex );
return null;
}
};

View File

@@ -20,7 +20,10 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import com.formdev.flatlaf.FlatLaf;
/**
* Provides Java version compatibility methods.
@@ -52,7 +55,7 @@ public class JavaCompatibility
? new Class[] { JComponent.class, Graphics2D.class, String.class, int.class, float.class, float.class }
: new Class[] { JComponent.class, Graphics.class, String.class, int.class, int.class, int.class } );
} catch( Exception ex ) {
ex.printStackTrace();
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
throw new RuntimeException( ex );
}
}
@@ -64,7 +67,7 @@ public class JavaCompatibility
else
drawStringUnderlineCharAtMethod.invoke( null, c, g, text, underlinedIndex, x, y );
} catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException ex ) {
ex.printStackTrace();
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
throw new RuntimeException( ex );
}
}