56 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Host/StreamFile.h"10#include "lldb/Host/FileSystem.h"11#include "lldb/Utility/LLDBLog.h"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}56