diff --git a/include/ignition/common/Console.hh b/include/ignition/common/Console.hh index a5a256615..dfd36ce67 100644 --- a/include/ignition/common/Console.hh +++ b/include/ignition/common/Console.hh @@ -113,13 +113,25 @@ namespace ignition /// \brief Destructor. public: virtual ~Buffer(); + /// \brief Writes _count characters to the string buffer + /// \param[in] _char Input rharacter array. + /// \param[in] _count Number of characters in array. + /// \return The number of characters successfully written. + public: std::streamsize xsputn( + const char *_char, std::streamsize _count) override; + /// \brief Sync the stream (output the string buffer /// contents). /// \return Return 0 on success. - public: virtual int sync(); + public: int sync() override; /// \brief Stream to output information into. public: std::ofstream *stream; + /// \brief Mutex to synchronize writes to the string buffer + /// and the output stream. + /// \todo(nkoenig) Put this back in for ign-common5, and + /// remove the corresponding static version. + // public: std::mutex syncMutex; }; IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING diff --git a/src/Console.cc b/src/Console.cc index 1600150f7..1607841f4 100644 --- a/src/Console.cc +++ b/src/Console.cc @@ -362,16 +362,32 @@ FileLogger::Buffer::~Buffer() static_cast(this->stream)->close(); } +///////////////////////////////////////////////// +std::streamsize FileLogger::Buffer::xsputn(const char *_char, + std::streamsize _count) +{ + auto lk = BufferLock(reinterpret_cast(this)); + return std::stringbuf::xsputn(_char, _count); +} + ///////////////////////////////////////////////// int FileLogger::Buffer::sync() { if (!this->stream) return -1; - *this->stream << this->str(); - - this->stream->flush(); + { + auto lk = BufferLock(reinterpret_cast(this)); + *this->stream << this->str(); + } - this->str(""); + { + auto lk = BufferLock(reinterpret_cast(this)); + this->stream->flush(); + } + { + auto lk = BufferLock(reinterpret_cast(this)); + this->str(""); + } return !(*this->stream); }