10 #pragma warning(disable : 4100) // unreferenced formal parameter 11 #pragma warning(disable : 4103) 12 #pragma warning(disable : 4512) // assignment operator could not be generated 13 #pragma warning(disable : 4701) // potentially uninitialized local variable 14 #pragma warning(disable : 4244) 28 #include <boost/filesystem.hpp> 29 #include <boost/log/attributes.hpp> 30 #include <boost/log/core/core.hpp> 31 #include <boost/log/expressions.hpp> 32 #include <boost/log/sources/global_logger_storage.hpp> 33 #include <boost/log/sinks/sync_frontend.hpp> 34 #include <boost/log/sinks/text_ostream_backend.hpp> 35 #include <boost/log/sources/record_ostream.hpp> 36 #include <boost/log/sources/severity_logger.hpp> 37 #include <boost/log/support/date_time.hpp> 38 #include <boost/log/utility/setup/common_attributes.hpp> 39 #include <boost/log/utility/manipulators/add_value.hpp> 49 namespace bfs = boost::filesystem;
50 namespace expr = boost::log::expressions;
51 namespace sinks = boost::log::sinks;
58 #if _WIN32 || _WIN64 // WIN 75 template <
typename CharT,
typename TraitsT>
76 inline std::basic_ostream<CharT, TraitsT>& operator<<(std::basic_ostream<CharT, TraitsT>& strm,
79 static const char*
const str[] = {
" info",
"warning",
" error",
" debug"};
80 if (static_cast<std::size_t>(lvl) < (
sizeof(str) /
sizeof(*str)))
83 strm << static_cast<int>(lvl);
93 boost::log::sources::severity_logger_mt<xmlog::MessageTypeEnum>)
95 boost::log::sources::severity_logger_mt<xmlog::MessageTypeEnum> lg;
97 lg.add_attribute(
"TimeStamp", boost::log::attributes::local_clock());
98 lg.add_attribute(
"Process", boost::log::attributes::current_process_name());
99 lg.add_attribute(
"Scope", boost::log::attributes::named_scope());
125 void StackedErrToStream(std::ostream& a_os);
144 boost::log::formatter file_fmt = expr::stream <<
"[" << severity <<
"]" 146 << expr::format_date_time<boost::posix_time::ptime>(
147 "TimeStamp",
"%Y-%m-%d %H:%M:%S")
149 <<
"[" << expr::attr<std::string>(
"Process") <<
":" 150 << expr::attr<std::string>(
"FileName") <<
":" 151 << expr::attr<int>(
"LineNumber") <<
"]" 152 <<
": " << expr::smessage;
156 boost::shared_ptr<sinks::text_ostream_backend> backend =
157 boost::make_shared<sinks::text_ostream_backend>();
159 backend->add_stream(boost::shared_ptr<std::ostream>(
new std::ofstream(
XmLog::LogFilename())));
162 backend->auto_flush(
true);
166 typedef sinks::synchronous_sink<sinks::text_ostream_backend> sink_t;
167 boost::shared_ptr<sink_t> sink(
new sink_t(backend));
168 sink->set_formatter(file_fmt);
169 boost::log::core::get()->add_sink(sink);
172 boost::log::add_common_attributes();
193 std::string a_message)
197 m->m_firstRun =
false;
204 BOOST_LOG_SEV(xms_global_log::get(), a_level)
205 << boost::log::add_value(
"FileName", a_file) << boost::log::add_value(
"LineNumber", a_line)
210 m->m_stackedMessages.push_back(std::make_pair(a_level, a_message));
211 BOOST_LOG_SEV(xms_global_log::get(), a_level)
212 << boost::log::add_value(
"FileName", a_file) << boost::log::add_value(
"LineNumber", a_line)
222 return (
int)
m->m_stackedMessages.size();
230 std::stringstream ss;
231 m->StackedErrToStream(ss);
241 m->m_stackedMessages.clear();
253 return fg_logFilenameCallback;
270 static std::string fg_logPath;
271 if (fg_logPath.empty())
273 bfs::path p = bfs::temp_directory_path() / bfs::unique_path();
274 fg_logPath = p.string() +
"debug.log";
285 if (m_stackedMessages.size() > 0)
287 for (
auto it = m_stackedMessages.begin(); it != m_stackedMessages.end(); it++)
289 a_os <<
"---" << it->second <<
"\n" 292 m_stackedMessages.clear();
318 t.seekg(0, std::ios::end);
319 str.reserve((
int)t.tellg());
320 t.seekg(0, std::ios::beg);
322 str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
323 TS_ASSERT(str.find(
" debug]") != std::string::npos);
324 TS_ASSERT(str.find(
"Start Log") != std::string::npos);
325 TS_ASSERT(str.find(
"Debug Log Test") != std::string::npos);
337 t.seekg(0, std::ios::end);
338 str.reserve((
int)t.tellg());
339 t.seekg(0, std::ios::beg);
341 str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
343 TS_ASSERT(str.find(
" debug]") != std::string::npos);
344 TS_ASSERT(str.find(
"Start Log") != std::string::npos);
345 TS_ASSERT(str.find(
"Stackable Log Test") != std::string::npos);
#define XM_LOG(A, B)
Log message which can have a log type (Debug, Stackable, Gui).
std::vector< std::pair< xmlog::MessageTypeEnum, std::string > > MessageStack
Container type used to store log messages.
Details of internal operation.
int ErrCount()
Returns the current count of the error stack.
MessageStack GetAndClearStack()
Returns a copy of the error stack before clearing it.
void iTest_XM_LOG_gui()
Run GUI log test.
MessageTypeEnum
Log level for XM_LOG.
void testAll()
Test XM_LOG.
static XmLogFilenameCallback & LogFilenameCallback()
Set the callback that returns the log filename.
std::string GetAndClearStackStr()
Clears the error stack and returns its contents as a string.
Base class for classes that follow the singleton pattern.
void iTest_XM_LOG_stackable()
Run stackable log test.
boost::scoped_ptr< Impl > m
Implementation pointer.
void Log(const char *const a_file, int a_line, xmlog::MessageTypeEnum a_level, std::string a_message)
Logs.
BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(xms_global_log, boost::log::sources::severity_logger_mt< xmlog::MessageTypeEnum >)
Used for convenience to declare a global log object.
MessageStack m_stackedMessages
Stack of messages that can be shown at a later time.
void StackedErrToStream(std::ostream &a_os)
Sends error stack to passed stream. Formats for dialog box.
XmLog()
Constructor hidden and not implemented.
boost::function< std::string()> XmLogFilenameCallback
Callback to return the name of the log file.
static std::string LogFilename()
Return the name of the log file.
Informational message for the user.
Routines for creating and writing to logs and stacking errors.
void iTest_XM_LOG_debug()
Run debug log test.
static XmLogFilenameCallback fg_logFilenameCallback
Callback to return the name of the log file.
Critical error message for the user.
bool m_firstRun
Used to setup log file first time something is logged.