blob: 6d06607e760c0393a2cc935c777a6c45f987edb6 [file]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SRC_DEVICES_LIB_LOG_LOG_H_
#define SRC_DEVICES_LIB_LOG_LOG_H_
#include <lib/syslog/structured_backend/cpp/log_buffer.h>
#include <lib/syslog/structured_backend/cpp/logger.h>
#include <lib/zx/result.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <zircon/availability.h>
#if FUCHSIA_API_LEVEL_AT_LEAST(PLATFORM)
#include <span>
#endif
#include <string>
#include <utility>
#if FUCHSIA_API_LEVEL_AT_LEAST(HEAD) && __cplusplus >= 202002L
#include <format>
#include <source_location>
#endif
#include <print>
namespace fdf_log {
class Logger;
Logger& GetLogger();
namespace internal {
FuchsiaLogSeverity severity_from_verbosity(uint8_t verbosity);
void log_with_source(Logger& logger, FuchsiaLogSeverity severity, const char* tag, const char* file,
int line, const char* format, ...);
const char* SeverityToString(FuchsiaLogSeverity severity);
} // namespace internal
} // namespace fdf_log
#define LOGF(severity, message...) FX_LOGF(severity, nullptr, message)
#define VLOGF(verbosity, message...) FX_VLOGF(verbosity, nullptr, message)
// Writes formatted message to the global logger.
// |severity| is one of TRACE, DEBUG, INFO, WARNING, ERROR, FATAL
// |tag| is a tag to associated with the message, or NULL if none.
// |message| is the message to write, or NULL if none.
#define FX_LOGF(severity, tag, message...) \
FX_LOGF_INTERNAL(fdf_log::GetLogger(), (FUCHSIA_LOG_##severity), tag, message)
// Writes formatted verbose message to the global logger.
// |verbosity| is an integer value > 0 up to a maximum of 15.
// |tag| is a tag to associated with the message, or NULL if none.
// |message| is the message to write, or NULL if none.
#define FX_VLOGF(verbosity, tag, message...) \
FX_LOGF_INTERNAL(fdf_log::GetLogger(), fdf_log::internal::severity_from_verbosity(verbosity), \
tag, message)
#define FX_LOGF_INTERNAL(logger, severity, tag, message...) \
do { \
fdf_log::internal::log_with_source(logger, (severity), (tag), __FILE__, __LINE__, message); \
} while (0)
namespace fdf_log {
zx_koid_t GetCurrentThread();
class Logger {
public:
// Initializes a logger.
Logger(zx_koid_t pid, fuchsia_logging::Logger logger) : pid_(pid), logger_(std::move(logger)) {}
void SwitchToStdout() { use_stdout_ = true; }
// Gets the minimum severity of the logger
FuchsiaLogSeverity GetSeverity() const { return logger_.GetMinSeverity(); }
// Begins a record, used by internal macros only.
void BeginRecord(fuchsia_logging::LogBuffer& buffer, FuchsiaLogSeverity severity,
std::optional<std::string_view> file_name, unsigned int line,
std::optional<std::string_view> msg) const;
// Writes a log message, used by internal macros only.
void VLogWrite(FuchsiaLogSeverity severity, const char* tag, const char* msg, va_list args,
const char* file = nullptr, uint32_t line = 0) const;
#if FUCHSIA_API_LEVEL_AT_LEAST(HEAD) && __cplusplus >= 202002L
template <typename... Args>
void log(FuchsiaLogSeverity severity, const std::source_location& loc,
std::format_string<Args...> fmt, Args&&... args) {
if (severity < GetSeverity()) {
return;
}
if (use_stdout_) {
// We rely on line buffering to ensure this is a single syscall.
std::print("[driver_manager.cm]: {}: ", internal::SeverityToString(severity));
std::print(fmt, std::forward<Args>(args)...);
fputc('\n', stdout);
return;
}
LogInternal(severity, nullptr, loc.file_name(), loc.line(), fmt.get(),
std::make_format_args(args...));
}
#endif
private:
void LogInternal(FuchsiaLogSeverity severity, const char* tag, const char* file, int line,
std::string_view fmt, std::format_args args);
zx_koid_t pid_;
bool use_stdout_ = false;
fuchsia_logging::Logger logger_;
};
#if FUCHSIA_API_LEVEL_AT_LEAST(PLATFORM)
// Initializes the global logger. If the global logger has already been created, then this will
// replace the logger with a new one. This is *not* thread safe.
void InitGlobalLogger(std::span<const char*> tags, FuchsiaLogSeverity severity);
#endif
#if FUCHSIA_API_LEVEL_AT_LEAST(HEAD) && __cplusplus >= 202002L
// Use template type deduction to allow us to get source location while using variadic templates.
template <typename... Args>
struct trace {
trace(std::format_string<Args...> fmt, Args&&... args,
const std::source_location& loc = std::source_location::current()) {
fdf_log::GetLogger().log(FUCHSIA_LOG_TRACE, loc, fmt, std::forward<Args>(args)...);
}
};
template <typename... Args>
trace(std::format_string<Args...>, Args&&...) -> trace<Args...>;
template <typename... Args>
struct debug {
debug(std::format_string<Args...> fmt, Args&&... args,
const std::source_location& loc = std::source_location::current()) {
fdf_log::GetLogger().log(FUCHSIA_LOG_DEBUG, loc, fmt, std::forward<Args>(args)...);
}
};
template <typename... Args>
debug(std::format_string<Args...>, Args&&...) -> debug<Args...>;
template <typename... Args>
struct info {
info(std::format_string<Args...> fmt, Args&&... args,
const std::source_location& loc = std::source_location::current()) {
fdf_log::GetLogger().log(FUCHSIA_LOG_INFO, loc, fmt, std::forward<Args>(args)...);
}
};
template <typename... Args>
info(std::format_string<Args...>, Args&&...) -> info<Args...>;
template <typename... Args>
struct warn {
warn(std::format_string<Args...> fmt, Args&&... args,
const std::source_location& loc = std::source_location::current()) {
fdf_log::GetLogger().log(FUCHSIA_LOG_WARNING, loc, fmt, std::forward<Args>(args)...);
}
};
template <typename... Args>
warn(std::format_string<Args...>, Args&&...) -> warn<Args...>;
template <typename... Args>
struct error {
error(std::format_string<Args...> fmt, Args&&... args,
const std::source_location& loc = std::source_location::current()) {
fdf_log::GetLogger().log(FUCHSIA_LOG_ERROR, loc, fmt, std::forward<Args>(args)...);
}
};
template <typename... Args>
error(std::format_string<Args...>, Args&&...) -> error<Args...>;
template <typename... Args>
struct fatal {
fatal(std::format_string<Args...> fmt, Args&&... args,
const std::source_location& loc = std::source_location::current()) {
fdf_log::GetLogger().log(FUCHSIA_LOG_ERROR, loc, fmt, std::forward<Args>(args)...);
}
};
template <typename... Args>
fatal(std::format_string<Args...>, Args&&...) -> fatal<Args...>;
#endif
} // namespace fdf_log
// For backwards compatibility, also export to driver_logger.
namespace driver_logger {
using namespace fdf_log;
} // namespace driver_logger
#endif // SRC_DEVICES_LIB_LOG_LOG_H_