diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index baa927090f..b3b8d0086b 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -61,6 +61,7 @@ #include #include #include +#include #include #include #include @@ -2952,10 +2953,12 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessorResult* resu this->_do_export(*print, file, thumbnail_cb); file.flush(); if (file.is_error()) { + const std::string os_error = file.last_error(); file.close(); boost::nowide::remove(path_tmp.c_str()); // TRN %1% is a file path. - throw Slic3r::RuntimeError(Slic3r::format(_(L("G-code export to %1% failed.\nIs the disk full?")), path) + "\n"); + throw Slic3r::RuntimeError(Slic3r::format(_(L("G-code export to %1% failed.\nIs the disk full?")), path) + "\n" + + (os_error.empty() ? std::string() : os_error + "\n")); } } catch (std::exception &ex) { // Rethrow on any exception. std::runtime_exception and CanceledException are expected to be thrown. @@ -9174,9 +9177,15 @@ bool GCode::GCodeOutputStream::is_error() const return ::ferror(this->f); } +std::string GCode::GCodeOutputStream::last_error() const +{ + return m_write_errno == 0 ? std::string() : std::string(::strerror(m_write_errno)); +} + void GCode::GCodeOutputStream::flush() { - ::fflush(this->f); + if (::fflush(this->f) != 0 && m_write_errno == 0) + m_write_errno = errno; } void GCode::GCodeOutputStream::close() @@ -9192,7 +9201,9 @@ void GCode::GCodeOutputStream::write(const char *what) if (what != nullptr) { const char* gcode = what; // writes string to file - fwrite(gcode, 1, ::strlen(gcode), this->f); + const size_t len = ::strlen(gcode); + if (::fwrite(gcode, 1, len, this->f) != len && m_write_errno == 0) + m_write_errno = errno; //FIXME don't allocate a string, maybe process a batch of lines? m_processor.process_buffer(std::string(gcode)); } diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index 51b3687811..8e15cb719b 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -416,6 +416,8 @@ protected: bool is_open() const { return f; } bool is_error() const; + // The OS error of the first failed write or flush, empty if none. + std::string last_error() const; void flush(); void close(); @@ -435,6 +437,7 @@ protected: private: FILE *f = nullptr; GCodeProcessor &m_processor; + int m_write_errno = 0; }; // Virtual hooks for belt printer subclass (BeltGCode).