001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://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        this(locale, (Object)channel);
058
059    }
060
061    /**
062     * Constructs a new report using the provided locale for the output language,
063     * using the provided Java class for the log channel.<p>
064     *
065     * @param locale the locale to use for the report output messages
066     * @param channel the log channel (usually a string with the package name, or a class)
067     */
068    public CmsLogReport(Locale locale, Object channel) {
069
070        init(locale, null);
071        m_buffer = new StringBuffer();
072        if (channel == null) {
073            channel = CmsLogReport.class;
074        }
075        m_channel = channel;
076    }
077
078    /**
079     * @see org.opencms.report.I_CmsReport#getReportUpdate()
080     */
081    public String getReportUpdate() {
082
083        return "";
084    }
085
086    /**
087     * @see org.opencms.report.A_CmsReport#print(java.lang.String, int)
088     */
089    @Override
090    public synchronized void print(String value, int format) {
091
092        switch (format) {
093            case FORMAT_HEADLINE:
094                m_buffer.append("[ ");
095                m_buffer.append(value);
096                m_buffer.append(" ]");
097                break;
098            case FORMAT_WARNING:
099                m_buffer.append("!!! ");
100                m_buffer.append(value);
101                m_buffer.append(" !!!");
102                addWarning(value);
103                break;
104            case FORMAT_ERROR:
105                m_buffer.append("!!! ");
106                m_buffer.append(value);
107                m_buffer.append(" !!!");
108                addError(value);
109                break;
110            case FORMAT_NOTE:
111            case FORMAT_OK:
112            case FORMAT_DEFAULT:
113            default:
114                m_buffer.append(value);
115        }
116        setLastEntryTime(System.currentTimeMillis());
117    }
118
119    /**
120     * @see org.opencms.report.I_CmsReport#println()
121     */
122    public synchronized void println() {
123
124        if (CmsLog.getLog(m_channel).isInfoEnabled()) {
125            CmsLog.getLog(m_channel).info(m_buffer.toString());
126        }
127        m_buffer = new StringBuffer();
128        setLastEntryTime(System.currentTimeMillis());
129    }
130
131    /**
132     * @see org.opencms.report.I_CmsReport#println(java.lang.Throwable)
133     */
134    public synchronized void println(Throwable t) {
135
136        if (CmsLog.getLog(m_channel).isInfoEnabled()) {
137            StringBuffer message = new StringBuffer();
138            message.append(getMessages().key(Messages.RPT_EXCEPTION_0));
139            message.append(t.getMessage());
140            m_buffer.append(message);
141            addError(message.toString());
142            CmsLog.getLog(m_channel).info(m_buffer.toString(), t);
143        }
144        m_buffer = new StringBuffer();
145        setLastEntryTime(System.currentTimeMillis());
146    }
147}