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