Skip to content
Snippets Groups Projects
Verified Commit 1aba35eb authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: logging

parent 1db4cc02
Branches
Tags
1 merge request!3Better Error Handling and Logging
Pipeline #10838 failed
This commit is part of merge request !3. Comments created here will be created in the context of that merge request.
...@@ -23,38 +23,16 @@ ...@@ -23,38 +23,16 @@
#ifndef CAOSDB_LOGGING_H #ifndef CAOSDB_LOGGING_H
#define CAOSDB_LOGGING_H #define CAOSDB_LOGGING_H
#include "caosdb/log_level.h" #include "caosdb/log_level.h" // for CAOSDB_LOG_...
#include <cstddef> #include "boost/log/sources/global_logger_storage.hpp" // for BOOST_LOG_I...
#include <string> #include "boost/log/sources/record_ostream.hpp" // IWYU pragma: keep
#include <iostream> #include "boost/log/sources/severity_channel_logger.hpp" // for BOOST_LOG_C...
#include <vector> #include "boost/log/utility/setup/settings.hpp" // for settings
#include <memory> #include "boost/smart_ptr/intrusive_ptr.hpp" // for intrusive_ptr
#include <boost/log/sources/severity_channel_logger.hpp> #include "boost/smart_ptr/intrusive_ref_counter.hpp" // for intrusive_p...
#include <boost/log/sources/record_ostream.hpp> #include <memory> // for shared_ptr
#include <boost/log/utility/setup/common_attributes.hpp> #include <string> // for string
#include <boost/log/utility/setup/settings.hpp> #include <vector> // for vector
#include <boost/log/utility/setup/from_settings.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#define CAOSDB_LOG_FATAL(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_FATAL)
#define CAOSDB_LOG_ERROR(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_ERROR)
#define CAOSDB_LOG_WARN(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_WARN)
#define CAOSDB_LOG_INFO(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_INFO)
#define CAOSDB_LOG_DEBUG(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_DEBUG)
#define CAOSDB_LOG_TRACE(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_TRACE)
namespace caosdb::logging { namespace caosdb::logging {
...@@ -62,6 +40,7 @@ const std::string logger_name = "caosdb::logging"; ...@@ -62,6 +40,7 @@ const std::string logger_name = "caosdb::logging";
typedef boost::log::sources::severity_channel_logger<int, std::string> typedef boost::log::sources::severity_channel_logger<int, std::string>
boost_logger_class; boost_logger_class;
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(logger, boost_logger_class) BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(logger, boost_logger_class)
class LevelConfiguration { class LevelConfiguration {
...@@ -156,5 +135,50 @@ private: ...@@ -156,5 +135,50 @@ private:
const std::string destination = "Syslog"; const std::string destination = "Syslog";
}; };
/**
* Convenience function for the c-interface.
*/
void caosdb_log_fatal(const char *channel, const char *msg);
/**
* Convenience function for the c-interface.
*/
void caosdb_log_error(const char *channel, const char *msg);
/**
* Convenience function for the c-interface.
*/
void caosdb_log_warn(const char *channel, const char *msg);
/**
* Convenience function for the c-interface.
*/
void caosdb_log_info(const char *channel, const char *msg);
/**
* Convenience function for the c-interface.
*/
void caosdb_log_debug(const char *channel, const char *msg);
/**
* Convenience function for the c-interface.
*/
void caosdb_log_trace(const char *channel, const char *msg);
} // namespace caosdb::logging } // namespace caosdb::logging
#define CAOSDB_LOG_FATAL(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_FATAL)
#define CAOSDB_LOG_ERROR(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_ERROR)
#define CAOSDB_LOG_WARN(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_WARN)
#define CAOSDB_LOG_INFO(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_INFO)
#define CAOSDB_LOG_DEBUG(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_DEBUG)
#define CAOSDB_LOG_TRACE(Channel) \
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), Channel, \
CAOSDB_LOG_LEVEL_TRACE)
#endif #endif
...@@ -19,18 +19,27 @@ ...@@ -19,18 +19,27 @@
* *
*/ */
#include "caosdb/logging.h" #include "caosdb/logging.h"
#include "boost/core/swap.hpp" // for swap
#include "boost/iterator/iterator_facade.hpp"
#include "boost/log/attributes/clock.hpp"
#include "boost/log/core/core.hpp" // for core
#include "boost/log/core/record.hpp"
#include "boost/log/sources/record_ostream.hpp"
#include "boost/log/utility/setup/from_settings.hpp"
#include "boost/log/utility/setup/settings.hpp"
#include "boost/move/utility_core.hpp" // for move
#include "boost/multi_index/detail/bidir_node_iterator.hpp"
#include "boost/operators.hpp"
#include "boost/preprocessor/seq/limits/enum_256.hpp"
#include "boost/preprocessor/seq/limits/size_256.hpp"
#include "boost/property_tree/detail/exception_implementation.hpp"
#include "boost/smart_ptr/shared_ptr.hpp"
#include "boost/tuple/detail/tuple_basic.hpp" // for get
#include "caosdb/log_level.h" #include "caosdb/log_level.h"
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/utility/setup/from_settings.hpp>
#include <boost/log/utility/setup/settings.hpp>
#include <cstddef>
#include <iostream>
#include <memory> #include <memory>
#include <sstream>
#include <string> #include <string>
#include <utility> // for move
#include <vector> #include <vector>
namespace caosdb::logging { namespace caosdb::logging {
...@@ -152,4 +161,40 @@ auto initialize_logging(const LoggingConfiguration &configuration) -> void { ...@@ -152,4 +161,40 @@ auto initialize_logging(const LoggingConfiguration &configuration) -> void {
CAOSDB_LOG_DEBUG(logger_name) << "Initialized logging with custom settings."; CAOSDB_LOG_DEBUG(logger_name) << "Initialized logging with custom settings.";
} }
void caosdb_log_fatal(const char *channel, const char *msg) {
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), channel,
CAOSDB_LOG_LEVEL_FATAL)
<< msg;
}
void caosdb_log_error(const char *channel, const char *msg) {
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), channel,
CAOSDB_LOG_LEVEL_ERROR)
<< msg;
}
void caosdb_log_warn(const char *channel, const char *msg) {
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), channel,
CAOSDB_LOG_LEVEL_WARN)
<< msg;
}
void caosdb_log_info(const char *channel, const char *msg) {
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), channel,
CAOSDB_LOG_LEVEL_INFO)
<< msg;
}
void caosdb_log_debug(const char *channel, const char *msg) {
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), channel,
CAOSDB_LOG_LEVEL_DEBUG)
<< msg;
}
void caosdb_log_trace(const char *channel, const char *msg) {
BOOST_LOG_CHANNEL_SEV(caosdb::logging::logger::get(), channel,
CAOSDB_LOG_LEVEL_TRACE)
<< msg;
}
} // namespace caosdb::logging } // namespace caosdb::logging
...@@ -8,9 +8,12 @@ ...@@ -8,9 +8,12 @@
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include <string.h>
extern "C" { extern "C" {
#define CCAOSDB_LOGGER_NAME "ccaosd"
/* /*
* Macro for wrapping every function into a try-catch clause. If an exception * Macro for wrapping every function into a try-catch clause. If an exception
* occurs, the given StatusCode is being returned. * occurs, the given StatusCode is being returned.
...@@ -20,7 +23,7 @@ extern "C" { ...@@ -20,7 +23,7 @@ extern "C" {
try { \ try { \
body \ body \
} catch (const std::exception &exc) { \ } catch (const std::exception &exc) { \
CAOSDB_LOG_FATAL("ccaosdb") << "Exception: " << exc.what(); \ caosdb::logging::caosdb_log_fatal(CCAOSDB_LOGGER_NAME, exc.what()); \
return caosdb::StatusCode::code; \ return caosdb::StatusCode::code; \
} \ } \
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment