From bb98bc7d856d54bc5dd2b64d95d4752ee396526c Mon Sep 17 00:00:00 2001 From: Laurent Morichetti Date: Mon, 12 Sep 2022 13:03:48 -0700 Subject: [PATCH] Clean up logger.h Change-Id: Ibcb58d2236b012d00c3fc421a425c03093de5d50 --- src/roctracer/roctracer.cpp | 13 +---- src/util/logger.h | 95 ++++++++----------------------------- 2 files changed, 22 insertions(+), 86 deletions(-) diff --git a/src/roctracer/roctracer.cpp b/src/roctracer/roctracer.cpp index 3be42de65d..0ccb01d53e 100644 --- a/src/roctracer/roctracer.cpp +++ b/src/roctracer/roctracer.cpp @@ -99,14 +99,6 @@ MemoryPool* default_memory_pool = nullptr; } // namespace -namespace roctracer { - -// Logger routines and primitives -util::Logger::mutex_t util::Logger::mutex_; -std::atomic util::Logger::instance_{}; - -} // namespace roctracer - /////////////////////////////////////////////////////////////////////////////////////////////////// // Public library methods // @@ -117,7 +109,7 @@ ROCTRACER_API uint32_t roctracer_version_minor() { return ROCTRACER_VERSION_MINO // Returns the last error ROCTRACER_API const char* roctracer_error_string() { - return strdup(util::Logger::LastMessage().c_str()); + return strdup(util::Logger::Instance().LastMessage().c_str()); } // Return Op string by given domain and activity/API codes @@ -881,9 +873,6 @@ ROCTRACER_API roctracer_status_t roctracer_set_properties(roctracer_domain_t dom API_METHOD_SUFFIX } -__attribute__((constructor)) void constructor() { util::Logger::Create(); } -__attribute__((destructor)) void destructor() { util::Logger::Destroy(); } - extern "C" { // The HSA_AMD_TOOL_PRIORITY variable must be a constant value type initialized by the loader diff --git a/src/util/logger.h b/src/util/logger.h index daf07a6a7b..0bbdbe799f 100644 --- a/src/util/logger.h +++ b/src/util/logger.h @@ -43,11 +43,9 @@ namespace roctracer::util { class Logger { public: - typedef std::recursive_mutex mutex_t; - - template Logger& operator<<(const T& m) { + template Logger& operator<<(T&& m) { std::ostringstream oss; - oss << m; + oss << std::forward(m); if (!streaming_) Log(oss.str()); else @@ -56,7 +54,7 @@ class Logger { return *this; } - typedef void (*manip_t)(); + using manip_t = void (*)(); Logger& operator<<(manip_t f) { f(); return *this; @@ -65,59 +63,35 @@ class Logger { static void begm() { Instance().ResetStreaming(true); } static void endl() { Instance().ResetStreaming(false); } - static const std::string& LastMessage() { - Logger& logger = Instance(); - std::lock_guard lck(mutex_); - return logger.message_[GetTid()]; - } - - static Logger* Create() { - std::lock_guard lck(mutex_); - Logger* obj = instance_.load(std::memory_order_relaxed); - if (obj == NULL) { - obj = new Logger(); - if (obj == NULL) { - std::cerr << "ROCTracer: log object creation failed" << std::endl << std::flush; - abort(); - } - instance_.store(obj, std::memory_order_release); - } - return obj; - } - - static void Destroy() { - std::lock_guard lck(mutex_); - if (instance_ != NULL) delete instance_.load(); - instance_ = NULL; + const std::string& LastMessage() { + std::lock_guard lock(mutex_); + return message_[GetTid()]; } static Logger& Instance() { - Logger* obj = instance_.load(std::memory_order_acquire); - if (obj == NULL) obj = Create(); - return *obj; + static Logger instance; + return instance; } static uint32_t GetPid() { return syscall(__NR_getpid); } static uint32_t GetTid() { return syscall(__NR_gettid); } private: - Logger() : file_(NULL), dirty_(false), streaming_(false), messaging_(false) { - const char* path = getenv("ROCTRACER_LOG"); - if (path != NULL) { - file_ = fopen("/tmp/roctracer_log.txt", "a"); - } + Logger() : file_(nullptr), dirty_(false), streaming_(false), messaging_(false) { + const char* var = getenv("ROCTRACER_LOG"); + if (var != nullptr) file_ = fopen("/tmp/roctracer_log.txt", "a"); ResetStreaming(false); } ~Logger() { - if (file_ != NULL) { + if (file_ != nullptr) { if (dirty_) Put("\n"); fclose(file_); } } void ResetStreaming(const bool messaging) { - std::lock_guard lck(mutex_); + std::lock_guard lock(mutex_); if (messaging) { message_[GetTid()] = ""; } else if (streaming_) { @@ -129,11 +103,11 @@ class Logger { } void Put(const std::string& m) { - std::lock_guard lck(mutex_); + std::lock_guard lock(mutex_); if (messaging_) { message_[GetTid()] += m; } - if (file_ != NULL) { + if (file_ != nullptr) { dirty_ = true; flock(fileno(file_), LOCK_EX); fprintf(file_, "%s", m.c_str()); @@ -143,7 +117,7 @@ class Logger { } void Log(const std::string& m) { - const time_t rawtime = time(NULL); + const time_t rawtime = time(nullptr); tm tm_info; localtime_r(&rawtime, &tm_info); char tm_str[26]; @@ -158,8 +132,7 @@ class Logger { bool streaming_; bool messaging_; - static mutex_t mutex_; - static std::atomic instance_; + std::recursive_mutex mutex_; std::map message_; }; @@ -170,51 +143,25 @@ class Logger { roctracer::util::Logger::Instance() \ << "fatal: " << roctracer::util::Logger::begm << stream << roctracer::util::Logger::endl; \ abort(); \ - } while (0) + } while (false) #define ERR_LOGGING(stream) \ do { \ roctracer::util::Logger::Instance() \ << "error: " << roctracer::util::Logger::begm << stream << roctracer::util::Logger::endl; \ - } while (0) + } while (false) #define INFO_LOGGING(stream) \ do { \ roctracer::util::Logger::Instance() \ << "info: " << roctracer::util::Logger::begm << stream << roctracer::util::Logger::endl; \ - } while (0) + } while (false) #define WARN_LOGGING(stream) \ do { \ std::cerr << "ROCProfiler: " << stream << std::endl; \ roctracer::util::Logger::Instance() << "warning: " << roctracer::util::Logger::begm << stream \ << roctracer::util::Logger::endl; \ - } while (0) - -#ifdef DEBUG -#define DBG_LOGGING(stream) \ - do { \ - roctracer::util::Logger::Instance() \ - << roctracer::util::Logger::begm << "debug: \"" << stream << "\"" \ - << " in " << __FUNCTION__ << " at " << __FILE__ << " line " << __LINE__ \ - << roctracer::util::Logger::endl; \ - } while (0) -#endif - -#if DEBUG_TRACE_ON -inline static void DEBUG_TRACE(const char* fmt, ...) { - constexpr int size = 256; - char buf[size]; - - va_list valist; - va_start(valist, fmt); - vsnprintf(buf, size, fmt, valist); - printf("%u:%u %s", roctracer::util::Logger::GetPid(), roctracer::util::Logger::GetTid(), buf); - fflush(stdout); - va_end(valist); -} -#else -#define DEBUG_TRACE(...) -#endif + } while (false) #endif // SRC_UTIL_LOGGER_H_