|
9 | 9 | #ifndef LLDB_TOOLS_LLDB_DAP_DAPLOG_H |
10 | 10 | #define LLDB_TOOLS_LLDB_DAP_DAPLOG_H |
11 | 11 |
|
| 12 | +#include "llvm/ADT/StringRef.h" |
12 | 13 | #include "llvm/Support/Error.h" |
13 | 14 | #include "llvm/Support/FormatAdapters.h" |
14 | 15 | #include "llvm/Support/FormatVariadic.h" |
15 | | -#include <chrono> |
16 | | -#include <fstream> |
| 16 | +#include "llvm/Support/raw_ostream.h" |
| 17 | +#include <mutex> |
17 | 18 | #include <string> |
| 19 | +#include <system_error> |
18 | 20 |
|
19 | 21 | // Write a message to log, if logging is enabled. |
20 | 22 | #define DAP_LOG(log, ...) \ |
21 | 23 | do { \ |
22 | | - ::std::ofstream *log_private = (log); \ |
| 24 | + ::lldb_dap::Log *log_private = (log); \ |
23 | 25 | if (log_private) { \ |
24 | | - ::std::chrono::duration<double> now{ \ |
25 | | - ::std::chrono::system_clock::now().time_since_epoch()}; \ |
26 | | - *log_private << ::llvm::formatv("{0:f9} ", now.count()).str() \ |
27 | | - << ::llvm::formatv(__VA_ARGS__).str() << std::endl; \ |
| 26 | + log_private->WriteMessage(::llvm::formatv(__VA_ARGS__).str()); \ |
28 | 27 | } \ |
29 | 28 | } while (0) |
30 | 29 |
|
31 | 30 | // Write message to log, if error is set. In the log message refer to the error |
32 | 31 | // with {0}. Error is cleared regardless of whether logging is enabled. |
33 | 32 | #define DAP_LOG_ERROR(log, error, ...) \ |
34 | 33 | do { \ |
35 | | - ::std::ofstream *log_private = (log); \ |
| 34 | + ::lldb_dap::Log *log_private = (log); \ |
36 | 35 | ::llvm::Error error_private = (error); \ |
37 | 36 | if (log_private && error_private) { \ |
38 | | - ::std::chrono::duration<double> now{ \ |
39 | | - std::chrono::system_clock::now().time_since_epoch()}; \ |
40 | | - *log_private << ::llvm::formatv("{0:f9} ", now.count()).str() \ |
41 | | - << ::lldb_dap::FormatError(::std::move(error_private), \ |
42 | | - __VA_ARGS__) \ |
43 | | - << std::endl; \ |
| 37 | + log_private->WriteMessage( \ |
| 38 | + ::lldb_dap::FormatError(::std::move(error_private), __VA_ARGS__)); \ |
44 | 39 | } else \ |
45 | 40 | ::llvm::consumeError(::std::move(error_private)); \ |
46 | 41 | } while (0) |
47 | 42 |
|
48 | 43 | namespace lldb_dap { |
49 | 44 |
|
| 45 | +/// Log manages the lldb-dap log file, used with the corresponding `DAP_LOG` and |
| 46 | +/// `DAP_LOG_ERROR` helpers. |
| 47 | +class Log final { |
| 48 | +public: |
| 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 | + |
| 54 | +private: |
| 55 | + std::mutex m_mutex; |
| 56 | + llvm::raw_fd_ostream m_stream; |
| 57 | +}; |
| 58 | + |
50 | 59 | template <typename... Args> |
51 | 60 | inline auto FormatError(llvm::Error error, const char *format, Args &&...args) { |
52 | 61 | return llvm::formatv(format, llvm::toString(std::move(error)), |
|
0 commit comments