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. - Replaced prefix `@@` with `$` in `.properties` files.
- Fixed link color (in HTML text) and separator color in IntelliJ platform - Fixed link color (in HTML text) and separator color in IntelliJ platform
themes. themes.
- Use logging instead of printing errors to `System.err`.
## 0.22 ## 0.22

View File

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

View File

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

View File

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

View File

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