-
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.
Introduce a real logging library (#290)
### Issue Follow on from #176 #160 #140 ### Description * Adds a real logging library [spdlog](https://github.com/gabime/spdlog) to replace the homegrown logger.hpp * Lazy initializes * Supports logging to console (stderr) and/or disk * Not integrated yet (does not yet replace logger.hpp) -- want feedback from the team prior to replacing existing log statements ### List of the changes * Introduces new logging API * Adds tests and performance characterization code ### Testing * Unit tests ### API Changes There are no public API changes in this PR.
- Loading branch information
1 parent
79df609
commit f3bd693
Showing
9 changed files
with
460 additions
and
19 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
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
Oops, something went wrong.