Skip to content

Commit

Permalink
[fix] Stdout/stderr shouldn't have been closed when Logger::StdoutTar…
Browse files Browse the repository at this point in the history
…get is deleted
  • Loading branch information
pajama-coder committed Aug 31, 2023
1 parent c54a5f8 commit 0bac17d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/api/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ void Logger::History::dump_messages(Data &buffer) {
// Logger::StdoutTarget
//

Logger::StdoutTarget::~StdoutTarget() {
if (m_file_stream) {
m_file_stream->close(false);
}
}

void Logger::StdoutTarget::write(const Data &msg) {
if (Net::current().is_running()) {
if (!m_file_stream) m_file_stream = FileStream::make(false, m_f, &s_dp_stdout);
Expand Down
1 change: 1 addition & 0 deletions src/api/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Logger : public pjs::ObjectTemplate<Logger> {
class StdoutTarget : public Target {
public:
StdoutTarget(FILE *f) : m_f(f) {}
~StdoutTarget();

private:
virtual void write(const Data &msg) override;
Expand Down
10 changes: 7 additions & 3 deletions src/fstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ FileStream::FileStream(bool read, FILE *f, Data::Producer *dp)
if (read) this->read();
}

void FileStream::close() {
void FileStream::close(bool close_fd) {
std::error_code ec;
m_stream.close(ec);
if (close_fd) {
m_stream.close(ec);
} else {
m_stream.release();
}

if (m_receiving_state == PAUSED) {
m_receiving_state = RECEIVING;
Expand All @@ -65,7 +69,7 @@ void FileStream::close() {
}

if (m_f) {
fclose(m_f);
if (close_fd) fclose(m_f);
m_f = nullptr;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class FileStream :

auto fd() const -> int { return m_fd; }
void set_buffer_limit(size_t size) { m_buffer_limit = size; }
void close();
void close(bool close_fd = true);

private:
FileStream(bool read, int fd, Data::Producer *dp);
Expand Down

0 comments on commit 0bac17d

Please sign in to comment.