From a0da5a3120218c090d76fd8722ab72b4431c0b1b Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Wed, 25 Mar 2020 00:02:09 +0100 Subject: [PATCH] Improve debugging --- libretroshare/src/file_sharing/file_tree.cc | 2 ++ .../src/serialiser/rstypeserializer.h | 21 ++++++------ libretroshare/src/util/rsdebug.h | 34 ++++++++++++++----- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/libretroshare/src/file_sharing/file_tree.cc b/libretroshare/src/file_sharing/file_tree.cc index 3cf893402..ddc24c3ac 100644 --- a/libretroshare/src/file_sharing/file_tree.cc +++ b/libretroshare/src/file_sharing/file_tree.cc @@ -55,6 +55,8 @@ RsFileTree::fromBase64(const std::string& base64) const auto failure = [](std::error_condition ec) { return std::make_tuple(nullptr, ec); }; + if(base64.empty()) return failure(std::errc::invalid_argument); + std::error_condition ec; std::vector mem; if( (ec = RsBase64::decode(base64, mem)) ) return failure(ec); diff --git a/libretroshare/src/serialiser/rstypeserializer.h b/libretroshare/src/serialiser/rstypeserializer.h index 2c3d17f3a..ce53dce26 100644 --- a/libretroshare/src/serialiser/rstypeserializer.h +++ b/libretroshare/src/serialiser/rstypeserializer.h @@ -1037,8 +1037,6 @@ protected: VLQ_deserialize( const uint8_t data[], uint32_t size, uint32_t& offset, T& member ) { - uint32_t backupOffset = offset; - member = 0; uint32_t offsetBackup = offset; @@ -1048,14 +1046,14 @@ protected: for (size_t i = 0; offset < size && i <= sizeof(T); ++i) { member |= (data[offset] & 127) << (7 * i); - // If the next-byte flag is not set, ++ is after on purpose + // If the next-byte flag is not set. ++ is after on purpose if(!(data[offset++] & 128)) { - RsDbg() << __PRETTY_FUNCTION__ - << " size: " << size - << " backupOffset " << backupOffset - << " offset: " << offset - << " member " << member << std::endl; + Dbg2() << __PRETTY_FUNCTION__ + << " size: " << size + << " backupOffset " << offsetBackup + << " offset: " << offset + << " member " << member << std::endl; return true; } } @@ -1064,13 +1062,14 @@ protected: * ended before we encountered the end of the number, or the number * is VLQ encoded improperly */ RsErr() << __PRETTY_FUNCTION__ << std::errc::illegal_byte_sequence + << " size: " << size << " offsetBackup: " << offsetBackup << " offset: " << offset << " bytes: "; for(; offsetBackup < offset; ++offsetBackup) - std::cerr << " " << std::bitset<8>(data[offsetBackup]); - std::cerr << std::endl; - + RsErr().uStream() << " " << std::bitset<8>(data[offsetBackup]); + RsErr().uStream() << std::endl; print_stacktrace(); + return false; } diff --git a/libretroshare/src/util/rsdebug.h b/libretroshare/src/util/rsdebug.h index 9478c0f74..95a9ee955 100644 --- a/libretroshare/src/util/rsdebug.h +++ b/libretroshare/src/util/rsdebug.h @@ -46,7 +46,10 @@ struct t_RsLogger { inline t_RsLogger() = default; - typedef t_RsLogger stream_type; + /** On other platforms expose the type of underlying stream. + * On Android it cannot work like that so return the class type itself + * just for code compatibility with other platforms */ + using stream_type = t_RsLogger; template inline stream_type& operator<<(const T& val) @@ -68,6 +71,11 @@ struct t_RsLogger return *this; } + /** On other platforms return underlying stream to write avoiding additional + * prefixes. On Android it cannot work like that so return the object itself + * just for code compatibility with other platforms */ + inline stream_type& uStream() { return *this; } + private: std::ostringstream ostr; }; @@ -92,7 +100,8 @@ struct t_RsLogger { inline t_RsLogger() = default; - typedef decltype(std::cerr) stream_type; + /// Expose the type of underlying stream + using stream_type = decltype(std::cerr); template inline stream_type& operator<<(const T& val) @@ -111,6 +120,9 @@ struct t_RsLogger /// needed for manipulators and things like std::endl stream_type& operator<<(std::ostream& (*pf)(std::ostream&)) { return std::cerr << pf; } + + /// Return underlying stream to write avoiding additional prefixes + inline stream_type& uStream() const { return std::cerr; } }; #endif // def __ANDROID__ @@ -176,16 +188,22 @@ struct RsNoDbg { inline RsNoDbg() = default; - /** - * This match most of the types, but might be not enough for templated - * types - */ + /** Defined as the type itself just for code compatibility with other + * logging classes */ + using stream_type = RsNoDbg; + + /** This match most of the types, but might be not enough for templated + * types */ template - inline RsNoDbg& operator<<(const T&) { return *this; } + inline stream_type& operator<<(const T&) { return *this; } /// needed for manipulators and things like std::endl - inline RsNoDbg& operator<<(std::ostream& (*/*pf*/)(std::ostream&)) + inline stream_type& operator<<(std::ostream& (*/*pf*/)(std::ostream&)) { return *this; } + + /** Return the object itself just for code compatibility with other + * logging classes */ + inline stream_type& uStream() { return *this; } }; /**