From 9cb525a2e34d9548ddaf49505c9096ae9f046cd3 Mon Sep 17 00:00:00 2001 From: PulkoMandy Date: Sun, 30 Nov 2025 16:08:55 +0100 Subject: [PATCH] FunctionTracer: add support for "this" pointer and custom printf function Prepare for replacement of modified version used in input_server addons. Adapt existing callers. The "this" argument is required, and the CALLED macro can only set it to one single value for an entire file currently. So, in any case where at least one standalone function or static method uses CALLED, the macro has to use a NULL pointer for all calls. Change-Id: I42fdcbeaf6dd039d4c4369818220ad85e0b8087e Reviewed-on: https://review.haiku-os.org/c/haiku/+/10041 Tested-by: Commit checker robot Reviewed-by: Adrien Destugues --- headers/private/shared/FunctionTracer.h | 27 ++++++++++++++----- .../media-add-ons/opensound/OpenSoundNode.cpp | 2 +- src/apps/text_search/GrepWindow.cpp | 2 +- src/kits/interface/MenuField.cpp | 2 +- src/kits/interface/TextControl.cpp | 2 +- src/kits/interface/TextView.cpp | 2 +- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/headers/private/shared/FunctionTracer.h b/headers/private/shared/FunctionTracer.h index e8d26930c6..f375c65f4f 100644 --- a/headers/private/shared/FunctionTracer.h +++ b/headers/private/shared/FunctionTracer.h @@ -1,33 +1,40 @@ /* - * Copyright © 2008 Stephan Aßmus - * All rights reserved. Distributed under the terms of the MIT/X11 license. + * Copyright 2008 Stephan Aßmus + * Distributed under the terms of the MIT/X11 license. */ #ifndef FUNCTION_TRACER_H #define FUNCTION_TRACER_H + #include #include + namespace BPrivate { class FunctionTracer { public: - FunctionTracer(const char* functionName, int32& depth) + typedef int (*PrintFunction)(const char * fmt, ...); + + FunctionTracer(PrintFunction printFunction, const void* pointer, const char* functionName, + int32& depth) : fFunctionName(functionName), fPrepend(), - fFunctionDepth(depth) + fFunctionDepth(depth), + fPrintFunction(printFunction) { fFunctionDepth++; + if (pointer != NULL) + fPrepend.SetToFormat("%p ->", pointer); fPrepend.Append(' ', fFunctionDepth * 2); - printf("%s%s {\n", fPrepend.String(), fFunctionName.String()); + fPrintFunction("%s%s {\n", fPrepend.String(), fFunctionName.String()); } ~FunctionTracer() { -// printf("%s - leave\n", fFunctionName.String()); - printf("%s}\n", fPrepend.String()); + fPrintFunction("%s}\n", fPrepend.String()); fFunctionDepth--; } @@ -35,6 +42,12 @@ private: BString fFunctionName; BString fPrepend; int32& fFunctionDepth; +#if __GNUC__ < 3 + // gcc2 doesn't support function attributes on function pointers + PrintFunction fPrintFunction; +#else + PrintFunction _PRINTFLIKE(1, 2) fPrintFunction; +#endif }; } // namespace BPrivate diff --git a/src/add-ons/media/media-add-ons/opensound/OpenSoundNode.cpp b/src/add-ons/media/media-add-ons/opensound/OpenSoundNode.cpp index 9e07aa423b..2b6ae6bf60 100644 --- a/src/add-ons/media/media-add-ons/opensound/OpenSoundNode.cpp +++ b/src/add-ons/media/media-add-ons/opensound/OpenSoundNode.cpp @@ -50,7 +50,7 @@ using std::nothrow; #ifdef TRACE_OSS_NODE static int32 sDepth; # define TRACE(x...) printf(x) -# define CALLED(x...) FunctionTracer _ft(__PRETTY_FUNCTION__, sDepth) +# define CALLED(x...) FunctionTracer _ft(printf, NULL, __PRETTY_FUNCTION__, sDepth) # define PRINTING #else # define TRACE(x...) diff --git a/src/apps/text_search/GrepWindow.cpp b/src/apps/text_search/GrepWindow.cpp index 7dc99d483b..f8e8f4c4b5 100644 --- a/src/apps/text_search/GrepWindow.cpp +++ b/src/apps/text_search/GrepWindow.cpp @@ -56,7 +56,7 @@ static const bigtime_t kChangesPulseInterval = 150000; static int32 sDepth; -# define CALLED() FunctionTracer functionTracer(__PRETTY_FUNCTION__, sDepth) +# define CALLED() FunctionTracer functionTracer(printf, this, __PRETTY_FUNCTION__, sDepth) #else # define CALLED() #endif // TRACE_FUNCTIONS diff --git a/src/kits/interface/MenuField.cpp b/src/kits/interface/MenuField.cpp index 5197db3add..1bddb26e30 100644 --- a/src/kits/interface/MenuField.cpp +++ b/src/kits/interface/MenuField.cpp @@ -47,7 +47,7 @@ #ifdef TRACE_MENU_FIELD # include static int32 sFunctionDepth = -1; -# define CALLED(x...) FunctionTracer _ft(__PRETTY_FUNCTION__, sFunctionDepth) +# define CALLED(x...) FunctionTracer _ft(printf, this, __PRETTY_FUNCTION__, sFunctionDepth) # define TRACE(x...) { BString _to; \ _to.Append(' ', (sFunctionDepth + 1) * 2); \ printf("%s", _to.String()); printf(x); } diff --git a/src/kits/interface/TextControl.cpp b/src/kits/interface/TextControl.cpp index 4696779dfb..ea12187197 100644 --- a/src/kits/interface/TextControl.cpp +++ b/src/kits/interface/TextControl.cpp @@ -37,7 +37,7 @@ # include # include static int32 sFunctionDepth = -1; -# define CALLED(x...) FunctionTracer _ft(__PRETTY_FUNCTION__, sFunctionDepth) +# define CALLED(x...) FunctionTracer _ft(printf, this, __PRETTY_FUNCTION__, sFunctionDepth) # define TRACE(x...) { BString _to; \ _to.Append(' ', (sFunctionDepth + 1) * 2); \ printf("%s", _to.String()); printf(x); } diff --git a/src/kits/interface/TextView.cpp b/src/kits/interface/TextView.cpp index 22969420aa..73efc39e85 100644 --- a/src/kits/interface/TextView.cpp +++ b/src/kits/interface/TextView.cpp @@ -78,7 +78,7 @@ using BPrivate::gSystemCatalog; #ifdef TRACE_TEXT_VIEW # include static int32 sFunctionDepth = -1; -# define CALLED(x...) FunctionTracer _ft(__PRETTY_FUNCTION__, sFunctionDepth) +# define CALLED(x...) FunctionTracer _ft(printf, NULL, __PRETTY_FUNCTION__, sFunctionDepth) # define TRACE(x...) { BString _to; \ _to.Append(' ', (sFunctionDepth + 1) * 2); \ printf("%s", _to.String()); printf(x); }