View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.rng.examples.stress;
18  
19  import java.io.PrintStream;
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  
23  /**
24   * Simple utility for logging messages to the console.
25   *
26   * <p>This is not a replacement for a logging framework and may be replaced in the future.
27   */
28  final class LogUtils {
29      /** The logging level. */
30      private static LogLevel logLevel = LogLevel.INFO;
31  
32      /**
33       * The log level.
34       */
35      enum LogLevel {
36          /** The error log level. */
37          ERROR(0),
38          /** The information log level. */
39          INFO(1),
40          /** The debug log level. */
41          DEBUG(2);
42  
43          /** The prefix for log messages. */
44          private final String prefix;
45  
46          /** The logging level. */
47          private final int level;
48  
49          /**
50           * Create a new instance.
51           *
52           * @param level The level.
53           */
54          LogLevel(int level) {
55              // Just use the name
56              prefix = "[" + name() + "] ";
57              this.level = level;
58          }
59  
60          /**
61           * Gets the message prefix.
62           *
63           * @return the prefix
64           */
65          String getPrefix() {
66              return prefix;
67          }
68  
69          /**
70           * Gets the level.
71           *
72           * @return the level
73           */
74          int getLevel() {
75              return level;
76          }
77      }
78  
79      /**
80       * No public construction.
81       */
82      private LogUtils() {}
83  
84      /**
85       * Sets the log level.
86       *
87       * @param logLevel The new log level.
88       */
89      static void setLogLevel(LogLevel logLevel) {
90          LogUtils.logLevel = logLevel;
91      }
92  
93      /**
94       * Checks if the given level is loggable.
95       *
96       * @param level The level.
97       * @return true if loggable
98       */
99      static boolean isLoggable(LogLevel level) {
100         return level.getLevel() <= LogUtils.logLevel.getLevel();
101     }
102 
103     /**
104      * Log a debug message to {@link System#out}.
105      *
106      * @param message The message.
107      */
108     static void debug(String message) {
109         if (isLoggable(LogLevel.DEBUG)) {
110             println(System.out, LogLevel.DEBUG.getPrefix() + message);
111         }
112     }
113 
114     /**
115      * Log a debug message to {@link System#out}.
116      *
117      * @param format The format.
118      * @param args The arguments.
119      */
120     static void debug(String format, Object... args) {
121         if (isLoggable(LogLevel.DEBUG)) {
122             printf(System.out, LogLevel.DEBUG.getPrefix() + format, args);
123         }
124     }
125     /**
126      * Log an info message to {@link System#out}.
127      *
128      * @param message The message.
129      */
130     static void info(String message) {
131         if (isLoggable(LogLevel.INFO)) {
132             println(System.out, LogLevel.INFO.getPrefix() + message);
133         }
134     }
135 
136     /**
137      * Log an info message to {@link System#out}.
138      *
139      * @param format The format.
140      * @param args The arguments.
141      */
142     static void info(String format, Object... args) {
143         if (isLoggable(LogLevel.INFO)) {
144             printf(System.out, LogLevel.INFO.getPrefix() + format, args);
145         }
146     }
147 
148     /**
149      * Log an error message to {@link System#err}.
150      *
151      * @param message The message.
152      */
153     static void error(String message) {
154         if (isLoggable(LogLevel.ERROR)) {
155             println(System.err, LogLevel.ERROR.getPrefix() + message);
156         }
157     }
158 
159     /**
160      * Log an error message to {@link System#err}.
161      *
162      * @param format The format.
163      * @param args The arguments.
164      */
165     static void error(String format, Object... args) {
166         if (isLoggable(LogLevel.ERROR)) {
167             printf(System.err, LogLevel.ERROR.getPrefix() + format, args);
168         }
169     }
170 
171     /**
172      * Log an error message to {@link System#err}. The stack trace of the thrown is added.
173      *
174      * @param thrown The thrown.
175      * @param message The message.
176      */
177     static void error(Throwable thrown, String message) {
178         if (isLoggable(LogLevel.ERROR)) {
179             final StringWriter sw = new StringWriter();
180             try (PrintWriter pw = createErrorPrintWriter(sw)) {
181                 pw.print(message);
182                 addStackTrace(pw, thrown);
183             }
184             println(System.err, sw.toString());
185         }
186     }
187 
188     /**
189      * Log an error message to {@link System#err}. The stack trace of the thrown is
190      * added.
191      *
192      * @param thrown The thrown.
193      * @param format The format.
194      * @param args The arguments.
195      */
196     static void error(Throwable thrown, String format, Object... args) {
197         if (isLoggable(LogLevel.ERROR)) {
198             final StringWriter sw = new StringWriter();
199             try (PrintWriter pw = createErrorPrintWriter(sw)) {
200                 pw.printf(format, args);
201                 addStackTrace(pw, thrown);
202             }
203             printf(System.err, sw.toString());
204         }
205     }
206 
207     /**
208      * Creates the error print writer. It will contain the prefix for error
209      * messages.
210      *
211      * @param sw The string writer.
212      * @return the print writer
213      */
214     private static PrintWriter createErrorPrintWriter(StringWriter sw) {
215         final PrintWriter pw = new PrintWriter(sw);
216         pw.print(LogLevel.ERROR.getPrefix());
217         return pw;
218     }
219 
220     /**
221      * Adds the stack trace to the print writer.
222      *
223      * @param pw The print writer.
224      * @param thrown The thrown.
225      */
226     private static void addStackTrace(PrintWriter pw, Throwable thrown) {
227         // Complete the current line
228         pw.println();
229         thrown.printStackTrace(pw);
230     }
231 
232     /**
233      * Print a message to the provided {@link PrintStream}.
234      *
235      * @param out The output.
236      * @param message The message.
237      * @see PrintStream#println(String)
238      */
239     private static void println(PrintStream out, String message) {
240         out.println(message);
241     }
242 
243     /**
244      * Print a message to the provided {@link PrintStream}.
245      *
246      * @param out The output.
247      * @param format The format.
248      * @param args The arguments.
249      * @see PrintStream#printf(String, Object...)
250      */
251     private static void printf(PrintStream out, String format, Object... args) {
252         // Ensure a new line is added
253         out.printf(format + "%n", args);
254     }
255 }