LLDB mainline
StreamFile.cpp
Go to the documentation of this file.
1//===-- StreamFile.cpp ----------------------------------------------------===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8
12#include "lldb/Utility/Log.h"
13
14#include <cstdio>
15
16using namespace lldb;
17using namespace lldb_private;
18
19StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order)
20 : Stream(flags, addr_size, byte_order) {
21 m_file_sp = std::make_shared<File>();
22}
23
24StreamFile::StreamFile(int fd, bool transfer_ownership) : Stream() {
25 m_file_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWriteOnly,
26 transfer_ownership);
27}
28
29StreamFile::StreamFile(FILE *fh, bool transfer_ownership) : Stream() {
30 m_file_sp = std::make_shared<NativeFile>(fh, File::eOpenOptionWriteOnly,
31 transfer_ownership);
32}
33
34StreamFile::StreamFile(const char *path, File::OpenOptions options,
35 uint32_t permissions)
36 : Stream() {
37 auto file = FileSystem::Instance().Open(FileSpec(path), options, permissions);
38 if (file)
39 m_file_sp = std::move(file.get());
40 else {
41 // TODO refactor this so the error gets popagated up instead of logged here.
42 LLDB_LOG_ERROR(GetLog(LLDBLog::Host), file.takeError(),
43 "Cannot open {1}: {0}", path);
44 m_file_sp = std::make_shared<File>();
45 }
46}
47
48StreamFile::~StreamFile() = default;
49
50void StreamFile::Flush() { m_file_sp->Flush(); }
51
52size_t StreamFile::WriteImpl(const void *s, size_t length) {
53 m_file_sp->Write(s, length);
54 return length;
55}
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:392
A file utility class.
Definition FileSpec.h:57
int Open(const char *path, int flags, int mode=0600)
Wraps open in a platform-independent way.
static FileSystem & Instance()
@ eOpenOptionWriteOnly
Definition File.h:52
StreamFile(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order)
std::shared_ptr< File > m_file_sp
Definition StreamFile.h:50
void Flush() override
Flush the stream.
size_t WriteImpl(const void *s, size_t length) override
Output character bytes to the stream.
Stream(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order, bool colors=false)
Construct with flags and address size and byte order.
Definition Stream.cpp:27
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
ByteOrder
Byte ordering definitions.