I'm trying to write a class to wrap Boost logger in order to log widechar.
The problem is that if the Logger is defined as const member reference then it throws exception 'Could not convert character encoding', otherwise it works fine.
So the question is what's different if it is defined as const member reference?
Here's a simplest example:
#include "stdafx.h"
#include <iostream>
#include <boost/locale/generator.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/trivial.hpp>
namespace logging = boost::log;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
class Logger {
public:
static const Logger& GetInstance() {
static Logger logger;
return logger;
}
void Log(const std::wstring& message) const {
src::wseverity_logger< logging::trivial::severity_level > slg;
BOOST_LOG_SEV(slg, logging::trivial::error) << message;
}
private:
Logger() {
auto sink = logging::add_file_log(keywords::file_name = "sample.log");
std::locale loc = boost::locale::generator()("en_US.UTF-8");
sink->imbue(loc);
logging::add_common_attributes();
}
};
// *** The problem is introduced by line below ***
// *** If this line is removed, the logging doesn't throw exception. ***
const Logger& logger = Logger::GetInstance();
int main(int argc, char* argv[]) {
try {
Logger::GetInstance().Log(L"中文");
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire