001/*
002 * (C) Copyright 2011-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library 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 * Contributors:
015 *     Julien Carsique
016 *
017 */
018
019package org.nuxeo.launcher.gui.logs;
020
021import java.awt.Color;
022import java.util.Observable;
023import java.util.Observer;
024
025import org.nuxeo.launcher.gui.ColoredTextPane;
026
027/**
028 * @author jcarsique
029 * @since 5.4.2
030 */
031public class LogsHandler implements Observer {
032
033    private ColoredTextPane textArea;
034
035    /**
036     * @param textArea
037     */
038    public LogsHandler(ColoredTextPane textArea) {
039        this.textArea = textArea;
040    }
041
042    @Override
043    public void update(Observable obj, Object arg) {
044        if (arg instanceof String) {
045            notifyLogsView((String) arg);
046        }
047    }
048
049    /**
050     * @param logLine Line read from log file being sent to view
051     */
052    public void notifyLogsView(String logLine) {
053        Color color;
054        String[] split = logLine.split(" ", 4);
055        if (split.length < 3) {
056            color = new Color(234, 234, 234);
057        } else if ("INFO".equals(split[2])) {
058            color = new Color(234, 234, 234);
059        } else if ("DEBUG".equals(split[2])) {
060            color = new Color(108, 183, 242);
061        } else if ("WARN".equals(split[2])) {
062            color = new Color(234, 138, 2);
063        } else if ("ERROR".equals(split[2])) {
064            color = new Color(245, 0, 63);
065        } else {
066            color = new Color(234, 234, 234);
067        }
068        textArea.append(logLine, color);
069    }
070}