RosoutLogger.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.node;
00018 
00019 import org.apache.commons.logging.Log;
00020 import org.apache.commons.logging.LogFactory;
00021 import org.ros.Topics;
00022 import org.ros.node.topic.Publisher;
00023 
00024 import java.io.PrintWriter;
00025 import java.io.StringWriter;
00026 
00033 class RosoutLogger implements Log {
00034 
00035   private final DefaultNode defaultNode;
00036   private final Publisher<rosgraph_msgs.Log> publisher;
00037   private final Log log;
00038 
00039   public RosoutLogger(DefaultNode defaultNode) {
00040     this.defaultNode = defaultNode;
00041     publisher = defaultNode.newPublisher(Topics.ROSOUT, rosgraph_msgs.Log._TYPE);
00042     log = LogFactory.getLog(defaultNode.getName().toString());
00043   }
00044 
00045   public Publisher<rosgraph_msgs.Log> getPublisher() {
00046     return publisher;
00047   }
00048 
00049   private void publish(byte level, Object message, Throwable throwable) {
00050     StringWriter stringWriter = new StringWriter();
00051     PrintWriter printWriter = new PrintWriter(stringWriter);
00052     throwable.printStackTrace(printWriter);
00053     publish(level, message.toString() + '\n' + stringWriter.toString());
00054   }
00055 
00056   private void publish(byte level, Object message) {
00057     rosgraph_msgs.Log logMessage = publisher.newMessage();
00058     logMessage.getHeader().setStamp(defaultNode.getCurrentTime());
00059     logMessage.setLevel(level);
00060     logMessage.setName(defaultNode.getName().toString());
00061     logMessage.setMsg(message.toString());
00062     // TODO(damonkohler): Should update the topics field with a list of all
00063     // published and subscribed topics for the node that created this logger.
00064     // This helps filter the rosoutconsole.
00065     publisher.publish(logMessage);
00066   }
00067 
00068   @Override
00069   public boolean isDebugEnabled() {
00070     return log.isDebugEnabled();
00071   }
00072 
00073   @Override
00074   public boolean isErrorEnabled() {
00075     return log.isErrorEnabled();
00076   }
00077 
00078   @Override
00079   public boolean isFatalEnabled() {
00080     return log.isFatalEnabled();
00081   }
00082 
00083   @Override
00084   public boolean isInfoEnabled() {
00085     return log.isInfoEnabled();
00086   }
00087 
00088   @Override
00089   public boolean isTraceEnabled() {
00090     return log.isTraceEnabled();
00091   }
00092 
00093   @Override
00094   public boolean isWarnEnabled() {
00095     return log.isWarnEnabled();
00096   }
00097 
00098   @Override
00099   public void trace(Object message) {
00100     log.trace(message);
00101     if (log.isTraceEnabled() && publisher != null) {
00102       publish(rosgraph_msgs.Log.DEBUG, message);
00103     }
00104   }
00105 
00106   @Override
00107   public void trace(Object message, Throwable t) {
00108     log.trace(message, t);
00109     if (log.isTraceEnabled() && publisher != null) {
00110       publish(rosgraph_msgs.Log.DEBUG, message, t);
00111     }
00112   }
00113 
00114   @Override
00115   public void debug(Object message) {
00116     log.debug(message);
00117     if (log.isDebugEnabled() && publisher != null) {
00118       publish(rosgraph_msgs.Log.DEBUG, message);
00119     }
00120   }
00121 
00122   @Override
00123   public void debug(Object message, Throwable t) {
00124     log.debug(message, t);
00125     if (log.isDebugEnabled() && publisher != null) {
00126       publish(rosgraph_msgs.Log.DEBUG, message, t);
00127     }
00128   }
00129 
00130   @Override
00131   public void info(Object message) {
00132     log.info(message);
00133     if (log.isInfoEnabled() && publisher != null) {
00134       publish(rosgraph_msgs.Log.INFO, message);
00135     }
00136   }
00137 
00138   @Override
00139   public void info(Object message, Throwable t) {
00140     log.info(message, t);
00141     if (log.isInfoEnabled() && publisher != null) {
00142       publish(rosgraph_msgs.Log.INFO, message, t);
00143     }
00144   }
00145 
00146   @Override
00147   public void warn(Object message) {
00148     log.warn(message);
00149     if (log.isWarnEnabled() && publisher != null) {
00150       publish(rosgraph_msgs.Log.WARN, message);
00151     }
00152   }
00153 
00154   @Override
00155   public void warn(Object message, Throwable t) {
00156     log.warn(message, t);
00157     if (log.isWarnEnabled() && publisher != null) {
00158       publish(rosgraph_msgs.Log.WARN, message, t);
00159     }
00160   }
00161 
00162   @Override
00163   public void error(Object message) {
00164     log.error(message);
00165     if (log.isErrorEnabled() && publisher != null) {
00166       publish(rosgraph_msgs.Log.ERROR, message);
00167     }
00168   }
00169 
00170   @Override
00171   public void error(Object message, Throwable t) {
00172     log.error(message, t);
00173     if (log.isErrorEnabled() && publisher != null) {
00174       publish(rosgraph_msgs.Log.ERROR, message, t);
00175     }
00176   }
00177 
00178   @Override
00179   public void fatal(Object message) {
00180     log.fatal(message);
00181     if (log.isFatalEnabled() && publisher != null) {
00182       publish(rosgraph_msgs.Log.FATAL, message);
00183     }
00184   }
00185 
00186   @Override
00187   public void fatal(Object message, Throwable t) {
00188     log.fatal(message, t);
00189     if (log.isFatalEnabled() && publisher != null) {
00190       publish(rosgraph_msgs.Log.FATAL, message, t);
00191     }
00192   }
00193 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49