68 lines · c
1//===-- DAPLog.h ----------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLDB_TOOLS_LLDB_DAP_DAPLOG_H10#define LLDB_TOOLS_LLDB_DAP_DAPLOG_H11 12#include "llvm/ADT/StringRef.h"13#include "llvm/Support/Error.h"14#include "llvm/Support/FormatAdapters.h"15#include "llvm/Support/FormatVariadic.h"16#include "llvm/Support/raw_ostream.h"17#include <mutex>18#include <string>19#include <system_error>20 21// Write a message to log, if logging is enabled.22#define DAP_LOG(log, ...) \23 do { \24 ::lldb_dap::Log *log_private = (log); \25 if (log_private) { \26 log_private->WriteMessage(::llvm::formatv(__VA_ARGS__).str()); \27 } \28 } while (0)29 30// Write message to log, if error is set. In the log message refer to the error31// with {0}. Error is cleared regardless of whether logging is enabled.32#define DAP_LOG_ERROR(log, error, ...) \33 do { \34 ::lldb_dap::Log *log_private = (log); \35 ::llvm::Error error_private = (error); \36 if (log_private && error_private) { \37 log_private->WriteMessage( \38 ::lldb_dap::FormatError(::std::move(error_private), __VA_ARGS__)); \39 } else \40 ::llvm::consumeError(::std::move(error_private)); \41 } while (0)42 43namespace lldb_dap {44 45/// Log manages the lldb-dap log file, used with the corresponding `DAP_LOG` and46/// `DAP_LOG_ERROR` helpers.47class Log final {48public:49 /// Creates a log file with the given filename.50 Log(llvm::StringRef filename, std::error_code &EC);51 52 void WriteMessage(llvm::StringRef message);53 54private:55 std::mutex m_mutex;56 llvm::raw_fd_ostream m_stream;57};58 59template <typename... Args>60inline auto FormatError(llvm::Error error, const char *format, Args &&...args) {61 return llvm::formatv(format, llvm::toString(std::move(error)),62 std::forward<Args>(args)...)63 .str();64}65} // namespace lldb_dap66 67#endif68