001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.text.renderers;
021    
022    import org.crsh.text.Color;
023    import org.crsh.text.Decoration;
024    import org.crsh.text.LineRenderer;
025    import org.crsh.text.Renderer;
026    import org.crsh.text.ui.LabelElement;
027    import org.crsh.text.ui.RowElement;
028    import org.crsh.text.ui.TableElement;
029    
030    import java.util.Iterator;
031    import java.util.logging.Level;
032    import java.util.logging.Logger;
033    
034    public class LoggerRenderer extends Renderer<Logger> {
035    
036      @Override
037      public Class<Logger> getType() {
038        return Logger.class;
039      }
040    
041      @Override
042      public LineRenderer renderer(Iterator<Logger> stream) {
043        TableElement table = new TableElement();
044    
045        // Header
046        table.add(new RowElement().style(Decoration.bold.fg(Color.black).bg(Color.white)).add("NAME", "LEVEL"));
047    
048        //
049        while (stream.hasNext()) {
050          Logger logger = stream.next();
051    
052          // Determine level
053          String level;
054          if (logger.isLoggable(Level.FINER)) {
055            level = "TRACE";
056          } else if (logger.isLoggable(Level.FINE)) {
057            level = "DEBUG";
058          } else if (logger.isLoggable(Level.INFO)) {
059            level = "INFO";
060          } else if (logger.isLoggable(Level.WARNING)) {
061            level = "WARN";
062          }  else if (logger.isLoggable(Level.SEVERE)) {
063            level = "ERROR";
064          } else {
065            level = "UNKNOWN";
066          }
067    
068          //
069          table.row(logger.getName(), level);
070        }
071    
072        //
073        return table.renderer();
074      }
075    }