Better parsing of bool properties

master
Piotr Dziwinski 2015-08-15 19:45:49 +02:00
parent f773d5714d
commit bd10f424ec
3 changed files with 30 additions and 38 deletions

View File

@ -164,14 +164,7 @@ bool CConfigFile::GetStringProperty(std::string section, std::string key, std::s
}
catch (std::exception & e)
{
if (m_loaded)
{
GetLogger()->Info("Error on parsing config file: %s\n", e.what());
}
else
{
GetLogger()->Trace("Error on parsing config file: %s\n", e.what());
}
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
return false;
}
return true;
@ -201,14 +194,7 @@ bool CConfigFile::GetIntProperty(std::string section, std::string key, int &valu
}
catch (std::exception & e)
{
if (m_loaded)
{
GetLogger()->Info("Error on parsing config file: %s\n", e.what());
}
else
{
GetLogger()->Trace("Error on parsing config file: %s\n", e.what());
}
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
return false;
}
return true;
@ -225,7 +211,19 @@ bool CConfigFile::GetBoolProperty(std::string section, std::string key, bool& va
bool result = GetIntProperty(section, key, intValue);
if (result)
{
value = intValue == 1;
if (intValue == 0)
{
value = false;
}
else if (intValue == 1)
{
value = true;
}
else
{
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing bool property %s.%s (expected 0 or 1, not %d)\n", section, key, intValue);
return false;
}
}
return result;
}
@ -254,14 +252,7 @@ bool CConfigFile::GetFloatProperty(std::string section, std::string key, float &
}
catch (std::exception & e)
{
if (m_loaded)
{
GetLogger()->Info("Error on parsing config file: %s\n", e.what());
}
else
{
GetLogger()->Trace("Error on parsing config file: %s\n", e.what());
}
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
return false;
}
return true;

View File

@ -36,13 +36,11 @@ CLogger::CLogger()
#endif
}
CLogger::~CLogger()
{
Close();
}
void CLogger::Log(LogLevel type, const char* str, va_list args)
{
if (type < m_logLevel)
@ -72,7 +70,6 @@ void CLogger::Log(LogLevel type, const char* str, va_list args)
vfprintf(IsOpened() ? m_file : stderr, str, args);
}
void CLogger::Trace(const char* str, ...)
{
va_list args;
@ -81,7 +78,6 @@ void CLogger::Trace(const char* str, ...)
va_end(args);
}
void CLogger::Debug(const char* str, ...)
{
va_list args;
@ -90,7 +86,6 @@ void CLogger::Debug(const char* str, ...)
va_end(args);
}
void CLogger::Info(const char* str, ...)
{
va_list args;
@ -99,7 +94,6 @@ void CLogger::Info(const char* str, ...)
va_end(args);
}
void CLogger::Warn(const char* str, ...)
{
va_list args;
@ -108,7 +102,6 @@ void CLogger::Warn(const char* str, ...)
va_end(args);
}
void CLogger::Error(const char* str, ...)
{
va_list args;
@ -117,7 +110,6 @@ void CLogger::Error(const char* str, ...)
va_end(args);
}
void CLogger::Message(const char* str, ...)
{
va_list args;
@ -126,6 +118,13 @@ void CLogger::Message(const char* str, ...)
va_end(args);
}
void CLogger::Log(LogLevel logLevel, const char* str, ...)
{
va_list args;
va_start(args, str);
Log(logLevel, str, args);
va_end(args);
}
void CLogger::SetOutputFile(std::string filename)
{
@ -133,7 +132,6 @@ void CLogger::SetOutputFile(std::string filename)
Open();
}
void CLogger::Open()
{
m_file = fopen(m_filename.c_str(), "w");
@ -142,26 +140,22 @@ void CLogger::Open()
fprintf(stderr, "Could not create file %s\n", m_filename.c_str());
}
void CLogger::Close()
{
if (IsOpened())
fclose(m_file);
}
bool CLogger::IsOpened()
{
return m_file != NULL;
}
void CLogger::SetLogLevel(LogLevel type)
{
m_logLevel = type;
}
bool CLogger::ParseLogLevel(const std::string& str, LogLevel& logLevel)
{
if (str == "trace")

View File

@ -96,6 +96,13 @@ public:
*/
void Error(const char *str, ...);
/** Write message to console or file with given log level
* \param logLevel - log level
* \param str - message to write
* \param ... - additional arguments
*/
void Log(LogLevel logLevel, const char *str, ...);
/** Set output file to write logs to
* \param filename - output file to write to
*/