From 0bb6171a8512e7f107d10829263cb4ee90450d86 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 20 Mar 2014 16:50:25 -0700 Subject: [PATCH] beast.streams improvements: * New basic_std_ostream wraps any std::stream. * New debug_ostream redirects output to debugger if attached. --- Builds/VisualStudio2013/beast.vcxproj | 2 + Builds/VisualStudio2013/beast.vcxproj.filters | 6 ++ beast/streams/basic_abstract_ostream.h | 20 ----- beast/streams/basic_std_ostream.h | 73 ++++++++++++++++ beast/streams/debug_ostream.h | 85 +++++++++++++++++++ 5 files changed, 166 insertions(+), 20 deletions(-) create mode 100644 beast/streams/basic_std_ostream.h create mode 100644 beast/streams/debug_ostream.h diff --git a/Builds/VisualStudio2013/beast.vcxproj b/Builds/VisualStudio2013/beast.vcxproj index 3e65afc797..1df00296c4 100644 --- a/Builds/VisualStudio2013/beast.vcxproj +++ b/Builds/VisualStudio2013/beast.vcxproj @@ -193,6 +193,8 @@ + + diff --git a/Builds/VisualStudio2013/beast.vcxproj.filters b/Builds/VisualStudio2013/beast.vcxproj.filters index 1282af9395..e2c6b8d5bc 100644 --- a/Builds/VisualStudio2013/beast.vcxproj.filters +++ b/Builds/VisualStudio2013/beast.vcxproj.filters @@ -1164,6 +1164,12 @@ beast\container + + beast\streams + + + beast\streams + diff --git a/beast/streams/basic_abstract_ostream.h b/beast/streams/basic_abstract_ostream.h index 00da5ed669..a3ff5e3fa9 100644 --- a/beast/streams/basic_abstract_ostream.h +++ b/beast/streams/basic_abstract_ostream.h @@ -66,26 +66,6 @@ public: } /** @} */ - /** Returns a basic_scoped_ostream without appending anything. - This can be used if the caller wants to put the scoped stream - on the stack to build up output in a thread-safe fashion. - */ -#if 0 - scoped_stream_type - scoped_stream () - { -#if 0 - return scoped_stream_type ( - [this](string_type const& s) - { - this->write (s); - }); -#else - return scoped_stream_type (*this); -#endif - } -#endif - /** Called to output each string. */ virtual void diff --git a/beast/streams/basic_std_ostream.h b/beast/streams/basic_std_ostream.h new file mode 100644 index 0000000000..677eb39bbd --- /dev/null +++ b/beast/streams/basic_std_ostream.h @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_UTILITY_STD_OSTREAM_H_INCLUDED +#define BEAST_UTILITY_STD_OSTREAM_H_INCLUDED + +#include "basic_abstract_ostream.h" + +#include + +namespace beast { + +/** Wraps an existing std::basic_ostream as an abstract_ostream. */ +template < + class CharT, + class Traits = std::char_traits +> +class basic_std_ostream + : public basic_abstract_ostream +{ +private: + using typename basic_abstract_ostream ::string_type; + + std::reference_wrapper m_stream; + +public: + explicit basic_std_ostream ( + std::basic_ostream & stream) + : m_stream (stream) + { + } + + void + write (string_type const& s) override + { + m_stream.get() << s << std::endl; + } +}; + +typedef basic_std_ostream std_ostream; + +//------------------------------------------------------------------------------ + +/** Returns a basic_std_ostream using template argument deduction. */ +template < + class CharT, + class Traits = std::char_traits +> +basic_std_ostream +make_std_ostream (std::basic_ostream & stream) +{ + return basic_std_ostream (stream); +} + +} + +#endif diff --git a/beast/streams/debug_ostream.h b/beast/streams/debug_ostream.h new file mode 100644 index 0000000000..6dc16448fc --- /dev/null +++ b/beast/streams/debug_ostream.h @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_UTILITY_DEBUG_OSTREAM_H_INCLUDED +#define BEAST_UTILITY_DEBUG_OSTREAM_H_INCLUDED + +#include "abstract_ostream.h" + +#include + +#ifdef _MSC_VER +# ifndef WIN32_LEAN_AND_MEAN // VC_EXTRALEAN +# define WIN32_LEAN_AND_MEAN +# include +# undef WIN32_LEAN_AND_MEAN +# else +# include +# endif +#endif + +namespace beast { + +#ifdef _MSC_VER +/** A basic_abstract_ostream that redirects output to an attached debugger. */ +class debug_ostream + : public abstract_ostream +{ +private: + bool m_debugger; + +public: + debug_ostream() + : m_debugger (IsDebuggerPresent() != FALSE) + { + // Note that the check for an attached debugger is made only + // during construction time, for efficiency. A stream created before + // the debugger is attached will not have output redirected. + } + + void + write (string_type const& s) override + { + if (m_debugger) + { + OutputDebugStringA ((s + "\n").c_str()); + return; + } + + std::cout << s << std::endl; + } +}; + +#else +class debug_ostream + : public abstract_ostream +{ +public: + void + write (string_type const& s) override + { + std::cout << s << std::endl; + } +}; + +#endif + +} + +#endif