Skip to content

Commit

Permalink
[config] Remove case-insensitive queries and use libfmt for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
gempa-jabe committed May 29, 2024
1 parent df2096a commit 2637860
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 163 deletions.
55 changes: 28 additions & 27 deletions src/system/libs/seiscomp/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,6 @@ Config::~Config() {



// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Config::setCaseSensitivityCheck(bool f) {
_symbolTable->setCaseSensitivityCheck(f);
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<




// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
bool Config::readConfig(const std::string &filename, int stage, bool raw) {
_stage = stage;
Expand Down Expand Up @@ -553,7 +544,7 @@ Config &Config::operator=(Config &&other) {
_defaultNamespacePrefix = std::move(other._defaultNamespacePrefix);

_logger = other._logger; other._logger = nullptr;
_symbolTable = other._symbolTable; _symbolTable = nullptr;
_symbolTable = other._symbolTable; other._symbolTable = nullptr;
_trackVariables = std::move(other._trackVariables);
_variables = std::move(other._variables);

Expand Down Expand Up @@ -663,7 +654,7 @@ bool Config::parseFile(std::istream &is) {
}

if ( !_namespaces.empty() ) {
CONFIG_ERROR("Namespace '%s' has not been closed", _namespaces.back().c_str());
CONFIG_ERROR("Namespace '%s' has not been closed", _namespaces.back());
return false;
}

Expand Down Expand Up @@ -698,7 +689,7 @@ bool Config::handleEntry(const std::string& entry, const std::string& comment) {
if ( tokens.size() < 2 ) {
if ( (tokens.size() == 1) && (tokens[0] == BLOCK_END) ) {
if ( _namespaces.empty() ) {
CONFIG_ERROR("Unexpected closing block: %s", entry.c_str());
CONFIG_ERROR("Unexpected closing block: %s", entry);
return false;
}

Expand All @@ -716,13 +707,13 @@ bool Config::handleEntry(const std::string& entry, const std::string& comment) {
return true;
}
else {
CONFIG_ERROR("Config entry malformed: Too few parameters for %s", entry.c_str());
CONFIG_ERROR("Config entry malformed: Too few parameters for %s", entry);
return false;
}
}

if ( tokens[0][0] == '$' ) {
CONFIG_ERROR("Cannot assign to rvalue: %s", tokens[0].c_str());
CONFIG_ERROR("Cannot assign to rvalue: %s", tokens[0]);
return false;
}

Expand All @@ -731,18 +722,18 @@ bool Config::handleEntry(const std::string& entry, const std::string& comment) {
if ( tokens[0] == KEYWORD_INCLUDE ) {
if ( tokens.size() > 2 ) {
CONFIG_ERROR("Operator %s has too many operands -> %s file",
tokens[0].c_str(), tokens[0].c_str());
tokens[0], tokens[0]);
return false;
}

if ( !parseRValue(tokens[1], parsedValues, _symbolTable,
_resolveReferences, false, &errmsg) ) {
CONFIG_ERROR("%s", errmsg.c_str());
CONFIG_ERROR("%s", errmsg);
return false;
}

if ( !handleInclude(parsedValues[0]) ) {
CONFIG_ERROR("Could not read include file %s", parsedValues[0].c_str());
CONFIG_ERROR("Could not read include file %s", parsedValues[0]);
return false;
}
}
Expand All @@ -760,15 +751,15 @@ bool Config::handleEntry(const std::string& entry, const std::string& comment) {
parsedValues.clear();
if ( !parseRValue(tmp, parsedValues, _symbolTable,
_resolveReferences, false, &errmsg) ) {
CONFIG_ERROR("%s", errmsg.c_str());
CONFIG_ERROR("%s", errmsg);
return false;
}

std::vector<std::string>::const_iterator it = parsedValues.begin();
for ( ; it != parsedValues.end(); ++it ) {
if ( !_symbolTable->remove(_namespacePrefix + *it) ) {
CONFIG_ERROR("Could not remove variable %s from symbol table",
(_namespacePrefix + (*it)).c_str());
_namespacePrefix + (*it));
return false;
}
}
Expand All @@ -780,7 +771,7 @@ bool Config::handleEntry(const std::string& entry, const std::string& comment) {
}
else if ( tokens[1] == "=" ) {
if ( tokens.size() < 3 ) {
CONFIG_ERROR("RValue missing in assignment: %s", entry.c_str());
CONFIG_ERROR("RValue missing in assignment: %s", entry);
return false;
}

Expand All @@ -790,14 +781,14 @@ bool Config::handleEntry(const std::string& entry, const std::string& comment) {
tmp += tokens[i];

if ( !eval(tmp, values, _resolveReferences, &errmsg) ) {
CONFIG_ERROR("%s", errmsg.c_str());
CONFIG_ERROR("%s", errmsg);
return false;
}

handleAssignment(tokens[0], tmp, values, comment);
}
else {
CONFIG_ERROR("Invalid entry: %s", entry.c_str());
CONFIG_ERROR("Invalid entry: %s", entry);
return false;
}

Expand All @@ -809,8 +800,10 @@ bool Config::handleEntry(const std::string& entry, const std::string& comment) {


// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
bool Config::handleInclude(const std::string& fileName) {
if ( fileName.empty() ) return false;
bool Config::handleInclude(const std::string &fileName) {
if ( fileName.empty() ) {
return false;
}

std::string tmpFileName(fileName);
// Resolve ~ for home directory
Expand All @@ -826,8 +819,13 @@ bool Config::handleInclude(const std::string& fileName) {
// Change to the config file path to be able to handle relative paths
if ( getcwd(oldPath, PATH_MAX) ) {
std::string::size_type pos = _fileName.rfind("/");
if ( pos != std::string::npos )
chdir(_fileName.substr(0, pos).c_str());
if ( pos != std::string::npos ) {
if ( chdir(_fileName.substr(0, pos).c_str()) ) {
CONFIG_ERROR("Cannot change into directory %s",
_fileName.substr(0, pos));
return false;
}
}
}
}

Expand All @@ -841,7 +839,10 @@ bool Config::handleInclude(const std::string& fileName) {
}

if ( isRelativePath ) {
chdir(oldPath);
if ( chdir(oldPath) ) {
CONFIG_ERROR("Cannot change back into directory %s", oldPath);
return false;
}
}

return true;
Expand Down
16 changes: 5 additions & 11 deletions src/system/libs/seiscomp/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Config {
/**
* Mapping of configuration variable to type
*/
typedef std::map<std::string, std::string> Variables;
using Variables = std::map<std::string, std::string>;


/**
Expand All @@ -61,21 +61,13 @@ class SC_CONFIG_API Config {
// Public interface
// ------------------------------------------------------------------------
public:
/** When names are queried and this check is enabled, it will
* throw an exception if the same name is defined in a later stage
* with respect to case insensitive name comparison.
* This allows to check for parameter inconsistencies that are
* hard to track otherwise.
*/
void setCaseSensitivityCheck(bool);

/** Reads the given configuration file.
* @param file name of the configuration files
* @param stage Optional stage value to be set to each read symbol
* @param raw Raw mode which does not resolv references like ${var}
* @return true on success
*/
bool readConfig(const std::string& file, int stage=-1, bool raw=false);
bool readConfig(const std::string &file, int stage=-1, bool raw=false);

/** Writes the configuration to the given configuration file.
* @param file name of the configuarion files
Expand Down Expand Up @@ -327,7 +319,8 @@ class SC_CONFIG_API Config {
// Private data members
// ------------------------------------------------------------------------
private:
typedef std::deque<std::string> Namespaces;
using Namespaces = std::deque<std::string>;

int _stage;
int _line;
bool _resolveReferences;
Expand All @@ -346,4 +339,5 @@ class SC_CONFIG_API Config {
} // namespace Config
} // namespace Seiscomp


#endif
7 changes: 0 additions & 7 deletions src/system/libs/seiscomp/config/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ namespace Config {



// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
char log_msg_buffer[1024];
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<




// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Logger::~Logger() {}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Expand Down
8 changes: 3 additions & 5 deletions src/system/libs/seiscomp/config/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@


#include <seiscomp/config/api.h>
#include <fmt/printf.h>
#include <cstdio>


Expand All @@ -44,13 +45,10 @@ struct SC_CONFIG_API Logger {
};


extern char log_msg_buffer[1024];


#define CONFIG_LOG_CHANNEL(chan, msg, ...) \
if ( _logger ) {\
snprintf(log_msg_buffer, 1023, msg, __VA_ARGS__);\
_logger->log(chan, _fileName.c_str(), _line, log_msg_buffer);\
auto line = fmt::sprintf(msg, __VA_ARGS__);\
_logger->log(chan, _fileName.c_str(), _line, line.c_str());\
}


Expand Down
Loading

0 comments on commit 2637860

Please sign in to comment.