From 6ded7b6f2b7cfa8a8603261fa9eb6e82987e4b34 Mon Sep 17 00:00:00 2001 From: "Tomoya Matsuura(MacBookPro)" Date: Fri, 28 Aug 2020 22:19:03 +0900 Subject: [PATCH 1/4] made logger class available to be downcasted into std::ostream --- include/c74_min_logger.h | 168 ++++++++++++++++++++------------------- 1 file changed, 86 insertions(+), 82 deletions(-) diff --git a/include/c74_min_logger.h b/include/c74_min_logger.h index 17bb679..fb99090 100644 --- a/include/c74_min_logger.h +++ b/include/c74_min_logger.h @@ -7,93 +7,97 @@ namespace c74::min { - class logger_line_ending {}; ///< A type to represent line endings for the min::logger class. - static logger_line_ending endl; ///< An instance of a line ending for convenience. - - - /// Logging utility to deliver console messages - /// in such a way that they are mirrored to the Max window. - /// - /// This class is not intended to be used directly, - /// but rather through instances that are provided in min::object<> base class. - /// - /// @see min::object::cout - /// @see min::object::cerr - /// @see min::endl - - class logger { - public: - /// The output type of the message. - /// These are not `levels` as in some languages (e.g. Ruby) but distinct targets. - - enum class type { - message = 0, ///< A regular console post to the Max Window. - warning, ///< A highlighted and trappable warning post to the Max Window. - error ///< A highlighted and trappable error post to the Max Window. - }; - - - /// Constructor: typically you do not call this directly, - /// it used by min::object to create cout and cerr - /// @param an_owner Your object instance - /// @param a_type The type of console output to deliver - - logger(const object_base* an_owner, const logger::type a_type) - : m_owner { *an_owner } - , m_target { a_type } - {} - - - /// Use the insertion operator as for any other stream to build the output message - /// @param x A token to be added to the output stream. - /// @return A reference to the output stream. - - template - logger& operator<<(const T& x) { - m_stream << x; - return *this; - } + class logger_line_ending { }; ///< A type to represent line endings for the min::logger class. + static logger_line_ending endl; ///< An instance of a line ending for convenience. + + + /// Logging utility to deliver console messages + /// in such a way that they are mirrored to the Max window. + /// + /// This class is not intended to be used directly, + /// but rather through instances that are provided in min::object<> base class. + /// + /// @see min::object::cout + /// @see min::object::cerr + /// @see min::endl + template> + class basic_logger_streambuf : public std::basic_streambuf { }; + + template> + class basic_logger : public std::basic_iostream { + public: + /// The output type of the message. + /// These are not `levels` as in some languages (e.g. Ruby) but distinct targets. + + enum class type { + message = 0, ///< A regular console post to the Max Window. + warning, ///< A highlighted and trappable warning post to the Max Window. + error ///< A highlighted and trappable error post to the Max Window. + }; + + + /// Constructor: typically you do not call this directly, + /// it used by min::object to create cout and cerr + /// @param an_owner Your object instance + /// @param a_type The type of console output to deliver + + basic_logger(object_base* an_owner, basic_logger::type a_type) + : std::basic_iostream(new basic_logger_streambuf) + , m_owner {*an_owner} + , m_target {a_type} { } + + /// Use the insertion operator as for any other stream to build the output message + /// @param x A token to be added to the output stream. + /// @return A reference to the output stream. + + template + basic_logger& operator<<(const T2& x) { + m_stream << x; + return *this; + } + + + /// Pass endl to the insertion operator to complete the console post and flush it. + /// @param x The min::endl token + /// @return A reference to the output stream. + + basic_logger& operator<<(const logger_line_ending& x) { + const std::string& s = m_stream.str(); + + switch (m_target) { + case type::message: + std::cout << s << std::endl; + // if the max object is present then it is safe to post even if the owner isn't yet fully initialized + if (m_owner.initialized() || k_sym_max) + max::object_post(m_owner, s.c_str()); + break; + case type::warning: + std::cerr << s << std::endl; - /// Pass endl to the insertion operator to complete the console post and flush it. - /// @param x The min::endl token - /// @return A reference to the output stream. + // if the max object is present then it is safe to post even if the owner isn't yet fully initialized + if (m_owner.initialized() || k_sym_max) + max::object_warn(m_owner, s.c_str()); + break; + case type::error: + std::cerr << s << std::endl; - logger& operator<<(const logger_line_ending& x) { - const std::string& s = m_stream.str(); + // if the max object is present then it is safe to post even if the owner isn't yet fully initialized + if (m_owner.initialized() || k_sym_max) + max::object_error(m_owner, s.c_str()); + break; + } - switch (m_target) { - case type::message: - std::cout << s << std::endl; + m_stream.str(""); + return *this; + } - // if the max object is present then it is safe to post even if the owner isn't yet fully initialized - if (m_owner.initialized() || k_sym_max) - max::object_post(m_owner, s.c_str()); - break; - case type::warning: - std::cerr << s << std::endl; + private: + object_base& m_owner; + basic_logger::type m_target; + std::stringstream m_stream; + }; - // if the max object is present then it is safe to post even if the owner isn't yet fully initialized - if (m_owner.initialized() || k_sym_max) - max::object_warn(m_owner, s.c_str()); - break; - case type::error: - std::cerr << s << std::endl; - - // if the max object is present then it is safe to post even if the owner isn't yet fully initialized - if (m_owner.initialized() || k_sym_max) - max::object_error(m_owner, s.c_str()); - break; - } - - m_stream.str(""); - return *this; - } - - private: - const object_base& m_owner; - const logger::type m_target; - std::stringstream m_stream; - }; + using logger = basic_logger>; } // namespace c74::min From b477f5b228779a2386f1f83673f5f4aae88fab3f Mon Sep 17 00:00:00 2001 From: "Tomoya Matsuura(MacBookPro)" Date: Sat, 29 Aug 2020 01:02:59 +0900 Subject: [PATCH 2/4] changed to inheriting basic_stringbuf and iostream. --- include/c74_min_logger.h | 60 +++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/include/c74_min_logger.h b/include/c74_min_logger.h index fb99090..deff0e7 100644 --- a/include/c74_min_logger.h +++ b/include/c74_min_logger.h @@ -5,11 +5,12 @@ #pragma once +#include +#include namespace c74::min { - class logger_line_ending { }; ///< A type to represent line endings for the min::logger class. - static logger_line_ending endl; ///< An instance of a line ending for convenience. - + // for backword compatibility. min::endl is no longer a empty class but an alias to std::endl. + using std::endl; /// Logging utility to deliver console messages /// in such a way that they are mirrored to the Max window. @@ -19,12 +20,10 @@ namespace c74::min { /// /// @see min::object::cout /// @see min::object::cerr - /// @see min::endl - template> - class basic_logger_streambuf : public std::basic_streambuf { }; + /// @see min::object::cwarn + - template> - class basic_logger : public std::basic_iostream { + class loggerbuf : public std::basic_stringbuf { public: /// The output type of the message. /// These are not `levels` as in some languages (e.g. Ruby) but distinct targets. @@ -41,28 +40,13 @@ namespace c74::min { /// @param an_owner Your object instance /// @param a_type The type of console output to deliver - basic_logger(object_base* an_owner, basic_logger::type a_type) - : std::basic_iostream(new basic_logger_streambuf) + loggerbuf(object_base* an_owner, loggerbuf::type a_type) + : std::basic_stringbuf() , m_owner {*an_owner} , m_target {a_type} { } - /// Use the insertion operator as for any other stream to build the output message - /// @param x A token to be added to the output stream. - /// @return A reference to the output stream. - - template - basic_logger& operator<<(const T2& x) { - m_stream << x; - return *this; - } - - - /// Pass endl to the insertion operator to complete the console post and flush it. - /// @param x The min::endl token - /// @return A reference to the output stream. - - basic_logger& operator<<(const logger_line_ending& x) { - const std::string& s = m_stream.str(); + virtual int sync() override { + const std::string& s = str(); switch (m_target) { case type::message: @@ -81,23 +65,29 @@ namespace c74::min { break; case type::error: std::cerr << s << std::endl; - // if the max object is present then it is safe to post even if the owner isn't yet fully initialized if (m_owner.initialized() || k_sym_max) max::object_error(m_owner, s.c_str()); break; } - - m_stream.str(""); - return *this; + return 0; } private: - object_base& m_owner; - basic_logger::type m_target; - std::stringstream m_stream; + object_base& m_owner; + loggerbuf::type m_target; }; - using logger = basic_logger>; + class logger : public std::iostream { + public: + logger() = delete; + explicit logger(object_base* an_owner, loggerbuf::type a_type) + : buf(an_owner, a_type) + , std::iostream(&buf) { } + using type = loggerbuf::type; + + private: + loggerbuf buf; + }; } // namespace c74::min From 56af312f8d7df62d2a347c162feea0c87fab9bf4 Mon Sep 17 00:00:00 2001 From: "Tomoya Matsuura(MacBookPro)" Date: Tue, 10 Nov 2020 10:47:11 +0900 Subject: [PATCH 3/4] fixed a bug not clearing buffer after posting --- include/c74_min_logger.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/include/c74_min_logger.h b/include/c74_min_logger.h index deff0e7..3d13f92 100644 --- a/include/c74_min_logger.h +++ b/include/c74_min_logger.h @@ -21,6 +21,7 @@ namespace c74::min { /// @see min::object::cout /// @see min::object::cerr /// @see min::object::cwarn + /// @see min::endl class loggerbuf : public std::basic_stringbuf { @@ -50,26 +51,27 @@ namespace c74::min { switch (m_target) { case type::message: - std::cout << s << std::endl; + std::cout << s; // if the max object is present then it is safe to post even if the owner isn't yet fully initialized if (m_owner.initialized() || k_sym_max) max::object_post(m_owner, s.c_str()); break; case type::warning: - std::cerr << s << std::endl; + std::cerr << s; // if the max object is present then it is safe to post even if the owner isn't yet fully initialized if (m_owner.initialized() || k_sym_max) max::object_warn(m_owner, s.c_str()); break; case type::error: - std::cerr << s << std::endl; + std::cerr << s; // if the max object is present then it is safe to post even if the owner isn't yet fully initialized if (m_owner.initialized() || k_sym_max) max::object_error(m_owner, s.c_str()); break; } + str(""); return 0; } @@ -78,12 +80,12 @@ namespace c74::min { loggerbuf::type m_target; }; - class logger : public std::iostream { + class logger : public std::ostream { public: logger() = delete; explicit logger(object_base* an_owner, loggerbuf::type a_type) : buf(an_owner, a_type) - , std::iostream(&buf) { } + , std::ostream(&buf) { } using type = loggerbuf::type; private: From b81d17456ef704362eb6f4c236906e68d7b0c543 Mon Sep 17 00:00:00 2001 From: "Tomoya Matsuura(MacBookPro)" Date: Thu, 5 Jan 2023 22:02:24 +0900 Subject: [PATCH 4/4] rebased & updated dependencies --- include/readerwriterqueue | 2 +- max-api | 1 + test/mock | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) create mode 160000 max-api diff --git a/include/readerwriterqueue b/include/readerwriterqueue index a500a29..452ae29 160000 --- a/include/readerwriterqueue +++ b/include/readerwriterqueue @@ -1 +1 @@ -Subproject commit a500a29fe07c759fa793a95f4782645088ce5b7a +Subproject commit 452ae29683c0d60f68dfec0deefb87a36b9f7250 diff --git a/max-api b/max-api new file mode 160000 index 0000000..14ce0ac --- /dev/null +++ b/max-api @@ -0,0 +1 @@ +Subproject commit 14ce0ac18bb4ee967238d4cd03e1ecce86ffd960 diff --git a/test/mock b/test/mock index f3959e1..4a5ce91 160000 --- a/test/mock +++ b/test/mock @@ -1 +1 @@ -Subproject commit f3959e17a9323ef7317833d1db24db0db4e037a6 +Subproject commit 4a5ce91b398e49b61fd687c7fe3d96f5a634fc7b