Skip to content

Commit 2b37022

Browse files
committed
With the helper type we can move the timestamp logic into the Log::WriteMessage call instead of having it in the macro.
1 parent 8d66eb2 commit 2b37022

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

lldb/tools/lldb-dap/DAPLog.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@
88

99
#include "DAPLog.h"
1010
#include "llvm/ADT/StringRef.h"
11+
#include "llvm/Support/raw_ostream.h"
12+
#include <chrono>
1113
#include <fstream>
1214
#include <mutex>
15+
#include <system_error>
1316

1417
using namespace llvm;
1518

1619
namespace lldb_dap {
1720

18-
Log::Log(StringRef filename) : m_stream(std::ofstream(filename.str())) {}
21+
Log::Log(StringRef filename, std::error_code &EC) : m_stream(filename, EC) {}
1922

2023
void Log::WriteMessage(StringRef message) {
2124
std::scoped_lock<std::mutex> lock(m_mutex);
22-
m_stream << message.str();
25+
std::chrono::duration<double> now{
26+
std::chrono::system_clock::now().time_since_epoch()};
27+
m_stream << formatv("{0:f9} ", now.count()).str() << message << "\n";
2328
m_stream.flush();
2429
}
2530

lldb/tools/lldb-dap/DAPLog.h

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,17 @@
1313
#include "llvm/Support/Error.h"
1414
#include "llvm/Support/FormatAdapters.h"
1515
#include "llvm/Support/FormatVariadic.h"
16-
#include <chrono>
17-
#include <fstream>
16+
#include "llvm/Support/raw_ostream.h"
1817
#include <mutex>
1918
#include <string>
19+
#include <system_error>
2020

2121
// Write a message to log, if logging is enabled.
2222
#define DAP_LOG(log, ...) \
2323
do { \
2424
::lldb_dap::Log *log_private = (log); \
2525
if (log_private) { \
26-
::std::chrono::duration<double> now{ \
27-
::std::chrono::system_clock::now().time_since_epoch()}; \
28-
::std::string out; \
29-
::llvm::raw_string_ostream os(out); \
30-
os << ::llvm::formatv("{0:f9} ", now.count()).str() \
31-
<< ::llvm::formatv(__VA_ARGS__).str() << "\n"; \
32-
log_private->WriteMessage(out); \
26+
log_private->WriteMessage(::llvm::formatv(__VA_ARGS__).str()); \
3327
} \
3428
} while (0)
3529

@@ -40,14 +34,8 @@
4034
::lldb_dap::Log *log_private = (log); \
4135
::llvm::Error error_private = (error); \
4236
if (log_private && error_private) { \
43-
::std::chrono::duration<double> now{ \
44-
std::chrono::system_clock::now().time_since_epoch()}; \
45-
::std::string out; \
46-
::llvm::raw_string_ostream os(out); \
47-
os << ::llvm::formatv("{0:f9} ", now.count()).str() \
48-
<< ::lldb_dap::FormatError(::std::move(error_private), __VA_ARGS__) \
49-
<< "\n"; \
50-
log_private->WriteMessage(out); \
37+
log_private->WriteMessage( \
38+
::lldb_dap::FormatError(::std::move(error_private), __VA_ARGS__)); \
5139
} else \
5240
::llvm::consumeError(::std::move(error_private)); \
5341
} while (0)
@@ -59,19 +47,13 @@ namespace lldb_dap {
5947
class Log final {
6048
public:
6149
/// Creates a log file with the given filename.
62-
Log(llvm::StringRef filename);
63-
64-
/// Log is not copyable.
65-
/// @{
66-
Log(const Log &) = delete;
67-
void operator=(const Log &) = delete;
68-
/// @}
50+
Log(llvm::StringRef filename, std::error_code &EC);
6951

7052
void WriteMessage(llvm::StringRef message);
7153

7254
private:
7355
std::mutex m_mutex;
74-
std::ofstream m_stream;
56+
llvm::raw_fd_ostream m_stream;
7557
};
7658

7759
template <typename... Args>

lldb/tools/lldb-dap/lldb-dap.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <memory>
4949
#include <mutex>
5050
#include <string>
51+
#include <system_error>
5152
#include <thread>
5253
#include <utility>
5354
#include <vector>
@@ -486,8 +487,15 @@ int main(int argc, char *argv[]) {
486487

487488
std::unique_ptr<Log> log = nullptr;
488489
const char *log_file_path = getenv("LLDBDAP_LOG");
489-
if (log_file_path)
490-
log = std::make_unique<Log>(log_file_path);
490+
if (log_file_path) {
491+
std::error_code EC;
492+
log = std::make_unique<Log>(log_file_path, EC);
493+
if (EC) {
494+
llvm::logAllUnhandledErrors(llvm::errorCodeToError(EC), llvm::errs(),
495+
"Failed to create log file: ");
496+
return EXIT_FAILURE;
497+
}
498+
}
491499

492500
// Initialize LLDB first before we do anything.
493501
lldb::SBError error = lldb::SBDebugger::InitializeWithErrorHandling();

0 commit comments

Comments
 (0)