blob: c293d15c011e884a23514e4e5d342857eae0d5a5 [file] [log] [blame]
// 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/fuchsia_syslog.h>
#include <lib/zx/result.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include <vector>
namespace driver_logger {
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, ...);
} // namespace internal
} // namespace driver_logger
#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(driver_logger::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(driver_logger::GetLogger(), \
driver_logger::internal::severity_from_verbosity(verbosity), tag, message)
#define FX_LOGF_INTERNAL(logger, severity, tag, message...) \
do { \
driver_logger::internal::log_with_source(logger, (severity), (tag), __FILE__, __LINE__, \
message); \
} while (0)
namespace driver_logger {
zx_koid_t GetCurrentThread();
class Logger {
public:
// Initializes a logger.
// The logger does not take ownership of the socket and the caller is responsible for
// freeing it. This allows multiple loggers to be multiplexed over a single socket.
Logger(zx_koid_t pid, zx::socket socket) : pid_(pid), socket_(std::move(socket)) {}
// Sets the logger's minimum severity. Logs that are less severe
// than this will not be logged.
Logger& SetSeverity(FuchsiaLogSeverity severity) {
severity_ = severity;
return *this;
}
// Adds a global tag to the logger. Global tags will automatically
// be included with each log generated by this logger.
Logger& AddTag(const std::string& tag) {
tags_.push_back(tag);
return *this;
}
// Gets the minimum severity of the logger
FuchsiaLogSeverity GetSeverity() const { return severity_; }
// Begins a record, used by internal macros only.
void BeginRecord(fuchsia_syslog::LogBuffer& buffer, FuchsiaLogSeverity severity,
cpp17::optional<cpp17::string_view> file_name, unsigned int line,
cpp17::optional<cpp17::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;
// Flushes a log record to the socket, used by internal macros only.
void FlushRecord(fuchsia_syslog::LogBuffer& buffer, FuchsiaLogSeverity severity) const;
// Sets the socket to log to, used primarily for testing purposes.
void SetSocket(zx::socket socket) { socket_ = std::move(socket); }
private:
FuchsiaLogSeverity severity_ = FUCHSIA_LOG_INFO;
zx_koid_t pid_;
std::vector<std::string> tags_;
zx::socket socket_;
};
// Creates a logger.
zx::result<Logger> CreateLogger();
} // namespace driver_logger
#endif // SRC_DEVICES_LIB_LOG_LOG_H_