diff --git a/CMakeLists.txt b/CMakeLists.txt index f61fbe562f33f19f8998659f554851accf01f914..b71dca246add4dfa6b576d359afacab10aa455cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ project(libcaosdb DESCRIPTION "C and C++ client libraries for CaosDB" LANGUAGES CXX C) -set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD 99) set(CMAKE_CXX_STANDARD 17) set(CMAKE_C_EXTENSIONS OFF) diff --git a/DEPENDENCIES.md b/DEPENDENCIES.md index 7a8c01251891d47d3aa1b57028719e817843b0bc..63a0678aa601762279c7c46bd71d7582051c4287 100644 --- a/DEPENDENCIES.md +++ b/DEPENDENCIES.md @@ -1,7 +1,7 @@ # GENERAL -* >=conan-1.37.2 (e.g. with `pip install conan`) -* >=cmake-3.14 +* >=conan-1.37.2 +* >=cmake-3.13 * >=gcc-10.2.0 | >=clang-11 # OPTIONAL diff --git a/include/caosdb/connection.h b/include/caosdb/connection.h index 22bdc6f9f5b524ae895469d25d540f9679e65353..5d7930a0d63e484f01be52dab7749d02b3916c33 100644 --- a/include/caosdb/connection.h +++ b/include/caosdb/connection.h @@ -149,6 +149,9 @@ public: return ConnectionManager::GetInstance().mGetConnection(name); }; + /** + * Get the connection marked by the "default" key in the configuration. + */ inline static auto GetDefaultConnection() -> const std::shared_ptr<Connection> & { return ConnectionManager::GetInstance().mGetDefaultConnection(); diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h index 911f621b2f5b0be8b939ee42162652166364feba..34f8d1be886a1e14ee20eaf1d4cede203d736884 100644 --- a/include/caosdb/entity.h +++ b/include/caosdb/entity.h @@ -213,6 +213,8 @@ private: * * This is a property which belongs to another entity. Don't confuse it with * an Entity with the "Property" role. + * + * @brief Property of an Entity. */ class Property { public: diff --git a/include/caosdb/exceptions.h b/include/caosdb/exceptions.h index 6dc44cbef4d05345eaab4b00d439967f7c88d89a..4653ed29dfaaa6a7543b9ff5863d3f2dbce4316d 100644 --- a/include/caosdb/exceptions.h +++ b/include/caosdb/exceptions.h @@ -32,9 +32,9 @@ using std::runtime_error; /** * @brief Generic exception class of the caosdb client library. */ -class GenericException : public runtime_error { +class Exception : public runtime_error { public: - explicit GenericException(StatusCode code, const std::string &what_arg) + explicit Exception(StatusCode code, const std::string &what_arg) : runtime_error(what_arg), code(code) {} [[nodiscard]] inline auto GetCode() const -> StatusCode { return this->code; } @@ -45,57 +45,47 @@ private: /** * @brief Exception for authentication errors. */ -class AuthenticationError : public GenericException { +class AuthenticationError : public Exception { public: explicit AuthenticationError(const std::string &what_arg) - : GenericException(StatusCode::AUTHENTICATION_ERROR, what_arg) {} + : Exception(StatusCode::AUTHENTICATION_ERROR, what_arg) {} }; /** * @brief The connection to the CaosDB server is down. */ -class ConnectionError : public GenericException { +class ConnectionError : public Exception { public: explicit ConnectionError(const std::string &what_arg) - : GenericException(StatusCode::CONNECTION_ERROR, what_arg) {} + : Exception(StatusCode::CONNECTION_ERROR, what_arg) {} }; /** * @brief The transaction terminated unsuccessfully. */ -class TransactionError : public GenericException { -protected: - TransactionError(StatusCode code, const std::string &what_arg) - : GenericException(code, what_arg) {} - +class TransactionError : public Exception { public: explicit TransactionError(const std::string &what_arg) - : GenericException(StatusCode::GENERIC_TRANSACTION_ERROR, what_arg) {} -}; - -class TransactionStatusError : public TransactionError { -public: - explicit TransactionStatusError(const std::string &what_arg) - : TransactionError(StatusCode::TRANSACTION_STATUS_ERROR, what_arg) {} + : Exception(StatusCode::GENERIC_TRANSACTION_ERROR, what_arg) {} }; /** * @brief Exception for errors of the ConfigurationManager or other components * of the configuration. */ -class ConfigurationError : public GenericException { +class ConfigurationError : public Exception { public: explicit ConfigurationError(const std::string &what_arg) - : GenericException(StatusCode::CONFIGURATION_ERROR, what_arg) {} + : Exception(StatusCode::CONFIGURATION_ERROR, what_arg) {} }; /** * @brief The connection isn't known to the ConnectionManager under this name. */ -class UnknownConnectionError : public GenericException { +class UnknownConnectionError : public Exception { public: explicit UnknownConnectionError(const std::string &what_arg) - : GenericException(StatusCode::UNKNOWN_CONNECTION_ERROR, what_arg) {} + : Exception(StatusCode::UNKNOWN_CONNECTION_ERROR, what_arg) {} }; } // namespace caosdb::exceptions diff --git a/include/caosdb/log_level.h b/include/caosdb/log_level.h index af05d3ba11b3d0e6252c889f7c9be1ddd61015f8..c2fcb9b1181dcb86f91dfaf9a4ddecba3ea417cb 100644 --- a/include/caosdb/log_level.h +++ b/include/caosdb/log_level.h @@ -19,8 +19,8 @@ * */ -#ifndef CAOSDB_LOG_LEVELS_H -#define CAOSDB_LOG_LEVELS_H +#ifndef CAOSDB_LOG_LEVEL_H +#define CAOSDB_LOG_LEVEL_H #define CAOSDB_LOG_LEVEL_OFF 1000000 #define CAOSDB_LOG_LEVEL_FATAL 700 diff --git a/include/caosdb/logging.h b/include/caosdb/logging.h index 26ec1c8465b6805132f66853cea5aa4e384018ed..fcebc6df8b5db2eb9f800893f8d296d6174285ce 100644 --- a/include/caosdb/logging.h +++ b/include/caosdb/logging.h @@ -43,6 +43,9 @@ typedef boost::log::sources::severity_channel_logger<int, std::string> BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(logger, boost_logger_class) +/** + * This class stores the integer log level. + */ class LevelConfiguration { public: LevelConfiguration(int level) : level(level){}; @@ -54,6 +57,11 @@ private: class SinkConfiguration; +/** + * This class stores the logging level and log sinks. + * + * Sinks are represented by SinkConfiguration objects. + */ class LoggingConfiguration : public LevelConfiguration { public: virtual ~LoggingConfiguration() = default; @@ -69,6 +77,23 @@ private: auto initialize_logging_defaults() -> int; auto initialize_logging(const LoggingConfiguration &configuration) -> void; +/** + * A logging sink is characterized by a name and destination. + * + * Typical inheriting configurations exist for console, files and syslog. + * + * When a SinkConfiguration is created from a configuration, the sink + * configuration must contain a \p destination key which matches one of the + * keywords for implemented sinks. At the moment of writing this documentation, + * valid destinations are: + * + * \li \p file + * \li \p console + * \li \p syslog + * + * A \p level keyword sets the logging level, if it exists at the sink or + * logging level of the configuration. + */ class SinkConfiguration : public LevelConfiguration { public: virtual ~SinkConfiguration() = default; @@ -104,6 +129,12 @@ private: const std::string destination = "Console"; }; +/** + * The file name is the destination, the directory can be set separately. + * + * If there is a `directory` key in the configuration, that will be used as a + * default, otherwise it is the current directory. + */ class FileSinkConfiguration : public SinkConfiguration { public: virtual ~FileSinkConfiguration() = default; @@ -136,27 +167,27 @@ private: }; /** - * Convenience function for the c-interface. + * Convenience function for the C interface. */ void caosdb_log_fatal(const char *channel, const char *msg); /** - * Convenience function for the c-interface. + * Convenience function for the C interface. */ void caosdb_log_error(const char *channel, const char *msg); /** - * Convenience function for the c-interface. + * Convenience function for the C interface. */ void caosdb_log_warn(const char *channel, const char *msg); /** - * Convenience function for the c-interface. + * Convenience function for the C interface. */ void caosdb_log_info(const char *channel, const char *msg); /** - * Convenience function for the c-interface. + * Convenience function for the C interface. */ void caosdb_log_debug(const char *channel, const char *msg); /** - * Convenience function for the c-interface. + * Convenience function for the C interface. */ void caosdb_log_trace(const char *channel, const char *msg); diff --git a/include/caosdb/message_code.h b/include/caosdb/message_code.h index 17ec1c1a9fdfb58a512eaa5e7940686ea9ccbaa3..6f22c12c81064588642c747a0e2dad7d3ceef8c1 100644 --- a/include/caosdb/message_code.h +++ b/include/caosdb/message_code.h @@ -27,9 +27,13 @@ /** * MessageCodes for entity messages. * - * In contrast to the status codes, the message codes are part of the CaosDB - * API. Messages (and their codes) represent the state of the entities in a - * transaction or the server. + * In contrast to the status codes, the message codes are part of the + * CaosDB API. Messages (and their codes) represent the state of the + * entities in a transaction or the server. + * + * For a specification of the message codes, look at the protobuf + * documentation. The sources and documentation can be found at + * https://gitlab.indiscale.com/caosdb/src/caosdb-proto. */ namespace caosdb::entity { diff --git a/include/caosdb/transaction_status.h b/include/caosdb/transaction_status.h index 8645aceb3749efbbcf8c6cfec5aa96394b029ed7..6c5b7a6ee30c498f15c3c25c5ec5fbc3e1d93ea0 100644 --- a/include/caosdb/transaction_status.h +++ b/include/caosdb/transaction_status.h @@ -25,7 +25,12 @@ /** * TransactionStatus indicates the current status of a transaction and, when it * has already terminated, whether the transaction has been successful or not. + * + * A status code of 0 denotes a generic success state, positive values indicate + * errors, and negative values indicate other states, such as different stages + * of a transaction in process. */ + #include "caosdb/status_code.h" #include "caosdb/exceptions.h" #include <memory> // for shared_ptr, unique_ptr @@ -35,7 +40,7 @@ namespace caosdb::transaction { using caosdb::StatusCode; using caosdb::exceptions::AuthenticationError; using caosdb::exceptions::ConnectionError; -using caosdb::exceptions::GenericException; +using caosdb::exceptions::Exception; using caosdb::exceptions::TransactionError; /** @@ -113,7 +118,7 @@ public: case StatusCode::GENERIC_TRANSACTION_ERROR: throw TransactionError(this->description); default: - throw GenericException(StatusCode::GENERIC_ERROR, this->description); + throw Exception(StatusCode::GENERIC_ERROR, this->description); } } @@ -130,7 +135,7 @@ public: /** * Return a description of the erroneous state. * - * Returns an empty string if there is no description. + * No description yields an empty string. */ inline auto GetDescription() const -> const std::string & { return this->description; diff --git a/src/caosdb/configuration.cpp b/src/caosdb/configuration.cpp index b3934d31511ced991488cef7f0343294264f0574..24e27904bd5237448b62058d25e04a4e794487bc 100644 --- a/src/caosdb/configuration.cpp +++ b/src/caosdb/configuration.cpp @@ -358,7 +358,7 @@ auto ConfigurationManager::mReset() noexcept -> int { mClear(); InitializeDefaults(); return StatusCode::SUCCESS; - } catch (const caosdb::exceptions::GenericException &exc) { + } catch (const caosdb::exceptions::Exception &exc) { return exc.GetCode(); } catch (const std::exception &exc) { CAOSDB_LOG_ERROR(logger_name) @@ -373,7 +373,7 @@ auto ConfigurationManager::mClear() noexcept -> int { json_configuration = value(nullptr); ConnectionManager::Reset(); return StatusCode::SUCCESS; - } catch (const caosdb::exceptions::GenericException &exc) { + } catch (const caosdb::exceptions::Exception &exc) { return exc.GetCode(); } catch (const std::exception &exc) { CAOSDB_LOG_ERROR(logger_name) @@ -468,7 +468,7 @@ auto ConfigurationManager::InitializeDefaults() -> int { // NOLINT // find the configuration file... std::unique_ptr<path> configuration_file_path; - for (const std::string &configuration_file : + for (const std::string configuration_file : caosdb::LIBCAOSDB_CONFIGURATION_FILES_PRECEDENCE) { if (configuration_file == "$CAOSDB_CLIENT_CONFIGURATION") { // user specified a file via the environment variable @@ -508,9 +508,11 @@ auto ConfigurationManager::InitializeDefaults() -> int { // NOLINT // ... and use the configuration file if (configuration_file_path != nullptr) { + // TODO(tf): log which file has been used. mLoadSingleJSONConfiguration(*configuration_file_path); } + // Logging in the configuration leads to additional content. if (this->json_configuration.is_object() && this->json_configuration.as_object().contains("logging")) { LoggingConfiguration logging_configuration = diff --git a/src/caosdb/logging.cpp b/src/caosdb/logging.cpp index 486ab846c4ce43225b9242aceb557775b9632531..3618826a5ed6061f210c393b3429b641293fccae 100644 --- a/src/caosdb/logging.cpp +++ b/src/caosdb/logging.cpp @@ -59,6 +59,7 @@ auto LoggingConfiguration::GetSinks() const SinkConfiguration::SinkConfiguration(std::string name, int level) : LevelConfiguration(level), name(std::move(name)) {} + [[nodiscard]] auto SinkConfiguration::GetName() const -> const std::string & { return this->name; } @@ -77,6 +78,7 @@ auto SinkConfiguration::Configure(boost::log::settings &settings) const ConsoleSinkConfiguration::ConsoleSinkConfiguration(const std::string &name, int level) : SinkConfiguration(name, level) {} + [[nodiscard]] auto ConsoleSinkConfiguration::GetDestination() const -> const std::string & { CAOSDB_LOG_TRACE(logger_name) @@ -93,12 +95,14 @@ auto ConsoleSinkConfiguration::Configure(boost::log::settings &settings) const FileSinkConfiguration::FileSinkConfiguration(const std::string &name, int level) : SinkConfiguration(name, level) {} + [[nodiscard]] auto FileSinkConfiguration::GetDestination() const -> const std::string & { CAOSDB_LOG_TRACE(logger_name) << "Enter FileSinkConfiguration::GetDestination()"; return this->destination; } + auto FileSinkConfiguration::SetDirectory(const std::string &directory) -> void { this->directory = std::string(directory); } @@ -114,11 +118,13 @@ auto FileSinkConfiguration::Configure(boost::log::settings &settings) const SyslogSinkConfiguration::SyslogSinkConfiguration(const std::string &name, int level) : SinkConfiguration(name, level) {} + [[nodiscard]] auto SyslogSinkConfiguration::GetDestination() const -> const std::string & { return this->destination; } +// Called if no custom logging settings are specified. auto initialize_logging_defaults() -> int { // first: turn everything off boost::log::settings off_settings; @@ -148,6 +154,7 @@ auto initialize_logging_defaults() -> int { return 0; } +// Called if custom logging settings are specified. auto initialize_logging(const LoggingConfiguration &configuration) -> void { boost::log::settings new_settings;