From bc5075c2c5da6d4b6895c51c101c64ee44bc6fc3 Mon Sep 17 00:00:00 2001 From: Sarbojit Sarkar Date: Thu, 11 Jun 2020 05:33:40 -0400 Subject: [PATCH] Added file logging for rocclr & HIP Change-Id: Ic0a54f6ee82d010b011739e0059778ed31833518 [ROCm/clr commit: 5f055d227dd8e242275dc3bb3229088d265304ff] --- projects/clr/rocclr/utils/debug.cpp | 21 ++++++++++++++++++++- projects/clr/rocclr/utils/flags.hpp | 2 ++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/projects/clr/rocclr/utils/debug.cpp b/projects/clr/rocclr/utils/debug.cpp index 20479c8572..5e126ac2cb 100644 --- a/projects/clr/rocclr/utils/debug.cpp +++ b/projects/clr/rocclr/utils/debug.cpp @@ -26,6 +26,7 @@ #include "utils/flags.hpp" #endif +#include #include #include #include @@ -80,6 +81,15 @@ void log_timestamped(LogLevel level, const char* file, int line, const char* mes #endif } +FILE* g_pLogFile = nullptr; +std::once_flag g_iLogFileInitialized; + +void createLogFile() { + if (!flagIsDefault(AMD_LOG_FILE) && g_pLogFile == nullptr) { + g_pLogFile = fopen("rocclr-trace.log", "w"); + } +} + void log_printf(LogLevel level, const char* file, int line, const char* format, ...) { va_list ap; @@ -88,7 +98,16 @@ void log_printf(LogLevel level, const char* file, int line, const char* format, vsnprintf(message, sizeof(message), format, ap); va_end(ap); - fprintf(stderr, ":%d:%-25s:%-4d: %010lld: %s\n", level, file, line, Os::timeNanos() / 100ULL, message); + if (!flagIsDefault(AMD_LOG_FILE)) { + std::call_once(amd::g_iLogFileInitialized, amd::createLogFile); + + if (g_pLogFile) { + fprintf(g_pLogFile, ":%d:%-25s:%-4d: %010lld: %s\n", level, file, line, Os::timeNanos() / 100ULL, message); + } + } + else { + fprintf(stderr, ":%d:%-25s:%-4d: %010lld: %s\n", level, file, line, Os::timeNanos() / 100ULL, message); + } } } // namespace amd diff --git a/projects/clr/rocclr/utils/flags.hpp b/projects/clr/rocclr/utils/flags.hpp index da982e8892..ca7e23ef4c 100644 --- a/projects/clr/rocclr/utils/flags.hpp +++ b/projects/clr/rocclr/utils/flags.hpp @@ -26,6 +26,8 @@ \ release(int, AMD_LOG_LEVEL, 0, \ "The default log level") \ +release(bool, AMD_LOG_FILE, false, \ + "By default log file is disabled") \ release(uint, AMD_LOG_MASK, 0X7FFFFFFF, \ "The mask to enable specific kinds of logs") \ debug(uint, DEBUG_GPU_FLAGS, 0, \