View Javadoc
1   package org.newdawn.slick.util;
2   
3   import java.security.AccessController;
4   import java.security.PrivilegedAction;
5   
6   /**
7    * A simple central logging system.
8    * 
9    * @author kevin
10   */
11  public final class Log {
12      /** True if we're doing verbose logging INFO and DEBUG. */
13      private static boolean verbose = true;
14      /** true if activated by the system property "org.newdawn.slick.forceVerboseLog". */
15      private static boolean forcedVerbose = false;
16  
17      /**
18       * The debug property which can be set via JNLP or startup parameter to switch
19       * logging mode to verbose for games that were released without verbose logging
20       * value must be "true".
21       */
22      private static final String forceVerboseProperty = "org.newdawn.slick.forceVerboseLog";
23  
24      /**
25       * the verbose property must be set to "true" to switch on verbose logging.
26       */
27      private static final String forceVerbosePropertyOnValue = "true";
28  
29      /** The log system plugin in use. */
30      private static LogSystem logSystem = new DefaultLogSystem();
31  
32      /**
33       * The log is a simple static utility, no construction.
34       */
35      private Log() {
36  
37      }
38  
39      /**
40       * Set the log system that will have all of the log info
41       * sent to it.
42       *
43       * @param system The system to use for logging.
44       */
45      public static void setLogSystem(LogSystem system) {
46          logSystem = system;
47      }
48  
49      /**
50       * Indicate that we want verbose logging.
51       * The call is ignored if verbose logging is forced by the system property
52       * "org.newdawn.slick.forceVerboseLog"
53       *
54       * @param v True if we want verbose logging (INFO and DEBUG)
55       */
56      public static void setVerbose(boolean v) {
57          if (forcedVerbose)
58              return;
59          verbose = v;
60      }
61  
62      /**
63       * Check if the system property org.newdawn.slick.verboseLog is set to true.
64       * If this is the case we activate the verbose logging mode
65       */
66      public static void checkVerboseLogSetting() {
67          try {
68              AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
69                  String val = System.getProperty(Log.forceVerboseProperty);
70                  if ((val != null) && (val.equalsIgnoreCase(Log.forceVerbosePropertyOnValue))) {
71                      Log.setForcedVerboseOn();
72                  }
73                  return null;
74              });
75          } catch (Throwable e) {
76              // ignore, security failure - probably an applet
77          }
78      }
79  
80      /**
81       * Indicate that we want verbose logging, even if switched off in game code.
82       * Only be called when system property "org.newdawn.slick.forceVerboseLog" is set to true.
83       * You must not call this method directly.
84       */
85      private static void setForcedVerboseOn() {
86          forcedVerbose = true;
87          verbose = true;
88      }
89  
90      /**
91       * Log an error.
92       *
93       * @param message The message describing the error
94       * @param e The exception causing the error
95       */
96      public static void error(String message, Throwable e) {
97          logSystem.error(message, e);
98      }
99  
100     /**
101      * Log an error.
102      *
103      * @param e The exception causing the error
104      */
105     public static void error(Throwable e) {
106         logSystem.error(e);
107     }
108 
109     /**
110      * Log an error.
111      *
112      * @param message The message describing the error
113      */
114     public static void error(String message) {
115         logSystem.error(message);
116     }
117 
118     /**
119      * Log a warning.
120      *
121      * @param message The message describing the warning
122      */
123     public static void warn(String message) {
124         logSystem.warn(message);
125     }
126 
127     /**
128      * Log a warning.
129      *
130      * @param message The message describing the warning
131      * @param e The issue causing the warning
132      */
133     public static void warn(String message, Throwable e) {
134         logSystem.warn(message, e);
135     }
136 
137     /**
138      * Log an information message.
139      *
140      * @param message The message describing the information
141      */
142     public static void info(String message) {
143         if (verbose || forcedVerbose) {
144             logSystem.info(message);
145         }
146     }
147 
148     /**
149      * Log a debug message.
150      *
151      * @param message The message describing the debug
152      */
153     public static void debug(String message) {
154         if (verbose || forcedVerbose) {
155             logSystem.debug(message);
156         }
157     }
158 }