Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/logs_doc.doxy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::unbindThreadName
*
* @var mlog::__details::__Static_declarer::MUTEX
* A mutex to guaranty mutual exclusion for logging.
* Only if multithreading explicitly enabled (-pthread or -fopenmp).
Expand Down
66 changes: 60 additions & 6 deletions logs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
#include <string>
#include <utility>
#include <vector>
#include <map>

// 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 <thread>
# include <mutex>
# 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
Expand All @@ -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) \
Expand Down Expand Up @@ -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<MTL_THREAD_ID, std::string>& threads_names
# endif // MTL_LOG_WITH_THREADS
)
{
for(const auto& p : this->chunks)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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<MTL_THREAD_ID, std::string> THREAD_NAME;
# endif
private:
# ifdef MTL_LOG_WITH_THREADS
static std::mutex MUTEX;
Expand Down Expand Up @@ -220,6 +248,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<MTL_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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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...);
}
Expand Down