From 160e67a94d47e9c985080008d741e77210ebc7da Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Mon, 21 Jul 2014 02:11:07 +0200 Subject: [PATCH] utility: added stream logging macros For easier use with int and other basic types, new logging macros where introduced that use stringstream internally, so heterogeneous data can be logged using the following syntax: LOG_INFO_STREAM(<< "hello " << 42 << '!'); --- bin/test/data/log/test_log.txt | 6 +-- src/lib/data/Storage.cpp | 21 +++----- src/lib/utility/logging/logging.h | 41 ++++++++++++--- src/lib/utility/text/TextAccess.cpp | 77 ++++++++--------------------- 4 files changed, 66 insertions(+), 79 deletions(-) diff --git a/bin/test/data/log/test_log.txt b/bin/test/data/log/test_log.txt index fd67f034..988d0b56 100644 --- a/bin/test/data/log/test_log.txt +++ b/bin/test/data/log/test_log.txt @@ -1,8 +1,8 @@ ConfigManager.cpp ERROR: value path/to/nowhere is not present in config. Graph.cpp ERROR: Can't remove member edge, without removing the child node. -TextAccess.cpp WARNING: Index 'firstLine' has to be lower or equal index 'lastLine', is 2 > 1 -TextAccess.cpp WARNING: Tried to access index 9. Maximum index is 7 -TextAccess.cpp WARNING: Tried to access index 9. Maximum index is 7 +TextAccess.cpp WARNING: Index 'firstLine' has to be lower or equal index 'lastLine', is 3 > 2 +TextAccess.cpp WARNING: Tried to access index 10. Maximum index is 8 +TextAccess.cpp WARNING: Tried to access index 10. Maximum index is 8 TextAccess.cpp WARNING: Line numbers start with one, is 0 TextAccess.cpp WARNING: Line numbers start with one, is 0 TokenLocationCollection.cpp ERROR: Can't create TokenLocation with wrong boundaries. diff --git a/src/lib/data/Storage.cpp b/src/lib/data/Storage.cpp index b7e89876..157cdfe9 100644 --- a/src/lib/data/Storage.cpp +++ b/src/lib/data/Storage.cpp @@ -1,7 +1,5 @@ #include "data/Storage.h" -#include - #include "data/graph/edgeComponent/EdgeComponentDataType.h" #include "data/location/TokenLocation.h" #include "data/location/TokenLocationFile.h" @@ -29,16 +27,12 @@ void Storage::clear() void Storage::logGraph() const { - std::stringstream str; - str << "\n" << m_graph; - LOG_INFO(str.str()); + LOG_INFO_STREAM(<< '\n' << m_graph); } void Storage::logLocations() const { - std::stringstream str; - str << "\n" << m_locationCollection; - LOG_INFO(str.str()); + LOG_INFO_STREAM(<< '\n' << m_locationCollection); } @@ -512,12 +506,11 @@ TokenLocation* Storage::addTokenLocation(Token* token, const ParseLocation& loc) void Storage::log(std::string type, std::string str, const ParseLocation& location) const { - std::stringstream info; - info << type << ": " << str; - info << " <" << location.filePath << " "; - info << location.startLineNumber << ":" << location.startColumnNumber << " "; - info << location.endLineNumber << ":" << location.endColumnNumber << ">"; - LOG_INFO(info.str()); + LOG_INFO_STREAM( + << type << ": " << str << " <" << location.filePath << " " + << location.startLineNumber << ":" << location.startColumnNumber << " " + << location.endLineNumber << ":" << location.endColumnNumber << ">" + ); } std::vector> Storage::getEdgesOfTypeOfNode(const Id id, const Edge::EdgeType type) const diff --git a/src/lib/utility/logging/logging.h b/src/lib/utility/logging/logging.h index 454dc7ba..c40f6796 100644 --- a/src/lib/utility/logging/logging.h +++ b/src/lib/utility/logging/logging.h @@ -1,29 +1,58 @@ #ifndef LOGGING_H #define LOGGING_H +#include + #include "utility/logging/LogManager.h" /** * @brief Makros to simplify usage of the log manager */ -#define LOG_INFO(str) \ +#define LOG_INFO(__str__) \ do \ { \ - LogManager::getInstance()->logInfo(str, __FILE__, __FUNCTION__, __LINE__); \ + LogManager::getInstance()->logInfo(__str__, __FILE__, __FUNCTION__, __LINE__); \ } \ while(0) \ -#define LOG_WARNING(str) \ +#define LOG_WARNING(__str__) \ do \ { \ - LogManager::getInstance()->logWarning(str, __FILE__, __FUNCTION__, __LINE__); \ + LogManager::getInstance()->logWarning(__str__, __FILE__, __FUNCTION__, __LINE__); \ } \ while(0) \ -#define LOG_ERROR(str) \ +#define LOG_ERROR(__str__) \ do \ { \ - LogManager::getInstance()->logError(str, __FILE__, __FUNCTION__, __LINE__); \ + LogManager::getInstance()->logError(__str__, __FILE__, __FUNCTION__, __LINE__); \ + } \ + while(0) \ + +#define LOG_INFO_STREAM(__s__) \ + do \ + { \ + std::stringstream __ss__; \ + __ss__ __s__; \ + LogManager::getInstance()->logInfo(__ss__.str(), __FILE__, __FUNCTION__, __LINE__); \ + } \ + while(0) \ + +#define LOG_WARNING_STREAM(__s__) \ + do \ + { \ + std::stringstream __ss__; \ + __ss__ __s__; \ + LogManager::getInstance()->logWarning(__ss__.str(), __FILE__, __FUNCTION__, __LINE__); \ + } \ + while(0) \ + +#define LOG_ERROR_STREAM(__s__) \ + do \ + { \ + std::stringstream __ss__; \ + __ss__ __s__; \ + LogManager::getInstance()->logError(__ss__.str(), __FILE__, __FUNCTION__, __LINE__); \ } \ while(0) \ diff --git a/src/lib/utility/text/TextAccess.cpp b/src/lib/utility/text/TextAccess.cpp index 9efdd95e..eff2fe83 100644 --- a/src/lib/utility/text/TextAccess.cpp +++ b/src/lib/utility/text/TextAccess.cpp @@ -1,7 +1,6 @@ #include "TextAccess.h" #include -#include #include "utility/logging/logging.h" @@ -40,50 +39,23 @@ std::string TextAccess::getFilePath() const std::string TextAccess::getLine(const unsigned int lineNumber) const { - if (lineNumber < 1) - { - std::stringstream message; - message << "Line numbers start with one, is " << lineNumber; - LOG_WARNING(message.str()); - - return ""; - } - - if (checkIndexInRange(lineNumber-1) == false) + if (!checkIndexInRange(lineNumber)) { return ""; } - return m_lines[lineNumber-1]; // -1 to correct for use as index + return m_lines[lineNumber - 1]; // -1 to correct for use as index } std::vector TextAccess::getLines(const unsigned int firstLineNumber, const unsigned int lastLineNumber) { - if(firstLineNumber < 1) - { - std::stringstream message; - message << "Line numbers start with one, is " << firstLineNumber; - LOG_WARNING(message.str()); - - return std::vector(); - } - - if(lastLineNumber < 1) - { - std::stringstream message; - message << "Line numbers start with one, is " << lastLineNumber; - LOG_WARNING(message.str()); - - return std::vector(); - } - - if (checkIndexIntervalInRange(firstLineNumber-1, lastLineNumber-1) == false) + if (!checkIndexIntervalInRange(firstLineNumber, lastLineNumber)) { return std::vector(); } - std::vector::iterator first = m_lines.begin() + firstLineNumber-1; // -1 to correct for use as index - std::vector::iterator last = m_lines.begin() + lastLineNumber; // no correction needed because 'last' needs to point one behind the last included element + std::vector::iterator first = m_lines.begin() + firstLineNumber - 1; // -1 to correct for use as index + std::vector::iterator last = m_lines.begin() + lastLineNumber; return std::vector(first, last); } @@ -121,7 +93,7 @@ std::vector TextAccess::readFile(const std::string& filePath) { std::string line; std::getline(srcFile, line); - result.push_back(line + "\n"); + result.push_back(line + '\n'); } srcFile.close(); @@ -132,14 +104,14 @@ std::vector TextAccess::splitStringByLines(const std::string& text) { std::vector result; size_t prevIndex = 0; - size_t index = text.find("\n"); + size_t index = text.find('\n'); while (index != std::string::npos) { - result.push_back(text.substr(prevIndex, index - prevIndex) + "\n"); + result.push_back(text.substr(prevIndex, index - prevIndex) + '\n'); prevIndex = index + 1; - index = text.find("\n", prevIndex); + index = text.find('\n', prevIndex); } if (prevIndex < text.length() - 1) @@ -157,13 +129,14 @@ TextAccess::TextAccess() bool TextAccess::checkIndexInRange(const unsigned int index) const { - if (index >= m_lines.size()) + if (index < 1) { - std::stringstream message; - message << "Tried to access index " << index; - message << ". Maximum index is " << m_lines.size() - 1; - LOG_WARNING(message.str()); - + LOG_WARNING_STREAM(<< "Line numbers start with one, is " << index); + return false; + } + else if (index > m_lines.size()) + { + LOG_WARNING_STREAM(<< "Tried to access index " << index << ". Maximum index is " << m_lines.size()); return false; } @@ -175,23 +148,15 @@ bool TextAccess::checkIndexIntervalInRange( const unsigned int lastIndex ) const { - if (checkIndexInRange(firstIndex) == false) + if (!checkIndexInRange(firstIndex) || !checkIndexInRange(lastIndex)) { return false; } - - if (checkIndexInRange(lastIndex) == false) + else if (firstIndex > lastIndex) { - return false; - } - - if (firstIndex > lastIndex) - { - std::stringstream message; - message << "Index 'firstLine' has to be lower or equal index 'lastLine'"; - message << ", is " << firstIndex << " > " << lastIndex; - LOG_WARNING(message.str()); - + LOG_WARNING_STREAM( + << "Index 'firstLine' has to be lower or equal index 'lastLine', is " << firstIndex << " > " << lastIndex + ); return false; }