diff --git a/doc/logs_doc.doxy b/doc/logs_doc.doxy index acd06f4..ea9dae2 100644 --- a/doc/logs_doc.doxy +++ b/doc/logs_doc.doxy @@ -270,6 +270,21 @@ * - {TYPE} To display here the type of the log. * - {DATE} To display here the date of the log. * - {THREAD} To display here the emiter thread number (only if -pthread or -fopenmp). + * + * @fn void mlog::Options::bindThreadName(const std::thread::id& id, const std::string& name) + * Binds the thread id to the name so that when using the tag + * {THREAD} the name given is used instead of an hexadecimal output + * + * @param[in] id The identifier returned by std::thread::get_id() + * @param[in] name Name that should be displayed instead of an id + * @see mlog::Options::unbindThreadName + * + * @fn void mlog::Options::unbindThreadName(const std::thread::id& id) + * Unbinds the thread id if it exists. This way, using the {THREAD} + * tag will print the thread id + * + * @param[in] id The identifier returned by std::thread::get_id() + * @see mlog::Options::bindThreadName */ // Developpers part, enable HIDE_THIS_DOXYGEN to see it @@ -411,6 +426,11 @@ * @var mlog::__details::__Static_declarer::FORMAT * The header format ("[{TYPE} {DATE}] : " by default). * + * @var mlog::__details::__Static_declarer::THREAD_NAME + * The container for bounds between thread id and a string value + * @see mlog::Options::bindThreadName + * @see mlog::Options::unbindThreadName + * * @var mlog::__details::__Static_declarer::MUTEX * A mutex to guaranty mutual exclusion for logging. * Only if multithreading explicitly enabled (-pthread or -fopenmp). diff --git a/logs.hpp b/logs.hpp index 8f01f47..806db0d 100644 --- a/logs.hpp +++ b/logs.hpp @@ -9,14 +9,22 @@ #include #include #include +#include // if -pthread or -fopenmp provided only #ifdef _REENTRANT -# define MTL_LOG_WITH_THREADS + // For now, when using windows there's no multithreading feature +# if !defined _WIN32 && !defined __CYGWIN__ +# define MTL_LOG_WITH_THREADS +# endif #endif #ifdef MTL_LOG_WITH_THREADS # include # include +# define MTL_THREAD_ID std::thread::id +#else + // As Threads are not used here, a fake thread id type can be used safely +# define MTL_THREAD_ID int #endif #ifndef MTL_LOG_NAMESPACE @@ -39,6 +47,12 @@ #define MTL_LOG_VARIABLE(varname) #varname " =", varname +#ifdef MTL_LOG_WITH_THREADS +#define MTL_LOG_IF_WITH_THREAD(code) code +#else +#define MTL_LOG_IF_WITH_THREAD(code) +#endif + namespace MTL_LOG_NAMESPACE { # define MTL_LOG_DECLARE(funcName) \ @@ -85,7 +99,11 @@ namespace MTL_LOG_NAMESPACE return this->pattern; } void display(std::ostream& out, const std::string& type, const char *const color, - const char *const nocolor, bool colorEnabled) + const char *const nocolor, bool colorEnabled +# ifdef MTL_LOG_WITH_THREADS + , const std::map& threads_names +# endif // MTL_LOG_WITH_THREADS + ) { for(const auto& p : this->chunks) { @@ -110,9 +128,16 @@ namespace MTL_LOG_NAMESPACE break; } case -3: -# ifdef MTL_LOG_WITH_THREADS - out << "0x" << std::hex << std::this_thread::get_id() << std::dec; -# endif +# ifdef MTL_LOG_WITH_THREADS + try + { + out << threads_names.at(std::this_thread::get_id()); + } + catch (const std::out_of_range&) + { + out << "0x" << std::hex << std::this_thread::get_id() << std::dec; + } +# endif // MTL_LOG_WITH_THREADS break; case -2: if (colorEnabled) @@ -191,6 +216,9 @@ namespace MTL_LOG_NAMESPACE static bool ENABLE_ALPHA_BOOL; static MTL_LOG_NAMESPACE::__details::__Header FORMAT; +# ifdef MTL_LOG_WITH_THREADS + static std::map THREAD_NAME; +# endif private: # ifdef MTL_LOG_WITH_THREADS static std::mutex MUTEX; @@ -220,6 +248,7 @@ namespace MTL_LOG_NAMESPACE STATIC_DECLARATION(bool, ENABLE_ALPHA_BOOL, true) # ifdef MTL_LOG_WITH_THREADS template std::mutex __Static_declarer::MUTEX; + template std::map __Static_declarer::THREAD_NAME = {}; # endif STATIC_DECLARATION(MTL_LOG_NAMESPACE::__details::__Header, FORMAT, std::string("[{TYPE} {DATE} {TIME}] : ")) # undef STATIC_DECLARATION @@ -277,6 +306,27 @@ namespace MTL_LOG_NAMESPACE MTL_LOG_LOCK; return MTL_LOG_NAMESPACE::Options::FORMAT; } + static void bindThreadName(const std::string& MTL_LOG_IF_WITH_THREAD(name)) + { +# ifdef MTL_LOG_WITH_THREADS + MTL_LOG_LOCK; + MTL_LOG_NAMESPACE::Options::THREAD_NAME.insert(std::make_pair(std::this_thread::get_id(), name)); +# endif + } + static void bindThreadName(const MTL_THREAD_ID& MTL_LOG_IF_WITH_THREAD(id), const std::string& MTL_LOG_IF_WITH_THREAD(name)) + { +# ifdef MTL_LOG_WITH_THREADS + MTL_LOG_LOCK; + MTL_LOG_NAMESPACE::Options::THREAD_NAME.insert(std::make_pair(id, name)); +# endif + } + static void unbindThreadName(const MTL_THREAD_ID& MTL_LOG_IF_WITH_THREAD(id = std::this_thread::get_id())) + { +# ifdef MTL_LOG_WITH_THREADS + MTL_LOG_LOCK; + MTL_LOG_NAMESPACE::Options::THREAD_NAME.erase(id); +# endif + } }; # undef MTL_LOG_GET_SET @@ -329,7 +379,11 @@ namespace MTL_LOG_NAMESPACE MTL_LOG_NAMESPACE::Options::FORMAT.display(*MTL_LOG_NAMESPACE::Options::OUT, tag, color, MTL_LOG_NAMESPACE::Options::C_BLANK, - MTL_LOG_NAMESPACE::Options::isColorEnabled()); + MTL_LOG_NAMESPACE::Options::isColorEnabled() +# ifdef MTL_LOG_WITH_THREADS + , MTL_LOG_NAMESPACE::Options::THREAD_NAME +# endif + ); } MTL_LOG_NAMESPACE::__details::_Logger::_print_(args...); }