e50ce19519
Change-Id: I2bbe9085d607e9d8d5acda1ed43e3245335d239f
[ROCm/clr commit: 9e6e77b7dd]
129 lines
4.3 KiB
C++
129 lines
4.3 KiB
C++
/* Copyright (c) 2008 - 2021 Advanced Micro Devices, Inc.
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE. */
|
|
|
|
#include "top.hpp"
|
|
#include "utils/debug.hpp"
|
|
#include "os/os.hpp"
|
|
|
|
#if !defined(AMD_LOG_LEVEL)
|
|
#include "utils/flags.hpp"
|
|
#endif
|
|
|
|
#include <cstdlib>
|
|
#include <cstdio>
|
|
#include <cstdarg>
|
|
#include <thread>
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif // _WIN32
|
|
|
|
namespace amd {
|
|
|
|
FILE* outFile = stderr;
|
|
|
|
// ================================================================================================
|
|
void report_warning(const char* message) { fprintf(outFile, "Warning: %s\n", message); }
|
|
|
|
// ================================================================================================
|
|
void log_entry(LogLevel level, const char* file, int line, const char* message) {
|
|
if (level == LOG_NONE) {
|
|
return;
|
|
}
|
|
fprintf(outFile, ":%d:%s:%d: %s\n", level, file, line, message);
|
|
fflush(outFile);
|
|
}
|
|
|
|
// ================================================================================================
|
|
void log_timestamped(LogLevel level, const char* file, int line, const char* message) {
|
|
static bool gotstart = false; // not thread-safe, but not scary if fails
|
|
static uint64_t start;
|
|
|
|
if (!gotstart) {
|
|
start = Os::timeNanos();
|
|
gotstart = true;
|
|
}
|
|
|
|
uint64_t time = Os::timeNanos() - start;
|
|
if (level == LOG_NONE) {
|
|
return;
|
|
}
|
|
#if 0
|
|
fprintf(outFile, ":%d:%s:%d: (%010lld) %s\n", level, file, line, time, message);
|
|
#else // if you prefer fixed-width fields
|
|
fprintf(outFile, ":% 2d:%15s:% 5d: (%010lld) us %s\n", level, file, line, time / 1000ULL,
|
|
message);
|
|
#endif
|
|
fflush(outFile);
|
|
}
|
|
|
|
// ================================================================================================
|
|
void log_printf(LogLevel level, const char* file, int line, const char* format, ...) {
|
|
va_list ap;
|
|
std::stringstream pidtid;
|
|
if (AMD_LOG_LEVEL > 4) {
|
|
pidtid << "[pid:" << Os::getProcessId() << " tid: " ;
|
|
pidtid << std::hex << std::setw(5) << std::this_thread::get_id() << "]";
|
|
}
|
|
|
|
va_start(ap, format);
|
|
char message[4096];
|
|
vsnprintf(message, sizeof(message), format, ap);
|
|
va_end(ap);
|
|
uint64_t timeUs = Os::timeNanos() / 1000ULL;
|
|
|
|
fprintf(outFile, ":%d:%-25s:%-4d: %010lud us: %s %s\n", level, file, line,
|
|
timeUs, pidtid.str().c_str(),message);
|
|
|
|
fflush(outFile);
|
|
}
|
|
|
|
// ================================================================================================
|
|
void log_printf(LogLevel level, const char* file, int line, uint64_t* start,
|
|
const char* format, ...) {
|
|
va_list ap;
|
|
std::stringstream pidtid;
|
|
if (AMD_LOG_LEVEL > 4) {
|
|
pidtid << "[pid:" << Os::getProcessId() << " tid: " ;
|
|
pidtid << std::hex << std::setw(5) << std::this_thread::get_id() << "]";
|
|
}
|
|
va_start(ap, format);
|
|
char message[4096];
|
|
vsnprintf(message, sizeof(message), format, ap);
|
|
va_end(ap);
|
|
uint64_t timeUs = Os::timeNanos() / 1000ULL;
|
|
|
|
if (start == 0 || *start == 0) {
|
|
fprintf(outFile, ":%d:%-25s:%-4d: %010lud us: %s %s\n", level, file, line,
|
|
timeUs, pidtid.str().c_str(), message);
|
|
} else {
|
|
fprintf(outFile, ":%d:%-25s:%-4d: %010lud us: %s %s: duration: %lud us\n",
|
|
level, file, line, timeUs, pidtid.str().c_str(), message, timeUs - *start);
|
|
}
|
|
fflush(outFile);
|
|
if (*start == 0) {
|
|
*start = timeUs;
|
|
}
|
|
}
|
|
|
|
} // namespace amd
|