~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/core/common/WLogEntry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
10
// OpenWalnut is free software: you can redistribute it and/or modify
 
11
// it under the terms of the GNU Lesser General Public License as published by
 
12
// the Free Software Foundation, either version 3 of the License, or
 
13
// (at your option) any later version.
 
14
//
 
15
// OpenWalnut is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU Lesser General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU Lesser General Public License
 
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#include <string>
 
26
#include <sstream>
 
27
 
 
28
#include <boost/algorithm/string.hpp>
 
29
 
 
30
#include "WTerminalColor.h"
 
31
 
 
32
#include "WLogEntry.h"
 
33
 
 
34
LogLevel logLevelFromString( const std::string& str )
 
35
{
 
36
    // get lower-case version
 
37
    std::string strLower = str; // NOTE: this reserves the needed space
 
38
    std::transform( str.begin(), str.end(), strLower.begin(), tolower );
 
39
    if( !strLower.compare( "debug" ) )
 
40
    {
 
41
        return LL_DEBUG;
 
42
    }
 
43
    else if( !strLower.compare( "info" ) )
 
44
    {
 
45
        return LL_INFO;
 
46
    }
 
47
    else if( !strLower.compare( "warning" ) )
 
48
    {
 
49
        return LL_WARNING;
 
50
    }
 
51
    else if( !strLower.compare( "error" ) )
 
52
    {
 
53
        return LL_ERROR;
 
54
    }
 
55
    return LL_DEBUG;
 
56
}
 
57
 
 
58
WLogEntry::WLogEntry( std::string logTime, std::string message, LogLevel level, std::string source )
 
59
    : m_time( logTime ),
 
60
      m_message( message ),
 
61
      m_level( level ),
 
62
      m_source( source ),
 
63
      m_errorColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGRed ) ),
 
64
      m_infoColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGGreen ) ),
 
65
      m_debugColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGBlue ) ),
 
66
      m_warningColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGYellow ) ),
 
67
      m_sourceColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGMagenta ) ),
 
68
      m_timeColor( WTerminalColor( WTerminalColor::Bold, WTerminalColor::FGBlack ) ),
 
69
      m_messageColor( WTerminalColor() )
 
70
{
 
71
}
 
72
 
 
73
WLogEntry::~WLogEntry()
 
74
{
 
75
}
 
76
 
 
77
std::string WLogEntry::getLogString( std::string format, bool colors ) const
 
78
{
 
79
    std::string s = format;
 
80
 
 
81
    m_errorColor.setEnabled( colors );
 
82
    m_infoColor.setEnabled( colors );
 
83
    m_debugColor.setEnabled( colors );
 
84
    m_warningColor.setEnabled( colors );
 
85
    m_sourceColor.setEnabled( colors );
 
86
    m_timeColor.setEnabled( colors );
 
87
    m_messageColor.setEnabled( colors );
 
88
 
 
89
    boost::ireplace_first( s, "%t", m_timeColor + m_time + !m_timeColor );
 
90
 
 
91
    switch ( m_level )
 
92
    {
 
93
        case LL_DEBUG:
 
94
            boost::ireplace_first( s, "%l", m_debugColor + "DEBUG  " + !m_debugColor );
 
95
            break;
 
96
        case LL_INFO:
 
97
            boost::ireplace_first( s, "%l", m_infoColor + "INFO   " + !m_infoColor );
 
98
            break;
 
99
        case LL_WARNING:
 
100
            boost::ireplace_first( s, "%l", m_warningColor + "WARNING" + !m_warningColor );
 
101
            break;
 
102
        case LL_ERROR:
 
103
            boost::ireplace_first( s, "%l", m_errorColor + "ERROR  " + !m_errorColor );
 
104
            break;
 
105
        default:
 
106
            break;
 
107
    }
 
108
 
 
109
    boost::ireplace_first( s, "%m", m_messageColor + m_message + !m_messageColor );
 
110
 
 
111
    boost::ireplace_first( s, "%s", m_sourceColor + m_source + !m_sourceColor );
 
112
 
 
113
    return s;
 
114
}
 
115
 
 
116
LogLevel WLogEntry::getLogLevel() const
 
117
{
 
118
    return m_level;
 
119
}
 
120
 
 
121
std::string WLogEntry::getMessage() const
 
122
{
 
123
    return m_message;
 
124
}
 
125
 
 
126
std::string WLogEntry::getSource() const
 
127
{
 
128
    return m_source;
 
129
}
 
130
 
 
131
std::string WLogEntry::getTime() const
 
132
{
 
133
    return m_time;
 
134
}
 
135