OpenWalnut  1.5.0dev
WLoggerWrapper.cpp
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 <fstream>
26 #include <iostream>
27 #include <memory>
28 #include <string>
29 
30 #include "WLoggerWrapper.h"
31 #include "core/common/WAssert.h"
32 #include "core/common/WLogStream.h"
33 #include "core/common/WLogger.h"
34 
36 {
37 }
38 
40 {
41  WAssert( logger, "Null pointer" );
42  m_logger = logger;
43 }
44 
46 {
48 }
49 
51 {
52  m_logger->removeStream( m_fileStreamList[ i ].m_WLogStream );
53  m_fileStreamList.erase( m_fileStreamList.begin() + i );
54 }
55 
56 bool WLoggerWrapper::addFileStream( std::string filename )
57 {
58  std::shared_ptr< std::ofstream > fileStream( new std::ofstream( filename.c_str() ) );
59  if( !fileStream )
60  {
61  return false;
62  }
63  FileStreamEntry newEntry;
64  newEntry.m_filename = filename;
65  newEntry.m_fileStream = fileStream;
66  newEntry.m_WLogStream = WLogStream::SharedPtr( new WLogStream( *fileStream ) );
67  m_fileStreamList.push_back( newEntry );
68  m_logger->addStream( newEntry.m_WLogStream );
69  return true;
70 }
71 
72 bool WLoggerWrapper::removeFileStream( std::string filename )
73 {
74  for( size_t i = 0; i < m_fileStreamList.size(); ++i )
75  {
76  if( filename == m_fileStreamList[ i ].m_filename )
77  {
79  return true;
80  }
81  }
82  return false;
83 }
84 
86 {
87  for( size_t i = 0; i < m_fileStreamList.size(); ++i )
88  {
90  }
91 }
92 
93 void WLoggerWrapper::error( std::string const& location, std::string const& message )
94 {
95  m_logger->addLogMessage( message, location, LL_ERROR );
96 }
97 
98 void WLoggerWrapper::warning( std::string const& location, std::string const& message )
99 {
100  m_logger->addLogMessage( message, location, LL_WARNING );
101 }
102 
103 void WLoggerWrapper::info( std::string const& location, std::string const& message )
104 {
105  m_logger->addLogMessage( message, location, LL_INFO );
106 }
107 
108 void WLoggerWrapper::debug( std::string const& location, std::string const& message )
109 {
110  m_logger->addLogMessage( message, location, LL_DEBUG );
111 }
Class implementing a capsule for an output stream and the needed level and format information.
Definition: WLogStream.h:39
std::shared_ptr< WLogStream > SharedPtr
shared pointer type
Definition: WLogStream.h:41
void warning(std::string const &location, std::string const &message)
Output a warning to the logs.
void debug(std::string const &location, std::string const &message)
Output a debug message.
void removeFileStreamNumber(size_t i)
Helper function that removes the file stream with the given index.
void removeAllFileStreams()
Remove all files to which the logger writes (and which were added by this wrapper).
std::vector< FileStreamEntry > m_fileStreamList
List of file streams.
void error(std::string const &location, std::string const &message)
Output an error to the logs.
WLoggerWrapper()
Constructor.
bool addFileStream(std::string filename)
Add a file to which the logger output will be written.
~WLoggerWrapper()
Destructor.
bool removeFileStream(std::string filename)
Remove a file to which the logger writes.
void info(std::string const &location, std::string const &message)
Output information to the logs.
WLogger * m_logger
A pointer to the logger.
This class defines the interface for adding logs and managing several output streams for them.
Definition: WLogger.h:47
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
void removeStream(WLogStream::SharedPtr s)
Remove the given stream.
Definition: WLogger.cpp:121
void addStream(WLogStream::SharedPtr s)
Adds a new stream to the logger.
Definition: WLogger.cpp:116
A helper class for storing information about file streams that we added to the logger.
std::string m_filename
The name of the log file.
WLogStream::SharedPtr m_WLogStream
The logstream instance.
std::shared_ptr< std::ofstream > m_fileStream
The actual stream.