-
Notifications
You must be signed in to change notification settings - Fork 1
Thread name #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Thread name #1
Changes from 4 commits
fc025a9
e389145
7cea2a3
e9a5790
c006aee
4171594
dbc86b8
a0470c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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::unbidThreadName | ||
| * | ||
| * @fn void mlog::Options::unbidThreadName(const std::thread::id& id) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A n is missing right here, (unbid instead of unbind), please add it in order to get consistent documentation. |
||
| * 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::bidThreadName | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bid instead of bind |
||
| */ | ||
|
|
||
| // Developpers part, enable HIDE_THIS_DOXYGEN to see it | ||
|
|
@@ -411,6 +426,11 @@ | |
| * @var mlog::__details::__Static_declarer::FORMAT | ||
| * The header format (<b>"[{TYPE} {DATE}] : "</b> 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::unbidThreadName | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here again. |
||
| * | ||
| * @var mlog::__details::__Static_declarer::MUTEX | ||
| * A mutex to guaranty mutual exclusion for logging. | ||
| * Only if multithreading explicitly enabled (-pthread or -fopenmp). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
| #include <map> | ||
|
|
||
| // if -pthread or -fopenmp provided only | ||
| #ifdef _REENTRANT | ||
|
|
@@ -85,8 +86,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, const void* threads_names) | ||
| { | ||
| # ifdef _REENTRANT | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MTL_LOG_WITh_THREADS |
||
| const std::map<std::thread::id, std::string>* thread = reinterpret_cast<const std::map<std::thread::id, std::string>*>(threads_names); | ||
| # endif | ||
| for(const auto& p : this->chunks) | ||
| { | ||
| switch(p.first) | ||
|
|
@@ -110,8 +114,15 @@ namespace MTL_LOG_NAMESPACE | |
| break; | ||
| } | ||
| case -3: | ||
| # ifdef MTL_LOG_WITH_THREADS | ||
| out << "0x" << std::hex << std::this_thread::get_id() << std::dec; | ||
| # ifdef _REENTRANT | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use MTL_LOG_WITH_THREADS macro instead of _REENTRANT. |
||
| try | ||
| { | ||
| out << (*thread).at(std::this_thread::get_id()); | ||
| } | ||
| catch (const std::out_of_range&) | ||
| { | ||
| out << "0x" << std::hex << std::this_thread::get_id() << std::dec; | ||
| } | ||
| # endif | ||
| break; | ||
| case -2: | ||
|
|
@@ -191,6 +202,9 @@ namespace MTL_LOG_NAMESPACE | |
| static bool ENABLE_ALPHA_BOOL; | ||
| static MTL_LOG_NAMESPACE::__details::__Header FORMAT; | ||
|
|
||
| # ifdef _REENTRANT | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MTL_LOG_WITH_THREADS instead of _REENTRANT |
||
| static std::map<std::thread::id, std::string> THREAD_NAME; | ||
| # endif | ||
| private: | ||
| # ifdef MTL_LOG_WITH_THREADS | ||
| static std::mutex MUTEX; | ||
|
|
@@ -220,6 +234,7 @@ namespace MTL_LOG_NAMESPACE | |
| STATIC_DECLARATION(bool, ENABLE_ALPHA_BOOL, true) | ||
| # ifdef MTL_LOG_WITH_THREADS | ||
| template<typename T> std::mutex __Static_declarer<T>::MUTEX; | ||
| template<typename T> std::map<std::thread::id, std::string> __Static_declarer<T>::THREAD_NAME = {}; | ||
| # endif | ||
| STATIC_DECLARATION(MTL_LOG_NAMESPACE::__details::__Header, FORMAT, std::string("[{TYPE} {DATE} {TIME}] : ")) | ||
| # undef STATIC_DECLARATION | ||
|
|
@@ -277,6 +292,16 @@ namespace MTL_LOG_NAMESPACE | |
| MTL_LOG_LOCK; | ||
| return MTL_LOG_NAMESPACE::Options::FORMAT; | ||
| } | ||
| # ifdef _REENTRANT | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MTL_LOG_WITH_THREADS |
||
| static void bindThreadName(const std::thread::id& id, const std::string& name) | ||
| { | ||
| MTL_LOG_NAMESPACE::Options::THREAD_NAME.insert(std::make_pair(id, name)); | ||
| } | ||
| static void unbidThreadName(const std::thread::id& id) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unbid instead of unbind |
||
| { | ||
| MTL_LOG_NAMESPACE::Options::THREAD_NAME.erase(id); | ||
| } | ||
| # endif | ||
| }; | ||
|
|
||
| # undef MTL_LOG_GET_SET | ||
|
|
@@ -329,7 +354,13 @@ 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 _REENTRANT | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MTL_LOG_WITH_THREADS |
||
| &MTL_LOG_NAMESPACE::Options::THREAD_NAME | ||
| # else | ||
| nullptr | ||
| # endif | ||
| ); | ||
| } | ||
| MTL_LOG_NAMESPACE::__details::_Logger::_print_(args...); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same story here, unbid instead of unbind.