123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- #ifndef _FASTDDS_LOG_LOG_HPP_
- #define _FASTDDS_LOG_LOG_HPP_
- #include <fastrtps/utils/DBQueue.h>
- #include <fastrtps/fastrtps_dll.h>
- #include <thread>
- #include <sstream>
- #include <atomic>
- #include <regex>
- #define logInfo(cat, msg) logInfo_(cat, msg)
- #define logWarning(cat, msg) logWarning_(cat, msg)
- #define logError(cat, msg) logError_(cat, msg)
- namespace eprosima {
- namespace fastdds {
- namespace dds {
- class LogConsumer;
- class Log
- {
- public:
-
- enum Kind
- {
- Error,
- Warning,
- Info,
- };
-
- RTPS_DllAPI static void RegisterConsumer(
- std::unique_ptr<LogConsumer>&& consumer);
-
- RTPS_DllAPI static void ClearConsumers();
-
- RTPS_DllAPI static void ReportFilenames(
- bool);
-
- RTPS_DllAPI static void ReportFunctions(
- bool);
-
- RTPS_DllAPI static void SetVerbosity(
- Log::Kind);
-
- RTPS_DllAPI static Log::Kind GetVerbosity();
-
- RTPS_DllAPI static void SetCategoryFilter(
- const std::regex&);
-
- RTPS_DllAPI static void SetFilenameFilter(
- const std::regex&);
-
- RTPS_DllAPI static void SetErrorStringFilter(
- const std::regex&);
-
- RTPS_DllAPI static void Reset();
-
- RTPS_DllAPI static void Flush();
-
- RTPS_DllAPI static void KillThread();
-
-
- struct Context
- {
- const char* filename;
- int line;
- const char* function;
- const char* category;
- };
- struct Entry
- {
- std::string message;
- Log::Context context;
- Log::Kind kind;
- std::string timestamp;
- };
-
- RTPS_DllAPI static void QueueLog(
- const std::string& message,
- const Log::Context&,
- Log::Kind);
- private:
- struct Resources
- {
- fastrtps::DBQueue<Entry> logs;
- std::vector<std::unique_ptr<LogConsumer> > consumers;
- std::unique_ptr<std::thread> logging_thread;
-
- std::condition_variable cv;
- std::mutex cv_mutex;
- bool logging;
- bool work;
- int current_loop;
-
- std::mutex config_mutex;
- bool filenames;
- bool functions;
- std::unique_ptr<std::regex> category_filter;
- std::unique_ptr<std::regex> filename_filter;
- std::unique_ptr<std::regex> error_string_filter;
- std::atomic<Log::Kind> verbosity;
- Resources();
- ~Resources();
- };
- static struct Resources resources_;
-
-
-
- static bool preprocess(
- Entry&);
- static void run();
- static void get_timestamp(
- std::string&);
- };
- class LogConsumer
- {
- public:
- virtual ~LogConsumer()
- {
- }
- virtual void Consume(
- const Log::Entry&) = 0;
- protected:
- void print_timestamp(
- std::ostream& stream,
- const Log::Entry&,
- bool color) const;
- void print_header(
- std::ostream& stream,
- const Log::Entry&,
- bool color) const;
- void print_context(
- std::ostream& stream,
- const Log::Entry&,
- bool color) const;
- void print_message(
- std::ostream& stream,
- const Log::Entry&,
- bool color) const;
- void print_new_line(
- std::ostream& stream,
- bool color) const;
- };
- #if defined(WIN32)
- #define __func__ __FUNCTION__
- #endif
- #ifndef LOG_NO_ERROR
- #define logError_(cat, msg) \
- { \
- using namespace eprosima::fastdds::dds; \
- std::stringstream ss; \
- ss << msg; \
- Log::QueueLog(ss.str(), Log::Context{__FILE__, __LINE__, __func__, #cat}, Log::Kind::Error); \
- }
- #elif (defined(__INTERNALDEBUG) || defined(_INTERNALDEBUG))
- #define logError_(cat, msg) \
- { \
- auto tmp_lambda = [&]() \
- { \
- std::stringstream ss; \
- ss << msg; \
- }; \
- (void)tmp_lambda; \
- }
- #else
- #define logError_(cat, msg)
- #endif
- #ifndef LOG_NO_WARNING
- #define logWarning_(cat, msg) \
- { \
- using namespace eprosima::fastdds::dds; \
- if (Log::GetVerbosity() >= Log::Kind::Warning) \
- { \
- std::stringstream ss; \
- ss << msg; \
- Log::QueueLog(ss.str(), Log::Context{__FILE__, __LINE__, __func__, #cat}, Log::Kind::Warning); \
- } \
- }
- #elif (defined(__INTERNALDEBUG) || defined(_INTERNALDEBUG))
- #define logWarning_(cat, msg) \
- { \
- auto tmp_lambda = [&]() \
- { \
- std::stringstream ss; \
- ss << msg; \
- }; \
- (void)tmp_lambda; \
- }
- #else
- #define logWarning_(cat, msg)
- #endif
- #if (defined(__INTERNALDEBUG) || defined(_INTERNALDEBUG)) && (defined(_DEBUG) || defined(__DEBUG)) && \
- (!defined(LOG_NO_INFO))
- #define logInfo_(cat, msg) \
- { \
- using namespace eprosima::fastdds::dds; \
- if (Log::GetVerbosity() >= Log::Kind::Info) \
- { \
- std::stringstream ss; \
- ss << msg; \
- Log::QueueLog(ss.str(), Log::Context{__FILE__, __LINE__, __func__, #cat}, Log::Kind::Info); \
- } \
- }
- #elif (defined(__INTERNALDEBUG) || defined(_INTERNALDEBUG))
- #define logInfo_(cat, msg) \
- { \
- auto tmp_lambda = [&]() \
- { \
- std::stringstream ss; \
- ss << msg; \
- }; \
- (void)tmp_lambda; \
- }
- #else
- #define logInfo_(cat, msg)
- #endif
- }
- }
- }
- #endif
|