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, transfer_ownership);
31}
32
33StreamFile::StreamFile(const char *path, File::OpenOptions options,
34 uint32_t permissions)
35 : Stream() {
36 auto file = FileSystem::Instance().Open(FileSpec(path), options, permissions);
37 if (file)
38 m_file_sp = std::move(file.get());
39 else {
40 // TODO refactor this so the error gets popagated up instead of logged here.
41 LLDB_LOG_ERROR(GetLog(LLDBLog::Host), file.takeError(),
42 "Cannot open {1}: {0}", path);
43 m_file_sp = std::make_shared<File>();
44 }
45}
46
47StreamFile::~StreamFile() = default;
48
49void StreamFile::Flush() { m_file_sp->Flush(); }
50
51size_t StreamFile::WriteImpl(const void *s, size_t length) {
52 m_file_sp->Write(s, length);
53 return length;
54}
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:382
A file utility class.
Definition: FileSpec.h:56
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)
Definition: StreamFile.cpp:19
std::shared_ptr< File > m_file_sp
Definition: StreamFile.h:47
void Flush() override
Flush the stream.
Definition: StreamFile.cpp:49
size_t WriteImpl(const void *s, size_t length) override
Output character bytes to the stream.
Definition: StreamFile.cpp:51
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
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:331
Definition: SBAddress.h:15
ByteOrder
Byte ordering definitions.