Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/log/Logger.javaGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 package edu.wpi.hri.log; 00035 00036 import java.io.PrintWriter; 00037 import java.io.StringWriter; 00038 00046 public abstract class Logger { 00047 00048 private final String prefix; 00049 protected final LoggerLevel maxLevel; 00050 00051 protected Logger(LoggerLevel level, Colors color, String name) { 00052 this.prefix = color.bashColor + "[" + name + "]: "; 00053 this.maxLevel = level; 00054 } 00055 00061 public void printThrowable(LoggerLevel level, Throwable e) { 00062 if (level.isBelow(this.maxLevel)) { 00063 StringWriter writer = new StringWriter(); 00064 PrintWriter stream = new PrintWriter(writer); 00065 e.printStackTrace(stream); 00066 this.error(Colors.RED_BG.bashColor + Colors.DEFAULT.bashColor 00067 + writer.toString()); 00068 } 00069 } 00070 00071 protected final String concat(Colors color, String str) { 00072 return this.prefix + Colors.DEFAULT.bashColor + color.bashColor + str; 00073 } 00074 00084 public abstract void debug(LoggerLevel level, String str); 00085 00093 public abstract void info(String str); 00094 00102 public abstract void warn(String str); 00103 00111 public abstract void error(String str); 00112 00122 public abstract void fatal(String str, int exitCode); 00123 00134 public final Logger sub(Colors color, String name) { 00135 return new SubLogger(this, color, name); 00136 } 00137 00152 public static Logger createBase(LoggerLevel maxLevel, String type, 00153 Colors color, String name) { 00154 if (type.toLowerCase().equals("ros")) 00155 return new ROSLogger(maxLevel, color, name); 00156 else if (type.toLowerCase().equals("system")) 00157 return new SystemLogger(maxLevel, color, name); 00158 else 00159 return null; 00160 } 00161 00162 private static int count = 0; 00163 00169 public static Logger createDefault() { 00170 return new SystemLogger(LoggerLevel.ALL, Colors.GREEN_FG, "DEFAULT " 00171 + count++); 00172 } 00173 00179 public enum Colors { 00180 // simple colors 00182 DEFAULT("\033[0m"), 00184 RED_FG("\033[31m"), 00186 GREEN_FG("\033[32m"), 00188 YELLOW_FG("\033[33m"), 00190 BLUE_FG("\033[34m"), 00192 MAGENTA_FG("\033[35m"), 00194 CYAN_FG("\033[36m"), 00196 RED_BG("\033[41m"), 00198 GREEN_BG("\033[42m"), 00200 YELLOW_BG("\033[43m"), 00202 BLUE_BG("\033[44m"), 00204 MAGENTA_BG("\033[45m"), 00206 CYAN_BG("\033[46m"), 00207 // compound/other colors for generation 00209 GENER(GREEN_FG.bashColor), 00211 SPINNER(BLUE_FG.bashColor), 00213 EXEC_TURN_FRAG(GREEN_FG.bashColor), 00215 BML(RED_FG.bashColor), 00217 XML(YELLOW_FG.bashColor), 00219 POLICY(MAGENTA_FG.bashColor), 00221 GAZE_KNOWLEDGE(DEFAULT.bashColor + BLUE_BG.bashColor), 00223 REFERENCE(DEFAULT.bashColor + MAGENTA_BG.bashColor), 00225 SITUATION_KNOWLEDGE(DEFAULT.bashColor + RED_BG.bashColor), 00226 // other colors for the realizer 00228 REALIZER(BLUE_FG.bashColor), 00230 BEHAVIOR(RED_FG.bashColor), 00232 BEHAV_LIST(YELLOW_FG.bashColor), 00234 EXECUTOR(GREEN_FG.bashColor), 00236 CONTROL(CYAN_FG.bashColor), 00238 SCHEDULE(MAGENTA_FG.bashColor), 00240 PETRI_NET(DEFAULT.bashColor); 00241 00242 final String bashColor; 00243 00244 private Colors(String str) { 00245 this.bashColor = str; 00246 } 00247 } 00248 00257 public static enum LoggerLevel { 00259 ALWAYS(0), 00261 INIT(10), 00263 POLICY_EXECUTION(20), 00265 BML_EXECUTION(30), 00267 BEHAVIOR(40), 00269 SCHEDULE(50), 00271 MATH(60), 00273 IO(70), 00275 MASTER(80), 00280 ALL(Integer.MAX_VALUE); 00281 00282 private final int level; 00283 00284 private LoggerLevel(int i) { 00285 this.level = i; 00286 } 00287 00298 public boolean isBelow(LoggerLevel max) { 00299 return (this.level <= max.level); 00300 } 00301 } 00302 } |