BowlerKernel
Log.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.common;
16 
17 import java.io.PrintStream;
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 
24 import com.neuronrobotics.sdk.config.SDKBuildInfo;
25 // TODO: Auto-generated Javadoc
31 public class Log {
32 
33 
34 
36  public static final int LOG =-1;
37 
39  public static final int INFO = 0;
40 
42  public static final int DEBUG = 1;
43 
45  public static final int WARNING = 2;
46 
48  public static final int ERROR = 3;
49 
51  private static Log instance;
52 
54  private Message m;
55  //private ArrayList<Message> messages = new ArrayList<Message>();
56 
58  private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SS");
59 
61  private int minprintlevel = WARNING;
62 
64  private boolean systemprint = false;
65 
67  private boolean debugprint = false;
68 
70  private static PrintStream outStream = System.out;
71 
73  private static PrintStream errStream = System.err;
74 
76  private boolean useColoredPrints=false;
77 
78 
82  private Log() {
83  // private for singleton pattern
85  }
86 
92  public static void error(String message) {
93  instance().add(message, ERROR);
94  }
95 
101  public static void warning(String message) {
102  instance().add(message, WARNING);
103  }
104 
110  public static void info(String message) {
111  instance().add(message, INFO);
112  }
113 
119  public static void log(String message) {
120  instance().add(message, LOG);
121  }
122 
128  public static void debug(String message) {
129  instance().add(message, DEBUG);
130  }
131 
137  public static void add(String message) {
138  instance().add(message, LOG);
139  }
140 
147  private void add(String message, int importance) {
148 
149  if( importance < minprintlevel) {
150  return;
151  }
152  if(m==null)
153  m = new Message(message, importance);
154  else{
155  m.init(message, importance);
156  }
157  //messages.add(m);
158 
159  if(isPrinting() && importance >= minprintlevel && systemprint) {
160  errStream.println(m);
161  if(errStream != System.err)
162  System.err.println(m);
163  }
164 
165  if(debugprint&& systemprint) {
166  outStream.println("# " + message);
167  if(outStream != System.out)
168  System.out.println(m);
169  }
170 
171 
172  }
173 
179  public static void enableSystemPrint(boolean systemprint) {
181  }
182 
187  public static void enableDebugPrint() {
188  Log.enableSystemPrint(true);
190  }
191 
198  public static void enableDebugPrint(boolean flag) {
199  Log.enableSystemPrint(flag);
201  }
202 
207  public static void enableInfoPrint() {
208  Log.enableSystemPrint(true);
210  }
211 
216  public static void enableWarningPrint() {
217  Log.enableSystemPrint(true);
219  }
220 
225  public static void enableErrorPrint() {
226  Log.enableSystemPrint(true);
228  }
229 
230 
236  public static void setMinimumPrintLevel(int level) {
237  Log.instance().minprintlevel = level;
238  }
239 
246  public static int getMinimumPrintLevel() {
247  return Log.instance().minprintlevel;
248  }
249 
255  public static Log instance() {
256  if(instance == null) {
257  instance = new Log();
258  }
259  return instance;
260  }
261 
268  public String getImportance(int importance) {
269  switch(importance) {
270  case INFO:
271  return "Info";
272  case WARNING:
273  return "Warning";
274  case ERROR:
275  return "Error";
276  case DEBUG:
277  return "Debug";
278  case LOG:
279  default:
280  return "Log";
281  }
282  }
289  public String getImportanceColor(int importance) {
290  if(isUseColoredPrints()){
291  switch(importance) {
292  case INFO:
293  return "\033[92m";// green
294  case WARNING:
295  return "\033[93m";// orange
296  case ERROR:
297  return "\033[31m";// red
298  case DEBUG:
299  return "\033[94m";// blue
300  case LOG:
301  default:
302  return "\033[92m";// green
303  }
304  }
305  return "";
306  }
307 
313  public static PrintStream getErrStream() {
314  return errStream;
315  }
316 
322  public static void setErrStream(PrintStream newerrStream) {
323  errStream = newerrStream;
324  }
325 
331  public static PrintStream getOutStream() {
332  return outStream;
333  }
334 
340  public static void setOutStream(PrintStream newoutStream) {
341  outStream = newoutStream;
342  }
343 
349  private class Message {
350 
352  private String message;
353 
355  private int importance;
356 
358  private Date datetime;
359 
361  private String callingClass;
362 
369  public Message(String message, int importance) {
371  }
372 
379  public void init(String message, int importance){
380  this.message = message;
381  this.importance = importance;
382  datetime = new Date();
383  try
384  {
385  throw new Exception("Who called me?");
386  }
387  catch( Exception e )
388  {
389  callingClass= e.getStackTrace()[3].getClassName()+":"+e.getStackTrace()[3].getMethodName();
390  }
391  }
392 
393  /* (non-Javadoc)
394  * @see java.lang.Object#toString()
395  */
396  public String toString() {
397  //return "\t\t\t\t[" + dateFormat.format(datetime) + "] " + " " + getImportance(importance) +" "+callingClass+ " :\n"+ message;
398  return getImportanceColor(importance)+"\t\t\t\t[" + dateFormat.format(datetime) + "] " + " " + getImportance(importance) +" "+callingClass+ " :\n"+ message+getColorNormalizationCode();
399  }
400  }
401 
407  private String getColorNormalizationCode(){
408  if(isUseColoredPrints())
409  return "\033[39m";
410  return "";
411  }
412 
418  public static boolean isUseColoredPrints() {
419  return instance().useColoredPrints;
420  }
421 
427  public static void setUseColoredPrints(boolean useColoredPrints) {
429  }
430 
436  public static boolean isPrinting() {
437  // TODO Auto-generated method stub
438  return instance().systemprint;
439  }
440 
441  public static void error(Throwable ex) {
442  StringWriter sw = new StringWriter();
443  PrintWriter pw = new PrintWriter(sw);
444  ex.printStackTrace(pw);
445  String sStackTrace = sw.toString(); // stack trace as a string
446  Log.error(sStackTrace);
447  }
448 
449 }
Message(String message, int importance)
Definition: Log.java:369
void init(String message, int importance)
Definition: Log.java:379
static void enableErrorPrint()
Definition: Log.java:225
static void info(String message)
Definition: Log.java:110
static void enableDebugPrint()
Definition: Log.java:187
static final int DEBUG
Definition: Log.java:42
static PrintStream errStream
Definition: Log.java:73
static void error(Throwable ex)
Definition: Log.java:441
static void error(String message)
Definition: Log.java:92
void add(String message, int importance)
Definition: Log.java:147
static void setMinimumPrintLevel(int level)
Definition: Log.java:236
static final int ERROR
Definition: Log.java:48
static void warning(String message)
Definition: Log.java:101
static final int LOG
Definition: Log.java:36
static void debug(String message)
Definition: Log.java:128
static void enableDebugPrint(boolean flag)
Definition: Log.java:198
static void enableWarningPrint()
Definition: Log.java:216
static void log(String message)
Definition: Log.java:119
String getImportance(int importance)
Definition: Log.java:268
static final int WARNING
Definition: Log.java:45
static PrintStream getOutStream()
Definition: Log.java:331
static void setOutStream(PrintStream newoutStream)
Definition: Log.java:340
static void enableInfoPrint()
Definition: Log.java:207
static PrintStream outStream
Definition: Log.java:70
static boolean isUseColoredPrints()
Definition: Log.java:418
static PrintStream getErrStream()
Definition: Log.java:313
static void add(String message)
Definition: Log.java:137
static void setUseColoredPrints(boolean useColoredPrints)
Definition: Log.java:427
static int getMinimumPrintLevel()
Definition: Log.java:246
static void enableSystemPrint(boolean systemprint)
Definition: Log.java:179
static void setErrStream(PrintStream newerrStream)
Definition: Log.java:322
static boolean isPrinting()
Definition: Log.java:436
static final int INFO
Definition: Log.java:39
String getImportanceColor(int importance)
Definition: Log.java:289