diff --git a/srtcore/api.cpp b/srtcore/api.cpp index 56c581fec..62bbd787b 100644 --- a/srtcore/api.cpp +++ b/srtcore/api.cpp @@ -231,7 +231,7 @@ string srt::CUDTUnited::CONID(SRTSOCKET sock) if (sock == 0) return ""; - std::ostringstream os; + fmt::obufstream os; os << "@" << sock << ":"; return os.str(); } @@ -3240,7 +3240,7 @@ bool srt::CUDTUnited::updateListenerMux(CUDTSocket* s, const CUDTSocket* ls) CMultiplexer& m = i->second; #if ENABLE_HEAVY_LOGGING - ostringstream that_muxer; + fmt::obufstream that_muxer; that_muxer << "id=" << m.m_iID << " port=" << m.m_iPort << " ip=" << (m.m_iIPversion == AF_INET ? "v4" : "v6"); #endif diff --git a/srtcore/queue.cpp b/srtcore/queue.cpp index 98999a81f..78de6a6b9 100644 --- a/srtcore/queue.cpp +++ b/srtcore/queue.cpp @@ -1078,8 +1078,8 @@ bool srt::CRendezvousQueue::qualifyToHandle(EReadStatus rst, else { HLOGC(cnlog.Debug, - log << "RID: socket @" << i->m_iID << " still active (remaining " << std::fixed - << (count_microseconds(i->m_tsTTL - tsNow) / 1000000.0) << "s of TTL)..."); + log << "RID: socket @" << i->m_iID << " still active (remaining " + << fmt::sfmt(count_microseconds(i->m_tsTTL - tsNow) / 1000000.0, "f") << "s of TTL)..."); } const steady_clock::time_point tsLastReq = i->m_pUDT->m_tsLastReqTime; diff --git a/srtcore/sfmt.h b/srtcore/sfmt.h index f635a1020..367926bce 100644 --- a/srtcore/sfmt.h +++ b/srtcore/sfmt.h @@ -10,6 +10,9 @@ // for FILE type from stdio. It has nothing to do with the rest of the {fmt} // library, except that it reuses the namespace. +#ifndef INC_FMT_SFMT_H +#define INC_FMT_SFMT_H + #include #include #include @@ -490,6 +493,19 @@ class obufstream return *this; } + // For unusual manipulation, usually to add NUL termination. + // NOTE: you must make sure that you won't use the extended + // buffers if the intention was to get a string. + void append(char c) + { + buffer.append(c); + } + + const char* bufptr() const + { + return buffer.get_first(); + } + template obufstream& operator<<(const internal::form_memory_buffer& b) { @@ -558,6 +574,41 @@ class obufstream std::copy(data, data + i->size(), std::back_inserter(out)); } } + + template + size_t copy_to(OutputContainer& out, size_t maxsize) const + { + using namespace internal; + size_t avail = maxsize; + if (avail < buffer.first_size()) + { + std::copy(buffer.get_first(), buffer.get_first() + avail, + std::back_inserter(out)); + return maxsize; + } + + std::copy(buffer.get_first(), buffer.get_first() + buffer.first_size(), + std::back_inserter(out)); + + avail -= buffer.first_size(); + + for (form_memory_buffer<>::slices_t::const_iterator i = buffer.get_slices().begin(); + i != buffer.get_slices().end(); ++i) + { + // Would be tempting to move the blocks, but C++03 doesn't feature moving. + const char* data = &(*i)[0]; + + if (avail < i->size()) + { + std::copy(data, data + avail, std::back_inserter(out)); + return maxsize; + } + std::copy(data, data + i->size(), std::back_inserter(out)); + avail -= i->size(); + } + + return maxsize - avail; + } }; namespace internal @@ -662,3 +713,5 @@ inline ostdiostream& operator<<(ostdiostream& sout, const os_flush_manip&) } + +#endif diff --git a/srtcore/sync.cpp b/srtcore/sync.cpp index a7cebb909..e4e511fb5 100644 --- a/srtcore/sync.cpp +++ b/srtcore/sync.cpp @@ -50,13 +50,21 @@ std::string FormatTime(const steady_clock::time_point& timestamp) const uint64_t hours = total_sec / (60 * 60) - days * 24; const uint64_t minutes = total_sec / 60 - (days * 24 * 60) - hours * 60; const uint64_t seconds = total_sec - (days * 24 * 60 * 60) - hours * 60 * 60 - minutes * 60; - ostringstream out; + + // Temporary solution. Need to find some better handling + // of dynamic width and precision. + fmt::obufstream dfmts; + dfmts << "0" << decimals; + dfmts.append('\0'); // form_memory_buffer doesn't use NUL-termination. + const char* decimal_fmt = dfmts.bufptr(); + + fmt::obufstream out; if (days) out << days << "D "; - out << setfill('0') << setw(2) << hours << ":" - << setfill('0') << setw(2) << minutes << ":" - << setfill('0') << setw(2) << seconds << "." - << setfill('0') << setw(decimals) << (timestamp - seconds_from(total_sec)).time_since_epoch().count() << " [STDY]"; + out << fmt::sfmt(hours, "02") << ":" + << fmt::sfmt(minutes, "02") << ":" + << fmt::sfmt(seconds, "02") << "." + << fmt::sfmt((timestamp - seconds_from(total_sec)).time_since_epoch().count(), decimal_fmt) << " [STDY]"; return out.str(); } @@ -72,8 +80,8 @@ std::string FormatTimeSys(const steady_clock::time_point& timestamp) char tmp_buf[512]; strftime(tmp_buf, 512, "%X.", &tm); - ostringstream out; - out << tmp_buf << setfill('0') << setw(6) << (count_microseconds(timestamp.time_since_epoch()) % 1000000) << " [SYST]"; + fmt::obufstream out; + out << tmp_buf << fmt::sfmt(count_microseconds(timestamp.time_since_epoch()) % 1000000, "06") << " [SYST]"; return out.str(); } diff --git a/srtcore/sync.h b/srtcore/sync.h index fb6d56432..627f7eac5 100644 --- a/srtcore/sync.h +++ b/srtcore/sync.h @@ -55,7 +55,7 @@ #include "srt.h" #include "utilities.h" #include "srt_attr_defs.h" - +#include "sfmt.h" namespace srt { @@ -775,7 +775,9 @@ struct DurationUnitName template inline std::string FormatDuration(const steady_clock::duration& dur) { - return Sprint(std::fixed, DurationUnitName::count(dur)) + DurationUnitName::name(); + fmt::obufstream out; + out << fmt::sfmt(DurationUnitName::count(dur), "f") << DurationUnitName::name(); + return out.str(); } inline std::string FormatDuration(const steady_clock::duration& dur) diff --git a/srtcore/utilities.h b/srtcore/utilities.h index 1786cf0ae..ef5e41d81 100644 --- a/srtcore/utilities.h +++ b/srtcore/utilities.h @@ -34,7 +34,6 @@ written by #include #include #include -#include #include #if HAVE_CXX11 @@ -46,6 +45,8 @@ written by #include #include +#include "sfmt.h" + // -------------- UTILITIES ------------------------ // --- ENDIAN --- @@ -981,6 +982,19 @@ inline std::string FormatBinaryString(const uint8_t* bytes, size_t size) if ( size == 0 ) return ""; + using namespace fmt; + + obufstream os; + + os << sfmt(bytes[0], "02X"); + for (size_t i = 1; i < size; ++i) + { + os << sfmt(bytes[i], "02X"); + } + return os.str(); + + /* OLD VERSION + //char buf[256]; using namespace std; @@ -1008,6 +1022,7 @@ inline std::string FormatBinaryString(const uint8_t* bytes, size_t size) os << int(bytes[i]); } return os.str(); + */ } @@ -1171,10 +1186,7 @@ inline std::string BufferStamp(const char* mem, size_t size) } // Convert to hex string - ostringstream os; - os << hex << uppercase << setfill('0') << setw(8) << sum; - - return os.str(); + return fmt::sfmts(sum, "08X"); } template