001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
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 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.report;
029
030import org.opencms.main.CmsLog;
031
032import java.util.Locale;
033
034/**
035 * Report class used for the logfile.<p>
036 *
037 * This prints all messages in the logfile at INFO level.<p>
038 *
039 * @since 6.0.0
040 */
041public class CmsLogReport extends A_CmsReport {
042
043    /** The buffer to write the log messages to. */
044    private StringBuffer m_buffer;
045
046    /** The class name to use for the logger. */
047    private Object m_channel;
048
049    /**
050     * Constructs a new report using the provided locale for the output language,
051     * using the provided Java class for the log channel.<p>
052     *
053     * @param locale the locale to use for the report output messages
054     * @param channel the log channel
055     */
056    public CmsLogReport(Locale locale, Class<?> channel) {
057
058        this(locale, (Object)channel);
059
060    }
061
062    /**
063     * Constructs a new report using the provided locale for the output language,
064     * using the provided Java class for the log channel.<p>
065     *
066     * @param locale the locale to use for the report output messages
067     * @param channel the log channel (usually a string with the package name, or a class)
068     */
069    public CmsLogReport(Locale locale, Object channel) {
070
071        init(locale, null);
072        m_buffer = new StringBuffer();
073        if (channel == null) {
074            channel = CmsLogReport.class;
075        }
076        m_channel = channel;
077    }
078
079    /**
080     * @see org.opencms.report.I_CmsReport#getReportUpdate()
081     */
082    public String getReportUpdate() {
083
084        return "";
085    }
086
087    /**
088     * @see org.opencms.report.A_CmsReport#print(java.lang.String, int)
089     */
090    @Override
091    public synchronized void print(String value, int format) {
092
093        switch (format) {
094            case FORMAT_HEADLINE:
095                m_buffer.append("[ ");
096                m_buffer.append(value);
097                m_buffer.append(" ]");
098                break;
099            case FORMAT_WARNING:
100                m_buffer.append("!!! ");
101                m_buffer.append(value);
102                m_buffer.append(" !!!");
103                addWarning(value);
104                break;
105            case FORMAT_ERROR:
106                m_buffer.append("!!! ");
107                m_buffer.append(value);
108                m_buffer.append(" !!!");
109                addError(value);
110                break;
111            case FORMAT_NOTE:
112            case FORMAT_OK:
113            case FORMAT_DEFAULT:
114            default:
115                m_buffer.append(value);
116        }
117        setLastEntryTime(System.currentTimeMillis());
118    }
119
120    /**
121     * @see org.opencms.report.I_CmsReport#println()
122     */
123    public synchronized void println() {
124
125        if (CmsLog.getLog(m_channel).isInfoEnabled()) {
126            CmsLog.getLog(m_channel).info(m_buffer.toString());
127        }
128        m_buffer = new StringBuffer();
129        setLastEntryTime(System.currentTimeMillis());
130    }
131
132    /**
133     * @see org.opencms.report.I_CmsReport#println(java.lang.Throwable)
134     */
135    public synchronized void println(Throwable t) {
136
137        if (CmsLog.getLog(m_channel).isInfoEnabled()) {
138            StringBuffer message = new StringBuffer();
139            message.append(getMessages().key(Messages.RPT_EXCEPTION_0));
140            message.append(t.getMessage());
141            m_buffer.append(message);
142            addError(message.toString());
143            CmsLog.getLog(m_channel).info(m_buffer.toString(), t);
144        }
145        m_buffer = new StringBuffer();
146        setLastEntryTime(System.currentTimeMillis());
147    }
148}