Lib: fix a bug with diagnostic output
The header "format-cout.hpp" offers a convenience function to print pretty much any object or data in human readable form. However, the formatter for pointers used within this framework switched std::cout into hexadecimal display of numbers and failed to clean-up this state. Since the "stickyness" of IOS stream manipulators is generally a problem, we now provide a RAII helper to capture the previous stream state and automatically restore it when leaving the scope.
This commit is contained in:
parent
8fdde735d4
commit
f393780845
7 changed files with 263 additions and 6 deletions
|
|
@ -42,6 +42,7 @@
|
|||
|
||||
#include "lib/error.hpp"
|
||||
#include "lib/format-obj.hpp"
|
||||
#include "lib/ios-savepoint.hpp"
|
||||
//#include "lib/format-string.hpp"
|
||||
#include "lib/unique-malloc-owner.hpp"
|
||||
#include "lib/symbol.hpp"
|
||||
|
|
@ -375,6 +376,7 @@ namespace util {
|
|||
ostream&
|
||||
showAddr (ostream& stream, void const* addr)
|
||||
{
|
||||
IosSavepoint save{stream};
|
||||
size_t suffix_modulus = size_t(1) << DIAGNOSTICS_ADDRESS_SUFFIX_LEN * 8;
|
||||
return stream << "╲"
|
||||
<< hex
|
||||
|
|
|
|||
82
src/lib/ios-savepoint.hpp
Normal file
82
src/lib/ios-savepoint.hpp
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
IOS-SAVEPOINT.hpp - capture and restore std::ostream format settings
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2022, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/** @file ios-savepoint.hpp
|
||||
** Capture previous settings of an `std::ostream` and restore them when leaving scope.
|
||||
** The stream manipulators of the IOS-Framework from the C++ standard lib allow to adjust
|
||||
** various aspects of output formatting for following data fed to the output stream.
|
||||
** Unfortunately many of these settings are _sticky_ and thus may "taint" common
|
||||
** output streams like `std::cout`. By planting this RAII capsule into a local scope,
|
||||
** the internal settings of an output stream can be recorded and restored automatically,
|
||||
** once control flow leaves the scope.
|
||||
** @remark based on this [stackoverflow] by «[qbert220]», 2013
|
||||
**
|
||||
** @see IosSavepoint_test
|
||||
**
|
||||
** [qbert220]: https://stackoverflow.com/users/617617/qbert220
|
||||
** [stackoverflow]: https://stackoverflow.com/a/18822888 "Restore state of `std::cout` after manipulating it"
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LIB_IOS_SAVEPOINT_H
|
||||
#define LIB_IOS_SAVEPOINT_H
|
||||
|
||||
#include "lib/nocopy.hpp"
|
||||
|
||||
#include <ostream>
|
||||
|
||||
|
||||
|
||||
namespace util {
|
||||
|
||||
/**
|
||||
* RAII helper to capture and restore
|
||||
* output stream format settings.
|
||||
*/
|
||||
class IosSavepoint
|
||||
: MoveOnly
|
||||
{
|
||||
std::ostream& theStream_;
|
||||
std::ios_base::fmtflags prevFlags_;
|
||||
char fillChar_;
|
||||
|
||||
public:
|
||||
explicit
|
||||
IosSavepoint(std::ostream& stream_to_capture)
|
||||
: theStream_{stream_to_capture}
|
||||
, prevFlags_{theStream_.flags()}
|
||||
, fillChar_{theStream_.fill()}
|
||||
{ }
|
||||
|
||||
~IosSavepoint()
|
||||
{
|
||||
theStream_.flags(prevFlags_);
|
||||
theStream_.fill(fillChar_);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace util
|
||||
#endif /*LIB_IOS_SAVEPOINT_H*/
|
||||
|
|
@ -85,7 +85,7 @@ namespace meta{
|
|||
* The following explicit specialisations handle the other cases, which are
|
||||
* not objects, but primitive types (function (member) pointers and references).
|
||||
* @remarks The key trick of this solution is to rely on `decltype` of `operator()`
|
||||
* and was proposed 10/2011 by user "[kennytm]" in this [stackoverflow].
|
||||
* and was proposed 10/2011 by user «[kennytm]» in this [stackoverflow].
|
||||
* @note for a member pointer to function, only the actual arguments in the
|
||||
* function signature are reflected. But if you bind such a member
|
||||
* pointer into a `std::function`, an additional first parameter
|
||||
|
|
@ -102,7 +102,7 @@ namespace meta{
|
|||
* @see FunctionSignature_test
|
||||
*
|
||||
* [kennytm]: http://stackoverflow.com/users/224671/kennytm
|
||||
* [stackoverflow] : http://stackoverflow.com/questions/7943525/is-it-possible-to-figure-out-the-parameter-type-and-return-type-of-a-lambda/7943765#7943765 "answer on stackoverflow"
|
||||
* [stackoverflow]: http://stackoverflow.com/questions/7943525/is-it-possible-to-figure-out-the-parameter-type-and-return-type-of-a-lambda/7943765#7943765 "figure out parameter and return type of a Lambda"
|
||||
*/
|
||||
template<typename FUN, typename SEL =void>
|
||||
struct _Fun
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@
|
|||
**
|
||||
** # Rationale
|
||||
** The UI-Bus acts as a **mediating backbone**, impersonating the role
|
||||
** of the _Model_ and the _Controller_ in the [MVC-Pattern]in common UI architecture.
|
||||
** of the _Model_ and the _Controller_ in the [MVC-Pattern] in common UI architecture.
|
||||
**
|
||||
** The MVC-Pattern as such is fine, and probably the best we know for construction of
|
||||
** user interfaces. But it doesn't scale well towards the integration into a larger and
|
||||
|
|
@ -66,9 +66,9 @@
|
|||
** directed towards individual elements. The interactions at the bus are closely interrelated
|
||||
** with the [elementary UI-Element operations](\ref tangible.hpp).
|
||||
**
|
||||
** - *act*: send a [GenNode] representing the action
|
||||
** - *act*: send a \ref GenNode representing the action
|
||||
** - the ID is either a globally registered command-ID or an explicitly
|
||||
** ["opened"](\ref steam::control::SessionCommand::cycle(Symbol,string)) command instance ID.
|
||||
** [opened](\ref steam::control::SessionCommand::cycle(Symbol,string const&)) command instance ID.
|
||||
** - the payload is a Record<GenNode> holding the actual command arguments
|
||||
** - on reception, an _instance_ (anonymous clone copy) of the command is created, bound
|
||||
** with the arguments and handed over to the SteamDispatcher to be enqueued for execution.
|
||||
|
|
|
|||
|
|
@ -424,6 +424,21 @@ return: 0
|
|||
END
|
||||
|
||||
|
||||
TEST "IOS restore format" IosSavepoint_test <<END
|
||||
out-lit: 0x2a
|
||||
out-lit: 42
|
||||
out-lit: ******************42
|
||||
out-lit: 42
|
||||
out-lit: 2a
|
||||
out-lit: 052
|
||||
out-lit: 2a
|
||||
out-lit: 42
|
||||
out-lit: 1234
|
||||
out-lit: Tilt
|
||||
out-lit: 42
|
||||
END
|
||||
|
||||
|
||||
TEST "configurable Factory" MultiFact_test <<END
|
||||
END
|
||||
|
||||
|
|
|
|||
136
tests/library/ios-savepoint-test.cpp
Normal file
136
tests/library/ios-savepoint-test.cpp
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
IosSavepoint(Test) - manipulate and restore output stream formatting
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2022, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
/** @file ios-savepoint-test.cpp
|
||||
** unit test \ref IosSavepoint_test
|
||||
*/
|
||||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
#include "lib/format-cout.hpp"
|
||||
#include "lib/ios-savepoint.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
using std::string;
|
||||
|
||||
|
||||
namespace util {
|
||||
namespace test {
|
||||
|
||||
|
||||
|
||||
|
||||
/***************************************************************************//**
|
||||
* @test verifies capturing and restoring of std::ostream formatting state.
|
||||
* @see ios-savepoint.hpp
|
||||
*/
|
||||
class IosSavepoint_test
|
||||
: public Test
|
||||
{
|
||||
void
|
||||
run (Arg)
|
||||
{
|
||||
switchToHex();
|
||||
__verifySane();
|
||||
|
||||
setFill();
|
||||
__verifySane();
|
||||
|
||||
nested();
|
||||
__verifySane();
|
||||
|
||||
try {
|
||||
restoreAfterException();
|
||||
} catch(...){
|
||||
cout << "Tilt" <<endl;
|
||||
}
|
||||
__verifySane();
|
||||
}
|
||||
|
||||
|
||||
/** verify that original state is restored */
|
||||
void
|
||||
__verifySane()
|
||||
{
|
||||
cout << std::setw(10) << 42 <<endl;
|
||||
}
|
||||
|
||||
|
||||
/** @test verify that hexadecimal output is cleared */
|
||||
void
|
||||
switchToHex()
|
||||
{
|
||||
IosSavepoint save{cout};
|
||||
cout << std::hex
|
||||
<< std::showbase
|
||||
<< 42 <<endl;
|
||||
}
|
||||
|
||||
|
||||
/** @test verify that a custom fill character is cleared */
|
||||
void
|
||||
setFill()
|
||||
{
|
||||
IosSavepoint save{cout};
|
||||
cout << std::setfill('*')
|
||||
<< std::setw(20)
|
||||
<< 42 <<endl;
|
||||
}
|
||||
|
||||
|
||||
/** @test verify usage in nested scopes */
|
||||
void
|
||||
nested()
|
||||
{
|
||||
IosSavepoint save{cout};
|
||||
cout << std::hex << 42 <<endl;
|
||||
{
|
||||
IosSavepoint inner(cout);
|
||||
cout << std::oct << std::showbase << 42 <<endl;
|
||||
}
|
||||
cout << 42 <<endl;
|
||||
}
|
||||
|
||||
|
||||
/** @test verify clean-up happens even in case of an exception */
|
||||
void
|
||||
restoreAfterException()
|
||||
{
|
||||
auto boom = []() -> float
|
||||
{ throw 42; };
|
||||
|
||||
IosSavepoint save{cout};
|
||||
cout << std::hexfloat
|
||||
<< 1234
|
||||
<< endl
|
||||
<< boom()
|
||||
<< endl;
|
||||
}
|
||||
};
|
||||
|
||||
LAUNCHER (IosSavepoint_test, "unit common");
|
||||
|
||||
|
||||
}} // namespace util::test
|
||||
|
||||
|
|
@ -18862,8 +18862,30 @@
|
|||
<node CREATED="1664152996461" ID="ID_1521476415" MODIFIED="1664153038156" TEXT="und dieser führt jeweils zu einer anderen Ausdehnung des Hintergrunds (Flackern?)">
|
||||
<icon BUILTIN="broken-line"/>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1664153044266" ID="ID_175410634" MODIFIED="1664153998416" TEXT="und das Debugging per cout gibt Hexzahlen aus">
|
||||
<node COLOR="#435e98" CREATED="1664153044266" FOLDED="true" ID="ID_175410634" MODIFIED="1664236010809" TEXT="und das Debugging per cout gibt Hexzahlen aus">
|
||||
<icon BUILTIN="broken-line"/>
|
||||
<node CREATED="1664235646216" ID="ID_1679047596" MODIFIED="1664236010445" TEXT="Ursache waren naiv verwendete stream-Manipulatoren">
|
||||
<richcontent TYPE="NOTE"><html>
|
||||
<head>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
Die meisten IOS-Stream-Manipulatoren sind »sticky«, d.h. sie verändern per Seiteneffekt den Zustand im jeweiligen ostream. Ich hatte eine Methode zum Ausgeben einer Addresse, eingebaut im to-String-Framework, und diese hat std::output auf Hex-Ausgabe umgeschaltet. Erstaunlich daß ich das jahrelang nicht gemerkt habe!
|
||||
</p>
|
||||
</body>
|
||||
</html></richcontent>
|
||||
<icon BUILTIN="idea"/>
|
||||
</node>
|
||||
<node COLOR="#338800" CREATED="1664235664755" ID="ID_1756013187" MODIFIED="1664235705945" TEXT="RAII-Helper-Klasse, die ostream-Format-Zustand speichern und restaurieren kann">
|
||||
<icon BUILTIN="button_ok"/>
|
||||
</node>
|
||||
<node COLOR="#338800" CREATED="1664235714143" ID="ID_1375500060" MODIFIED="1664235720200" TEXT="mit Test abgedeckt">
|
||||
<icon BUILTIN="button_ok"/>
|
||||
</node>
|
||||
<node COLOR="#338800" CREATED="1664235720902" ID="ID_558625134" MODIFIED="1664235875234" TEXT="Bug in format-obj.cpp damit gefixt">
|
||||
<icon BUILTIN="button_ok"/>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
|
|
|
|||
Loading…
Reference in a new issue