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 41 42 43 44 45 46 47 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| /*
** logger.hpp
*/
#pragma once
#include <RWL/Cpp_Tools/class_templates.hpp>
#include <RWL/Cpp_Tools/debug.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <map>
#include <memory>
#define LOG_LOCATION L" [" << __WFILE__ << L", line " << __LINE__ << L"]"
namespace rwl
{
class Logger :
private Non_copyable
{
public :
enum class Level
{
trace,
debug,
info,
warning,
error,
exception,
fatal
};
Logger( const std::wstring& name = L"" );
~Logger( void );
std::wostringstream& log( Level level );
private :
std::wstring m_name;
std::wostringstream m_logbuffer;
Level m_level;
public :
static void enable( bool enable, const std::wstring& name = L"" );
static void set_level( Level level, const std::wstring& name = L"" );
static void set_logfile( const std::wstring& file_path, const std::wstring& name = L"" );
private:
class Logger_data
{
public :
Logger_data( void );
Logger_data( const Logger_data& logger_data );
~Logger_data( void );
Logger_data& operator=( const Logger_data& logger_data );
bool enable;
Level level;
std::shared_ptr< std::wofstream > logfile;
};
typedef std::map< const std::wstring, Logger_data > Logger_data_map;
static Logger_data_map m_loggers_data;
};
}
/*
** logger.cpp
*/
#include <RWL/Cpp_Tools/logger.hpp>
#include <locale>
#include <codecvt>
namespace rwl
{
Logger::Logger_data::Logger_data( void ) :
enable( true ),
level( Logger::Level::trace ),
logfile( std::make_shared< std::wofstream >() )
{
}
Logger::Logger_data::Logger_data( const Logger::Logger_data& logger_data ) :
enable( logger_data.enable ),
level( logger_data.level ),
logfile( logger_data.logfile )
{
}
Logger::Logger_data::~Logger_data( void )
{
logfile->close();
}
Logger::Logger_data& Logger::Logger_data::operator=( const Logger_data& logger_data )
{
enable = logger_data.enable;
level =logger_data.level;
logfile = logger_data.logfile;
return ( *this );
}
Logger::Logger_data_map Logger::m_loggers_data;
Logger::Logger( const std::wstring& name /*= L"" */ ) :
m_name( name )
{
}
Logger::~Logger( void )
{
Logger_data& logger_data = m_loggers_data[ m_name ];
std::wstring header;
if ( logger_data.enable && m_level >= logger_data.level )
{
switch ( m_level )
{
case ( Logger::Level::trace ) :
header = L"[Trace]";
break;
case ( Logger::Level::debug ) :
header = L"[Debug]";
break;
case ( Logger::Level::info ) :
header = L"[Info]";
break;
case ( Logger::Level::warning ) :
header = L"[Warning]";
break;
case ( Logger::Level::error ) :
header = L"[Error]";
break;
case ( Logger::Level::exception ) :
header = L"[Exception]";
break;
case ( Logger::Level::fatal ) :
header = L"[Fatal]";
break;
}
if ( m_name != L"" )
{
header += L"[";
header += m_name;
header += L"]";
}
header+= L": ";
if ( m_level <= Logger::Level::info )
std::wcout << header << m_logbuffer.str() << std::flush;
else if ( m_level >= Logger::Level::warning )
std::wcerr << header << m_logbuffer.str() << std::flush;
if ( logger_data.logfile )
if ( logger_data.logfile->is_open() && logger_data.logfile->good() )
*logger_data.logfile << header << m_logbuffer.str() << std::flush;
}
}
std::wostringstream& Logger::log( Level level )
{
m_level = level;
return ( m_logbuffer );
}
void Logger::enable( bool enable, const std::wstring& name /*= L"" */ )
{
m_loggers_data[ name ].enable = enable;
}
void Logger::set_level( Level level, const std::wstring& name /*= L"" */ )
{
m_loggers_data[ name ].level = level;
}
void Logger::set_logfile( const std::wstring& file_path, const std::wstring& name /*= L"" */ )
{
if ( m_loggers_data[ name ].logfile )
{
std::wofstream& logfile = *m_loggers_data[ name ].logfile;
logfile.open( file_path, std::ios::trunc | std::ios::out );
if ( logfile.is_open() && logfile.good() )
logfile.imbue( std::locale( logfile.getloc(), new ( std::codecvt_utf8_utf16< wchar_t > ) ) );
else
logfile.close();
}
}
} |
Partager