001/*
002 * (C) Copyright 2011-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Julien Carsique
018 *
019 */
020
021package org.nuxeo.launcher.gui.logs;
022
023import java.awt.Color;
024import java.util.Observable;
025import java.util.Observer;
026
027import org.nuxeo.launcher.gui.ColoredTextPane;
028
029/**
030 * @author jcarsique
031 * @since 5.4.2
032 */
033public class LogsHandler implements Observer {
034
035    private ColoredTextPane textArea;
036
037    /**
038     * @param textArea
039     */
040    public LogsHandler(ColoredTextPane textArea) {
041        this.textArea = textArea;
042    }
043
044    @Override
045    public void update(Observable obj, Object arg) {
046        if (arg instanceof String) {
047            notifyLogsView((String) arg);
048        }
049    }
050
051    /**
052     * @param logLine Line read from log file being sent to view
053     */
054    public void notifyLogsView(String logLine) {
055        Color color;
056        String[] split = logLine.split(" ", 4);
057        if (split.length < 3) {
058            color = new Color(234, 234, 234);
059        } else if ("INFO".equals(split[2])) {
060            color = new Color(234, 234, 234);
061        } else if ("DEBUG".equals(split[2])) {
062            color = new Color(108, 183, 242);
063        } else if ("WARN".equals(split[2])) {
064            color = new Color(234, 138, 2);
065        } else if ("ERROR".equals(split[2])) {
066            color = new Color(245, 0, 63);
067        } else {
068            color = new Color(234, 234, 234);
069        }
070        textArea.append(logLine, color);
071    }
072}