-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f749fc1
commit 7c48bda
Showing
8 changed files
with
467 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* SPDX-FileCopyrightText: (c) 2024 Tenstorrent Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "logger_.hpp" // TODO: rename after logger.hpp is removed | ||
|
||
#include <fmt/format.h> | ||
#include <spdlog/sinks/basic_file_sink.h> | ||
#include <spdlog/sinks/stdout_sinks.h> | ||
|
||
#include <mutex> | ||
|
||
namespace tt::umd::logger { | ||
|
||
void initialize(const Options& options) { | ||
static std::mutex mutex; | ||
std::scoped_lock lock{mutex}; | ||
|
||
if (detail::is_initialized.load(std::memory_order_relaxed)) { | ||
return; | ||
} | ||
|
||
std::vector<spdlog::sink_ptr> sinks; | ||
|
||
if (options.log_to_stderr) { | ||
auto stderr_sink = std::make_shared<spdlog::sinks::stderr_sink_mt>(); | ||
sinks.push_back(stderr_sink); | ||
} | ||
|
||
if (!options.filename.empty()) { | ||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(options.filename); | ||
sinks.push_back(file_sink); | ||
} | ||
|
||
auto logger = std::make_shared<spdlog::logger>("UMD", sinks.begin(), sinks.end()); | ||
logger->set_level(options.log_level); | ||
logger->set_pattern(options.pattern); | ||
|
||
spdlog::set_default_logger(logger); | ||
detail::is_initialized.store(true, std::memory_order_release); | ||
} | ||
|
||
namespace detail { | ||
std::atomic_bool is_initialized = false; | ||
} | ||
|
||
} // namespace tt::umd::logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* SPDX-FileCopyrightText: (c) 2024 Tenstorrent Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
|
||
#define SPDLOG_FMT_EXTERNAL | ||
#include <spdlog/spdlog.h> | ||
|
||
namespace tt::umd::logger { | ||
|
||
/** | ||
* Parameters controlling the behavior of the logger. | ||
*/ | ||
struct Options { | ||
bool log_to_stderr{true}; | ||
std::string filename{}; | ||
std::string pattern{"[%Y-%m-%d %H:%M:%S.%e] [%l] [%s:%#] %v"}; | ||
spdlog::level::level_enum log_level{spdlog::level::debug}; | ||
|
||
// TODO: this can be augmented as needed (log rotation, flush policy...) | ||
}; | ||
|
||
/** | ||
* One-time initialization of the logger. | ||
* | ||
* If you don't call it, the logger will be initialized with default options the | ||
* first time a message is logged. | ||
*/ | ||
void initialize(const Options& options = Options{}); | ||
|
||
/** | ||
* Macros for using the logger. | ||
*/ | ||
#define UMD_TRACE(...) \ | ||
do { \ | ||
::tt::umd::logger::detail::ensure_initialized(); \ | ||
SPDLOG_TRACE(__VA_ARGS__); \ | ||
} while (0) | ||
|
||
#define UMD_DEBUG(...) \ | ||
do { \ | ||
::tt::umd::logger::detail::ensure_initialized(); \ | ||
SPDLOG_DEBUG(__VA_ARGS__); \ | ||
} while (0) | ||
|
||
#define UMD_INFO(...) \ | ||
do { \ | ||
::tt::umd::logger::detail::ensure_initialized(); \ | ||
SPDLOG_INFO(__VA_ARGS__); \ | ||
} while (0) | ||
|
||
#define UMD_WARN(...) \ | ||
do { \ | ||
::tt::umd::logger::detail::ensure_initialized(); \ | ||
SPDLOG_WARN(__VA_ARGS__); \ | ||
} while (0) | ||
|
||
#define UMD_ERROR(...) \ | ||
do { \ | ||
::tt::umd::logger::detail::ensure_initialized(); \ | ||
SPDLOG_ERROR(__VA_ARGS__); \ | ||
} while (0) | ||
|
||
#define UMD_CRITICAL(...) \ | ||
do { \ | ||
::tt::umd::logger::detail::ensure_initialized(); \ | ||
SPDLOG_CRITICAL(__VA_ARGS__); \ | ||
} while (0) | ||
|
||
/** | ||
* This is not part of the API. | ||
*/ | ||
namespace detail { | ||
extern std::atomic_bool is_initialized; | ||
|
||
inline void ensure_initialized() { | ||
if (!is_initialized.load(std::memory_order_acquire)) { | ||
initialize(); | ||
} | ||
} | ||
|
||
} // namespace detail | ||
|
||
} // namespace tt::umd::logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* SPDX-FileCopyrightText: (c) 2024 Tenstorrent Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <chrono> | ||
#include <string> | ||
|
||
namespace tt::umd::util { | ||
|
||
class Timestamp { | ||
std::chrono::steady_clock::time_point start; | ||
|
||
public: | ||
Timestamp() : start(std::chrono::steady_clock::now()) {} | ||
|
||
void reset() { start = std::chrono::steady_clock::now(); } | ||
|
||
uint64_t nanoseconds() const { | ||
auto now = std::chrono::steady_clock::now(); | ||
return std::chrono::duration_cast<std::chrono::nanoseconds>(now - start).count(); | ||
} | ||
|
||
uint64_t microseconds() const { | ||
auto now = std::chrono::steady_clock::now(); | ||
return std::chrono::duration_cast<std::chrono::microseconds>(now - start).count(); | ||
} | ||
|
||
uint64_t milliseconds() const { | ||
auto now = std::chrono::steady_clock::now(); | ||
return std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count(); | ||
} | ||
|
||
uint64_t seconds() const { | ||
auto now = std::chrono::steady_clock::now(); | ||
return std::chrono::duration_cast<std::chrono::seconds>(now - start).count(); | ||
} | ||
|
||
std::string to_string() const { | ||
auto ns = nanoseconds(); | ||
if (ns < 1000) { | ||
return fmt::format("{} ns", ns); | ||
} | ||
auto us = microseconds(); | ||
if (us < 1000) { | ||
return fmt::format("{} μs", us); | ||
} | ||
auto ms = milliseconds(); | ||
if (ms < 1000) { | ||
return fmt::format("{} ms", ms); | ||
} | ||
return fmt::format("{} s", seconds()); | ||
} | ||
}; | ||
|
||
} // namespace tt::umd::util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
set(MISC_TESTS_SRCS test_logger.cpp) | ||
|
||
add_executable(misc_tests ${MISC_TESTS_SRCS}) | ||
target_link_libraries( | ||
misc_tests | ||
PRIVATE | ||
test_common | ||
spdlog::spdlog_header_only | ||
) | ||
set_target_properties( | ||
misc_tests | ||
PROPERTIES | ||
RUNTIME_OUTPUT_DIRECTORY | ||
${CMAKE_BINARY_DIR}/test/ | ||
OUTPUT_NAME | ||
misc_tests | ||
) |
Oops, something went wrong.