OW_FileAppender.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00035 #include "OW_config.h"
00036 #include "OW_FileAppender.hpp"
00037 #include "OW_Format.hpp"
00038 #include "OW_Logger.hpp"
00039 #include "OW_LogMessage.hpp"
00040 #include "OW_Mutex.hpp"
00041 #include "OW_MutexLock.hpp"
00042 #include "OW_FileSystem.hpp"
00043 
00044 #include <fstream>
00045 
00046 namespace OW_NAMESPACE
00047 {
00048 
00050 FileAppender::FileAppender(const StringArray& components,
00051    const StringArray& categories,
00052    const char* filename,
00053    const String& pattern,
00054    UInt64 maxFileSize,
00055    unsigned int maxBackupIndex,
00056    bool flushLog)
00057    : LogAppender(components, categories, pattern)
00058    , m_filename(filename)
00059    , m_maxFileSize(maxFileSize)
00060    , m_maxBackupIndex(maxBackupIndex)
00061    , m_flushLog(flushLog)
00062 {
00063    m_log.open(m_filename.c_str(), std::ios::out | std::ios::app);
00064    if (!m_log)
00065    {
00066       OW_THROW(LoggerException, Format("FileAppender: Unable to open file: %1", m_filename).toString().c_str() );
00067    }
00068 }
00069 
00071 FileAppender::~FileAppender()
00072 {
00073 }
00074 
00076 namespace
00077 {
00078    Mutex fileGuard;
00079 }
00080 void
00081 FileAppender::doProcessLogMessage(const String& formattedMessage, const LogMessage& message) const
00082 {
00083    MutexLock lock(fileGuard);
00084 
00085    // take into account external log rotators, if the file we have open no longer exists, then reopen it.
00086    if (!FileSystem::exists(m_filename.c_str()))
00087    {
00088       m_log.close();
00089       m_log.open(m_filename.c_str(), std::ios::out | std::ios::app);
00090    }
00091 
00092    if (!m_log)
00093    {
00094       // hmm, not much we can do here.  doProcessLogMessage can't throw.
00095       return;
00096    }
00097 
00098    m_log.write(formattedMessage.c_str(), formattedMessage.length());
00099    m_log << '\n';
00100 
00101    if (m_flushLog)
00102    {
00103       m_log.flush();
00104    }
00105 
00106    // handle log rotation
00107    if (m_maxFileSize != NO_MAX_LOG_SIZE && m_log.tellp() >= static_cast<std::streampos>(m_maxFileSize * 1024))
00108    {
00109       // since we can't throw an exception, or log any errors, it something fails here, we'll just return silently :-(
00110 
00111       // do the roll over
00112       m_log.close();
00113 
00114       if (m_maxBackupIndex > 0)
00115       {
00116          // delete the oldest file first - this may or may not exist, we try anyway.
00117          FileSystem::removeFile(m_filename + '.' + String(m_maxBackupIndex));
00118 
00119          // increment the numbers on all the files - some may exist or not, but try anyway.
00120          for (unsigned int i = m_maxBackupIndex - 1; i >= 1; --i)
00121          {
00122             FileSystem::renameFile(m_filename + '.' + String(i), m_filename + '.' + String(i + 1));
00123          }
00124 
00125          if (!FileSystem::renameFile(m_filename, m_filename + ".1"))
00126          {
00127             // if we can't rename it, avoid truncating it.
00128             return;
00129          }
00130       }
00131 
00132       // truncate the existing one
00133       m_log.open(m_filename.c_str(), std::ios_base::out | std::ios_base::trunc);
00134    }
00135 }
00136 
00138 const String FileAppender::STR_DEFAULT_MESSAGE_PATTERN("%d{%a %b %d %H:%M:%S %Y} [%t]: %m");
00139 
00140 } // end namespace OW_NAMESPACE
00141 
00142 
00143 
00144 

Generated on Thu Feb 9 08:47:59 2006 for openwbem by  doxygen 1.4.6