summaryrefslogtreecommitdiff
path: root/indra/test/debug.h
diff options
context:
space:
mode:
authorNat Goodspeed <nat@lindenlab.com>2024-04-18 11:51:50 -0400
committerNat Goodspeed <nat@lindenlab.com>2024-04-18 11:51:50 -0400
commitdba3b3b10c317eef14cbc24e519a8e4620bfc6e8 (patch)
tree3a95320d7df723e37847a313a405318bb812002c /indra/test/debug.h
parent427f971c200e15197f2cd8a9136377cac6f72e77 (diff)
In debug.h, add debug_expr() macro for inline expressions.
Break out LOGTEST_enabled() inline function because it's used for both Debug and debug_expr().
Diffstat (limited to 'indra/test/debug.h')
-rw-r--r--indra/test/debug.h48
1 files changed, 35 insertions, 13 deletions
diff --git a/indra/test/debug.h b/indra/test/debug.h
index 3c4f3cabb4..2f6a114761 100644
--- a/indra/test/debug.h
+++ b/indra/test/debug.h
@@ -37,29 +37,37 @@
* Debugging stuff
*****************************************************************************/
/**
- * This class is intended to illuminate entry to a given block, exit from the
- * same block and checkpoints along the way. It also provides a convenient
- * place to turn std::cerr output on and off.
- *
- * If the environment variable LOGTEST is non-empty, each Debug instance will
- * announce its construction and destruction, presumably at entry and exit to
- * the block in which it's declared. Moreover, any arguments passed to its
- * operator()() will be streamed to std::cerr, prefixed by the block
- * description.
+ * Return true if the environment variable LOGTEST is non-empty.
*
* The variable LOGTEST is used because that's the environment variable
* checked by test.cpp, our TUT main() program, to turn on LLError logging. It
* is expected that Debug is solely for use in test programs.
*/
+inline
+bool LOGTEST_enabled()
+{
+ auto LOGTEST{ getenv("LOGTEST") };
+ // debug output enabled when LOGTEST is set AND non-empty
+ return LOGTEST && *LOGTEST;
+}
+
+/**
+ * This class is intended to illuminate entry to a given block, exit from the
+ * same block and checkpoints along the way. It also provides a convenient
+ * place to turn std::cerr output on and off.
+ *
+ * If enabled, each Debug instance will announce its construction and
+ * destruction, presumably at entry and exit to the block in which it's
+ * declared. Moreover, any arguments passed to its operator()() will be
+ * streamed to std::cerr, prefixed by the block description.
+ */
class Debug
{
public:
template <typename... ARGS>
Debug(ARGS&&... args):
mBlock(stringize(std::forward<ARGS>(args)...)),
- mLOGTEST(getenv("LOGTEST")),
- // debug output enabled when LOGTEST is set AND non-empty
- mEnabled(mLOGTEST && *mLOGTEST)
+ mEnabled(LOGTEST_enabled())
{
(*this)("entry");
}
@@ -85,7 +93,6 @@ public:
private:
const std::string mBlock;
- const char* mLOGTEST;
bool mEnabled;
};
@@ -93,4 +100,19 @@ private:
// of the Debug block.
#define DEBUG Debug debug(LL_PRETTY_FUNCTION)
+/// If enabled, debug_expr(expression) gives you output concerning an inline
+/// expression such as a class member initializer.
+#define debug_expr(expr) debug_expr_(#expr, [&](){ return expr; })
+
+template <typename EXPR>
+inline auto debug_expr_(const char* strexpr, EXPR&& lambda)
+{
+ if (! LOGTEST_enabled())
+ return std::forward<EXPR>(lambda)();
+ print("Before: ", strexpr);
+ auto result{ std::forward<EXPR>(lambda)() };
+ print(strexpr, " -> ", result);
+ return result;
+}
+
#endif /* ! defined(LL_DEBUG_H) */