From bcc5cc28c6dc5178e8f4fd0cc521034ae5def388 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:22:18 +0200 Subject: 5.10 --- zen/error_log.h | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'zen/error_log.h') diff --git a/zen/error_log.h b/zen/error_log.h index 401581d7..490bb1f4 100644 --- a/zen/error_log.h +++ b/zen/error_log.h @@ -25,30 +25,35 @@ enum MessageType TYPE_FATAL_ERROR = 0x8, }; -typedef Zbase MsgString; //std::wstring may employ small string optimization: we cannot accept bloating the "logEntries" memory block below (think 1 million entries) +typedef Zbase MsgString; //std::wstring may employ small string optimization: we cannot accept bloating the "ErrorLog::entries" memory block below (think 1 million items) struct LogEntry { time_t time; MessageType type; - MsgString message; + MsgString message; }; -MsgString formatMessage(const LogEntry& msg); +template +String formatMessage(const LogEntry& entry); class ErrorLog { public: - template - void logMsg(const String& message, MessageType type); + template //a wchar_t-based string! + void logMsg(const String& text, MessageType type); int getItemCount(int typeFilter = TYPE_INFO | TYPE_WARNING | TYPE_ERROR | TYPE_FATAL_ERROR) const; - const std::vector& getEntries() const { return logEntries; } + //subset of std::vector<> interface: + typedef std::vector::const_iterator const_iterator; + const_iterator begin() const { return entries.begin(); } + const_iterator end () const { return entries.end (); } + bool empty() const { return entries.empty(); } private: - std::vector logEntries; //list of non-resolved errors and warnings + std::vector entries; //list of non-resolved errors and warnings }; @@ -59,29 +64,26 @@ private: - - - - //######################## implementation ########################## template inline -void ErrorLog::logMsg(const String& message, zen::MessageType type) +void ErrorLog::logMsg(const String& text, zen::MessageType type) { - const LogEntry newEntry = { std::time(nullptr), type, copyStringTo(message) }; - logEntries.push_back(newEntry); + const LogEntry newEntry = { std::time(nullptr), type, copyStringTo(text) }; + entries.push_back(newEntry); } inline int ErrorLog::getItemCount(int typeFilter) const { - return static_cast(std::count_if(logEntries.begin(), logEntries.end(), [&](const LogEntry& e) { return e.type & typeFilter; })); + return static_cast(std::count_if(entries.begin(), entries.end(), [&](const LogEntry& e) { return e.type & typeFilter; })); } namespace { -MsgString formatMessageImpl(const LogEntry& entry) //internal linkage +template +String formatMessageImpl(const LogEntry& entry) //internal linkage { auto getTypeName = [&]() -> std::wstring { @@ -96,11 +98,11 @@ MsgString formatMessageImpl(const LogEntry& entry) //internal linkage case TYPE_FATAL_ERROR: return _("Fatal Error"); } - assert(false); + assert(false); return std::wstring(); }; - MsgString formattedText = L"[" + formatTime(FORMAT_TIME, localTime(entry.time)) + L"] " + copyStringTo(getTypeName()) + L": "; + String formattedText = L"[" + formatTime(FORMAT_TIME, localTime(entry.time)) + L"] " + copyStringTo(getTypeName()) + L": "; const size_t prefixLen = formattedText.size(); for (auto iter = entry.message.begin(); iter != entry.message.end(); ) @@ -108,7 +110,7 @@ MsgString formatMessageImpl(const LogEntry& entry) //internal linkage { formattedText += L'\n'; - MsgString blanks; + String blanks; blanks.resize(prefixLen, L' '); formattedText += blanks; @@ -125,8 +127,8 @@ MsgString formatMessageImpl(const LogEntry& entry) //internal linkage } } -inline -MsgString formatMessage(const LogEntry& entry) { return formatMessageImpl(entry); } +template inline +String formatMessage(const LogEntry& entry) { return formatMessageImpl(entry); } } #endif //ERRORLOGGING_H_INCLUDED -- cgit