001/*
002 *   BshLog  -- A logging context that dumps the log to the supplied BeanShell Console.
003 *   
004 *   Copyright (C) 2014-2017, by Joseph A. Huwaldt.
005 *   All rights reserved.
006 *   
007 *   This library is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *   
012 *   This library is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 *   Lesser General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public License
018 *   along with this program; if not, write to the Free Software
019 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020 *   Or visit:  http://www.gnu.org/licenses/lgpl.html
021 */
022package geomss.ui;
023
024import bsh.util.JConsole;
025import static java.util.Objects.requireNonNull;
026import javolution.context.LogContext;
027
028/**
029*  A logging context that dumps the log to the supplied BeanShell Console.
030*
031*  <p>  Modified by:  Joseph A. Huwaldt   </p>
032*
033*  @author  Joseph A. Huwaldt   Date: March 5, 2014
034*  @version January 31, 2017
035*/
036public class BshLog extends LogContext {
037
038    private static final long serialVersionUID = 1L;
039    private final JConsole _bshConsole;
040    
041    public BshLog(JConsole bsh) {
042        _bshConsole = requireNonNull(bsh);
043    }
044    
045    @Override
046    public boolean isInfoLogged() {
047        return true;
048    }
049    
050    @Override
051    public void logInfo(CharSequence message) {
052        _bshConsole.print("[info] ");
053        _bshConsole.println(requireNonNull(message));
054    }
055    
056    @Override
057    public boolean isWarningLogged() {
058        return true;
059    }
060    
061    @Override
062    public void logWarning(CharSequence message) {
063        _bshConsole.print("[warning] ");
064        _bshConsole.println(requireNonNull(message));
065    }
066    
067    @Override
068    public boolean isErrorLogged() {
069        return true;
070    }
071    
072    @Override
073    public void logError(Throwable error, CharSequence message) {
074        _bshConsole.print("[error] ");
075        if (error != null) {
076            _bshConsole.print(error.getClass().getName());
077            _bshConsole.print(" - ");
078        }
079        String description = (message != null) ? message.toString()
080                : (error != null) ? error.getMessage() : "";
081        _bshConsole.println(description);
082        if (error != null) {
083            error.printStackTrace(_bshConsole.getErr());
084        }
085    }
086}