Bug 13406 – Feature request: @trace attribute for automatic tracing, OR @(scope, scopeFn)
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-08-30T21:44:00Z
Last change time
2014-11-03T12:03:37Z
Assigned to
nobody
Creator
kevin.lamonte
Comments
Comment #0 by kevin.lamonte — 2014-08-30T21:44:41Z
There have been several proposals for logging in phobos, but I have not seen a request for tracing. By tracing I mean on-the-fly recording of function entry+args and exit+return, sent to a logging framework of some kind. Other languages use aspect-oriented programming (Java) and before/after methods (lisp) to apply this automatically to all code in a project.
I believe that a @trace function attribute could satisfy this very well. Desired behavior:
@trace int foo(string bar) {
... blah ...
}
...magically becomes...
int foo(string bar) {
traceFunctionEnter(__LINE__, __FILE__, __MODULE__, __FUNCTION__, __PRETTYFUNCTION__, [ bar ]);
scope(success) {
traceFunctionExitSuccess(__LINE__, __FILE__, __MODULE__, __FUNCTION__, __PRETTYFUNCTION__, result);
}
scope(failure) {
traceFunctionExitFailure(__LINE__, __FILE__, __MODULE__, __FUNCTION__, __PRETTYFUNCTION__, result);
}
... blah ...
}
Add the ability to set traceFunctionEnter / traceFunctionExitFailure / traceFunctionExitSuccess and a logging framework could automagically plug in and become a tracing framework too.
If @trace as a feature is too library-specific, this idea could instead be implemented by something like @(scope, &scopeFn) where scopeFn looks like:
void scopeFn(scopeType, __LINE__, __FILE__, __MODULE__, __FUNCTION__, __PRETTYFUNCTION__, [ args ])
...where scopeType is an enum value meaning "in", "success", or "failure", and args contains either the input parameters or the return (value or exception), respectively.
Comment #1 by kevin.lamonte — 2014-11-03T12:03:37Z
I have changed my mind.
mixins can already accomplish this. Let's not make this a compiler dependency.