Commit 8c58af81 authored by Bryan Berthou's avatar Bryan Berthou
Browse files

refs#16

In ElementaryUtils (trunk) :
- Add getter() in PropertiesManager ; better exception message.
- Create custom exception class.
- In LoggerManager add new error method to handle custom exception message ; add color for WARN and ERROR message.
- In IniFileParser : store file path ; replace std::runtime_error by CustomException ; better exception message.
parent b977145c
......@@ -42,6 +42,8 @@ public:
std::string getString(const std::string & section, const std::string & key);
std::string getString(const std::string & key);
std::string getPropertiesFilePath() const;
private:
static sf::Mutex m_mutex;
......
#ifndef CUSTOM_EXCEPTION_H
#define CUSTOM_EXCEPTION_H
/**
* @file CustomException.h
* @author Bryan BERTHOU (SPhN / CEA Saclay)
* @date September 16, 2016
* @version 1.0
*/
#include <stdexcept>
#include <string>
namespace ElemUtils {
/**
* @class CustomException
* @brief
*/
class CustomException: public std::runtime_error {
public:
CustomException(const std::string &className, const std::string &funcName,
const std::string &errorMsg);
virtual ~CustomException() throw ();
virtual const char* what() const throw ();
const std::string& getClassName() const;
const std::string& getErrorMsg() const;
const std::string& getFuncName() const;
private:
std::string m_className;
std::string m_funcName;
std::string m_errorMsg;
};
} /* namespace ElemUtils */
#endif /* CUSTOM_EXCEPTION_H */
......@@ -19,6 +19,10 @@
#include "LoggerLevel.h"
#include "LoggerPrintMode.h"
namespace ElemUtils {
class CustomException;
} /* namespace ElemUtils */
namespace ElemUtils {
class LoggerMessage;
} /* namespace ElemUtils */
......@@ -62,6 +66,8 @@ public:
void error(const std::string & className, const std::string & functionName,
const std::string & message);
void error(const CustomException &customException);
void addMessageToBuffer(LoggerMessage loggerMessage);
std::string toString();
......
......@@ -36,6 +36,8 @@ public:
void clear();
std::string toString() const;
private:
// <key, parameters<key, value> >
std::multimap<std::string, Parameters> m_multimapParameters;
......
......@@ -40,7 +40,11 @@ public:
std::string getString(const std::string & key);
std::string getString(const std::string & section, const std::string & key);
const std::string& getFilePath() const;
private:
std::string m_filePath;
std::map<std::string, std::string> m_values;
std::map<std::string, std::string>::iterator m_it;
......
......@@ -2,9 +2,8 @@
#include <SFML/System/Lock.hpp>
#include <SFML/System/Mutex.hpp>
#include <stdexcept>
#include "../../include/ElementaryUtils/string_utils/Formatter.h"
#include "../../include/ElementaryUtils/logger/CustomException.h"
namespace ElemUtils {
......@@ -42,10 +41,7 @@ void PropertiesManager::init(const std::string &iniFilePath) {
try {
m_iniFileParser.parse(iniFilePath);
} catch (std::exception &e) {
throw std::runtime_error(
Formatter()
<< "(PropertiesManager::init) Cannot read ini file : "
<< iniFilePath << " " << e.what());
throw CustomException("PropertiesManager", __func__, e.what());
}
} // mutex.unlock()
......@@ -65,3 +61,7 @@ std::string PropertiesManager::getString(const std::string & key) {
} // mutex.unlock()
} // namespace ElemUtils
std::string ElemUtils::PropertiesManager::getPropertiesFilePath() const {
return m_iniFileParser.getFilePath();
}
#include "../../../include/ElementaryUtils/logger/CustomException.h"
#include "../../../include/ElementaryUtils/string_utils/Formatter.h"
namespace ElemUtils {
CustomException::CustomException(const std::string &className,
const std::string &funcName, const std::string &errorMsg) :
std::runtime_error(errorMsg), m_className(className), m_funcName(
funcName), m_errorMsg(errorMsg) {
}
CustomException::~CustomException() throw () {
//Noting to do
}
const char* CustomException::what() const throw () {
ElemUtils::Formatter formatter;
formatter << "(" << m_className << "::" << m_funcName << ") " << m_errorMsg;
return formatter.str().c_str();
}
} /* namespace ElemUtils */
const std::string& ElemUtils::CustomException::getClassName() const {
return m_className;
}
const std::string& ElemUtils::CustomException::getErrorMsg() const {
return m_errorMsg;
}
const std::string& ElemUtils::CustomException::getFuncName() const {
return m_funcName;
}
......@@ -3,11 +3,11 @@
#include <SFML/System/Lock.hpp>
#include <SFML/System/Sleep.hpp>
#include <SFML/System/Time.hpp>
#include <stdexcept>
#include <utility>
#include <vector>
#include "../../../include/ElementaryUtils/file_utils/FileUtils.h"
#include "../../../include/ElementaryUtils/logger/CustomException.h"
#include "../../../include/ElementaryUtils/logger/LoggerMessage.h"
#include "../../../include/ElementaryUtils/parameters/GenericType.h"
#include "../../../include/ElementaryUtils/parser/IniFileParser.h"
......@@ -281,11 +281,26 @@ void LoggerManager::error(const std::string & className,
message));
}
void LoggerManager::error(const CustomException &customException) {
addMessageToBuffer(
LoggerMessage(LoggerLevel::ERROR, customException.getClassName(),
customException.getFuncName(),
customException.getErrorMsg()));
}
void LoggerManager::addMessageToBuffer(LoggerMessage loggerMessage) {
sf::Lock lock(m_mutex); // mutex.lock()
if (m_active == true && isLoggable(loggerMessage)) {
m_buffer += loggerMessage.toString() + '\n';
if (loggerMessage.getLevel().getType() == LoggerLevel::ERROR) {
m_buffer += Formatter() << "\033[1;31m " << loggerMessage.toString()
<< " \033[0m\n";
} else if (loggerMessage.getLevel().getType() == LoggerLevel::WARN) {
m_buffer += Formatter() << "\033[33m " << loggerMessage.toString()
<< " \033[0m\n";
} else {
m_buffer += loggerMessage.toString() + '\n';
}
}
} // mutex.unlock()
......
......@@ -61,3 +61,18 @@ void ElemUtils::MultimapParameters::clear() {
}
} /* namespace ElemUtils */
std::string ElemUtils::MultimapParameters::toString() const {
ElemUtils::Formatter formatter;
std::multimap<std::string, Parameters>::const_iterator it;
for (it = m_multimapParameters.begin(); it != m_multimapParameters.end();
it++) {
formatter << "Object name = " << (it->first) << '\n';
formatter << "Parameters = " << (it->second).toString();
}
return formatter;
}
......@@ -43,8 +43,8 @@ const GenericType& Parameters::get(const std::string& key) const {
} else {
throw std::runtime_error(
Formatter()
<< "(Parameters::modify) Cannot found parameter name = "
<< key);
<< "(Parameters::get) Cannot found parameter name = \""
<< key << "\"");
}
}
......
#include "../../../include/ElementaryUtils/parser/IniFileParser.h"
#include <stddef.h>
#include <stdexcept>
#include <utility>
#include <vector>
#include "../../../include/ElementaryUtils/file_utils/FileUtils.h"
#include "../../../include/ElementaryUtils/logger/CustomException.h"
#include "../../../include/ElementaryUtils/string_utils/Formatter.h"
#include "../../../include/ElementaryUtils/string_utils/StringUtils.h"
......@@ -13,7 +13,8 @@ namespace ElemUtils {
std::string IniFileParser::SECTION_KEY_CONCATENATOR = "=";
IniFileParser::IniFileParser() {
IniFileParser::IniFileParser() :
m_filePath(StringUtils::EMPTY) {
}
IniFileParser::~IniFileParser() {
......@@ -21,6 +22,8 @@ IniFileParser::~IniFileParser() {
}
void IniFileParser::parse(const std::string & configFilePath) {
m_filePath = configFilePath;
if (FileUtils::isReadable(configFilePath)) {
// retrieve each line of the file
std::vector<std::string> fileLines = FileUtils::readByLine(
......@@ -35,9 +38,8 @@ void IniFileParser::parse(const std::string & configFilePath) {
}
} else {
// if the input file is unreadable then throw an exception
throw std::runtime_error(
"(IniFileParser) Unable to open config file: "
+ configFilePath);
throw CustomException("IniFileParser", __func__,
Formatter() << "Cannot open ini file: " << configFilePath);
}
}
......@@ -66,9 +68,9 @@ void IniFileParser::analyse(std::string & fileLine, std::string & sectionName,
}
// else the equal character is missing
else {
throw std::runtime_error(
throw CustomException("IniFileParser", __func__,
Formatter()
<< "(IniFileParser::analyse) ERROR in the ini file '=' character is missing, line : "
<< "Problem in ini file ; character '=' is missing ; line: "
<< fileLineNumber);
}
}
......@@ -81,9 +83,9 @@ void IniFileParser::analyseSection(const std::string & fileLine,
// if there is no close square bracket character then print an error
if (closeSquareBracketCharPos == std::string::npos) {
throw std::runtime_error(
throw CustomException("IniFileParser", __func__,
Formatter()
<< "(IniFileParser::analyseSection) ERROR in the ini file ']' character is missing, line : "
<< "Problem in ini file ; character ']' is missing ; line: "
<< fileLineNumber);
}
// if close square bracket character is at the end of the string then retrieve the section name
......@@ -92,9 +94,9 @@ void IniFileParser::analyseSection(const std::string & fileLine,
}
// else it's misplaced and print an error
else {
throw std::runtime_error(
throw CustomException("IniFileParser", __func__,
Formatter()
<< "(IniFileParser::analyseSection) ERROR in the ini file ']' character is at wrong place, line : "
<< "Problem in ini file ; character ']' is at wrong place ; line: "
<< fileLineNumber);
}
}
......@@ -132,9 +134,9 @@ std::string IniFileParser::getString(const std::string & key) {
m_it = m_values.find(makeKey(StringUtils::EMPTY, key));
return (m_it != m_values.end()) ?
m_it->second :
throw std::runtime_error(
"(IniFileParser::getString) enable to find property name = "
+ key);
throw CustomException("IniFileParser", __func__,
Formatter() << "Cannot find property name \"" << key
<< "\" in file \"" << m_filePath << "\"");
}
//TODO voir à ajouter en parametre la valeur de retour par défaut dans le cas ou EMPTY est une valeur possible
......@@ -143,9 +145,10 @@ std::string IniFileParser::getString(const std::string & section,
m_it = m_values.find(makeKey(section, key));
return (m_it != m_values.end()) ?
m_it->second :
throw std::runtime_error(
"[IniFileParser::getString] enable to find property name = "
+ key + " with section name = " + section);
throw CustomException("IniFileParser", __func__,
Formatter() << "Cannot find property name \"" << key
<< "\" with section name \"" << section
<< "\" in file \"" << m_filePath << "\"");
}
std::map<std::string, std::string> IniFileParser::getValues() {
......@@ -153,3 +156,7 @@ std::map<std::string, std::string> IniFileParser::getValues() {
}
} // namespace ElemUtils
const std::string& ElemUtils::IniFileParser::getFilePath() const {
return m_filePath;
}
......@@ -134,25 +134,29 @@ void XMLParser::parseAttributesFromXMLElement(const std::string& file,
std::string attributeName = StringUtils::EMPTY;
std::string attributeValue = StringUtils::EMPTY;
// if valueStringStartIndex is not a double quote character : attribute value is not empty
if (file.at(valueStringStartIndex) != '"') {
// try to retrieve end index of value
size_t valueStringEndIndex = file.find_first_of('"',
valueStringStartIndex);
// if end of file or end tag element is not reach : value is retrieved
if (valueStringEndIndex != std::string::npos
|| valueStringEndIndex < xmlEndTagIndex) {
attributeValue = file.substr(valueStringStartIndex,
valueStringEndIndex - valueStringStartIndex);
} else {
// else a missing double quote character is missing somewhere
std::runtime_error(
"(XMLParser::parseAttributesFromXMLElement) Bad XML file : missing character");
}
// removed unwanted space characters from attribute name
std::string attributeName = file.substr(keyStringStartIndex,
equalCharIndex - keyStringStartIndex);
StringUtils::replaceAll(attributeName, " ", StringUtils::EMPTY);
// if value retrieve then extract key and value to add them to attributs
// add new attribute to the previous attribute list
attributes.add(attributeName, attributeValue);
// parse remaining attributes recursively
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment