WLogger.cpp 3.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#include <iostream>
#include <string>

#include "boost/date_time/posix_time/posix_time.hpp"
#include <boost/filesystem/fstream.hpp>

#include "WLogger.h"


WLogger::WLogger( std::string fileName, LogLevel level ):
    WThreadedRunner(),
    m_LogLevel( level ),

    m_STDOUTLevel( level ),
    m_STDERRLevel( LL_ERROR ),
    m_LogFileLevel( level ),
schurade's avatar
schurade committed
41
    m_LogFileName( fileName ),
42
    m_QueueMutex()
43
{
schurade's avatar
schurade committed
44 45 46 47
    addLogMessage( "Initalizing Logger", "Logger", LL_DEBUG );
    addLogMessage( "===============================================================================", "Logger", LL_INFO );
    addLogMessage( "=                          Starting Log Session                               =", "Logger", LL_INFO );
    addLogMessage( "===============================================================================", "Logger", LL_INFO );
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

WLogger::~WLogger()
{
}


void WLogger::setLogLevel( LogLevel level )
{
    m_LogLevel = level;
}


void WLogger::setSTDOUTLevel( LogLevel level )
{
    m_STDOUTLevel = level;
}


void WLogger::setSTDERRLevel( LogLevel level )
{
    m_STDERRLevel = level;
}


void WLogger::setLogFileLevel( LogLevel level )
{
    m_LogFileLevel = level;
}


void WLogger::setLogFileName( std::string fileName )
{
    /**
     * TODO (schurade) check if it's a valid filename
     */
    m_LogFileName = fileName;
}


void WLogger::addLogMessage( std::string message, std::string source, LogLevel level )
{
90
    if ( m_LogLevel > level || m_FinishRequested )
91 92 93 94 95 96 97
    {
        return;
    }

    boost::posix_time::ptime t( boost::posix_time::second_clock::local_time() );
    std::string timeString( to_simple_string( t ) );
    WLogEntry entry( timeString, message, level, source );
schurade's avatar
schurade committed
98 99

    boost::mutex::scoped_lock l( m_QueueMutex );
100 101 102 103 104 105
    m_LogQueue.push( entry );
}


void WLogger::processQueue()
{
schurade's avatar
schurade committed
106 107
    boost::mutex::scoped_lock l( m_QueueMutex );

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    while ( !m_LogQueue.empty() )
    {
        WLogEntry entry = m_LogQueue.front();
        m_LogQueue.pop();

        m_SessionLog.push_back( entry );

        if ( entry.getLogLevel() >= m_STDOUTLevel )
        {
            std::cout << entry.getLogString();
        }

        if ( entry.getLogLevel() >= m_STDERRLevel )
        {
            std::cerr << entry.getLogString();
        }

        if ( entry.getLogLevel() >= m_LogFileLevel )
        {
            boost::filesystem::path p( "walnut.log" );
            boost::filesystem::ofstream ofs( p, boost::filesystem::ofstream::app );
            ofs << entry.getLogString();
        }
    }
}
schurade's avatar
schurade committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146


void WLogger::threadMain()
{
    // Since the modules run in a separate thread: such loops are possible
    while ( !m_FinishRequested )
    {
        processQueue();
        // do fancy stuff
        sleep( 1 );
    }

    // clean up stuff
}