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 << '!');
This commit is contained in:
Eberhard Graether
2014-07-21 02:11:07 +02:00
parent 0d46de871a
commit 160e67a94d
4 changed files with 66 additions and 79 deletions
+3 -3
View File
@@ -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.
+7 -14
View File
@@ -1,7 +1,5 @@
#include "data/Storage.h"
#include <sstream>
#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<std::tuple<Id, Id, Id>> Storage::getEdgesOfTypeOfNode(const Id id, const Edge::EdgeType type) const
+35 -6
View File
@@ -1,29 +1,58 @@
#ifndef LOGGING_H
#define LOGGING_H
#include <sstream>
#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) \
+21 -56
View File
@@ -1,7 +1,6 @@
#include "TextAccess.h"
#include <fstream>
#include <sstream>
#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<std::string> 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<std::string>();
}
if(lastLineNumber < 1)
{
std::stringstream message;
message << "Line numbers start with one, is " << lastLineNumber;
LOG_WARNING(message.str());
return std::vector<std::string>();
}
if (checkIndexIntervalInRange(firstLineNumber-1, lastLineNumber-1) == false)
if (!checkIndexIntervalInRange(firstLineNumber, lastLineNumber))
{
return std::vector<std::string>();
}
std::vector<std::string>::iterator first = m_lines.begin() + firstLineNumber-1; // -1 to correct for use as index
std::vector<std::string>::iterator last = m_lines.begin() + lastLineNumber; // no correction needed because 'last' needs to point one behind the last included element
std::vector<std::string>::iterator first = m_lines.begin() + firstLineNumber - 1; // -1 to correct for use as index
std::vector<std::string>::iterator last = m_lines.begin() + lastLineNumber;
return std::vector<std::string>(first, last);
}
@@ -121,7 +93,7 @@ std::vector<std::string> 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<std::string> TextAccess::splitStringByLines(const std::string& text)
{
std::vector<std::string> 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;
}