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    package org.crsh.text.renderers;
020    
021    import org.crsh.text.Color;
022    import org.crsh.text.LineRenderer;
023    import org.crsh.text.Renderer;
024    import org.crsh.text.ui.LabelElement;
025    import org.crsh.util.BaseIterator;
026    
027    import java.util.Iterator;
028    import java.util.logging.Level;
029    import java.util.logging.LogRecord;
030    import java.util.logging.SimpleFormatter;
031    
032    /**
033     * A renderer for {@link java.util.logging.LogRecord} objects based on the {@link java.util.logging.SimpleFormatter}
034     * formatter.
035     *
036     * @author Julien Viet
037     */
038    public class LogRecordRenderer extends Renderer<LogRecord> {
039    
040      @Override
041      public Class<LogRecord> getType() {
042        return LogRecord.class;
043      }
044    
045      @Override
046      public LineRenderer renderer(final Iterator<LogRecord> stream) {
047        return LineRenderer.vertical(new Iterable<LineRenderer>() {
048          @Override
049          public Iterator<LineRenderer> iterator() {
050            return new BaseIterator<LineRenderer>() {
051              final SimpleFormatter formatter = new SimpleFormatter();
052              @Override
053              public boolean hasNext() {
054                return stream.hasNext();
055              }
056              @Override
057              public LineRenderer next() {
058                LogRecord record = stream.next();
059                String line = formatter.format(record);
060                Color color;
061                if (record.getLevel() == Level.SEVERE) {
062                  color = Color.red;
063                } else if (record.getLevel() == Level.WARNING) {
064                  color = Color.yellow;
065                } else if (record.getLevel() == Level.INFO) {
066                  color = Color.green;
067                } else {
068                  color = Color.blue;
069                }
070                return new LabelElement(line).style(color.fg()).renderer();
071              }
072            };
073          }
074        });
075      }
076    }