001/*
002 * (C) Copyright 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 *     Thibaud Arguillere <targuillere@nuxeo.com>
016 *     Vladimir Pasquier <vpasquier@nuxeo.com>
017 */
018package org.nuxeo.automation.scripting.helper;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.ecm.automation.context.ContextHelper;
023import org.nuxeo.runtime.api.Framework;
024
025/**
026 * This helper writes in the log as browsers object console.log(), console.error(), console.warn() in Automation
027 * Scripting. Usage is with an uppercase "C". If logs info or trace are deactivated, Dev mode has to be set to display
028 * Automation scripting logs.
029 *
030 * @since 7.10
031 */
032public class Console implements ContextHelper {
033
034    private static final Log log = LogFactory.getLog(Console.class);
035
036    protected static boolean infoEnabled = log.isInfoEnabled();
037
038    protected static boolean traceEnabled = log.isTraceEnabled();
039
040    public void error(String inWhat) {
041        log.error(inWhat);
042    }
043
044    public void warn(String inWhat) {
045        log.warn(inWhat);
046    }
047
048    public void log(String inWhat) {
049        if (infoEnabled) {
050            log.info(inWhat);
051        } else if (Framework.isDevModeSet()) {
052            log.warn("[LOG] " + inWhat);
053        }
054    }
055
056    /*
057     * info() and log() are handled the same way
058     */
059    public void info(String inWhat) {
060        if (infoEnabled) {
061            log.info(inWhat);
062        } else if (Framework.isDevModeSet()) {
063            log.warn("[INFO] " + inWhat);
064        }
065    }
066
067    public void trace(String inWhat) {
068        if (traceEnabled) {
069            log.trace(inWhat);
070        } else if (Framework.isDevModeSet()) {
071            log.warn("[TRACE] " + inWhat);
072        }
073    }
074
075}