Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // orignal function name was 'FunctionName'
- void FunctionNameReal(...)
- {
- // Do Something
- }
- #undef FunctionName
- #define FunctionName printf("Calling FunctionName from %sn",__FUNCTION__);FunctionNameReal
- #include <iostream>
- int FunctionName(int one, int two)
- {
- static int calls=0;
- return (++calls+one)*two;
- }
- #ifdef REPORT
- // class to capture the caller and print it.
- class Reporter
- {
- public:
- Reporter(std::string Caller, std::string File, int Line)
- : caller_(Caller)
- , file_(File)
- , line_(Line)
- {}
- int operator()(int one, int two)
- {
- std::cout
- << "Reporter: FunctionName() is being called by "
- << caller_ << "() in " << file_ << ":" << line_ << std::endl;
- // can use the original name here, as it is still defined
- return FunctionName(one,two);
- }
- private:
- std::string caller_;
- std::string file_;
- int line_;
- };
- // remove the symbol for the function, then define a new version that instead
- // creates a stack temporary instance of Reporter initialized with the caller
- # undef FunctionName
- # define FunctionName Reporter(__FUNCTION__,__FILE__,__LINE__)
- #endif
- void Caller1()
- {
- int val = FunctionName(7,9); // <-- works for captured return value
- std::cout << "Mystery Function got " << val << std::endl;
- }
- void Caller2()
- {
- // Works for inline as well.
- std::cout << "Mystery Function got " << FunctionName(11,13) << std::endl;
- }
- int main(int argc, char** argv)
- {
- Caller1();
- Caller2();
- return 0;
- }
- Reporter: FunctionName() is being called by Caller1() in test.cpp:44
- Mystery Function got 72
- Reporter: FunctionName() is being called by Caller2() in test.cpp:51
- Mystery Function got 169
- void Caller2()
- {
- std::cout << "Mystery Function got " << Reporter(__FUNCTION__,"test.cpp",51)(11,13) << std::endl;
- }
- #include <iostream>
- class ClassName
- {
- public:
- explicit ClassName(int Member)
- : member_(Member)
- {}
- int FunctionName(int one, int two)
- {
- return (++member_+one)*two;
- }
- private:
- int member_;
- };
- #ifdef REPORT
- // class to capture the caller and print it.
- class ClassNameDecorator
- {
- public:
- ClassNameDecorator( int Member)
- : className_(Member)
- {}
- ClassNameDecorator& FunctionName(std::string Caller, std::string File, int Line)
- {
- std::cout
- << "Reporter: ClassName::FunctionName() is being called by "
- << Caller << "() in " << File << ":" << Line << std::endl;
- return *this;
- }
- int operator()(int one, int two)
- {
- return className_.FunctionName(one,two);
- }
- private:
- ClassName className_;
- };
- // remove the symbol for the function, then define a new version that instead
- // creates a stack temporary instance of ClassNameDecorator.
- // FunctionName is then replaced with a version that takes the caller information
- // and uses Method Chaining to allow operator() to be invoked with the original
- // parameters.
- # undef ClassName
- # define ClassName ClassNameDecorator
- # undef FunctionName
- # define FunctionName FunctionName(__FUNCTION__,__FILE__,__LINE__)
- #endif
- void Caller1()
- {
- ClassName foo(21);
- int val = foo.FunctionName(7,9); // <-- works for captured return value
- std::cout << "Mystery Function got " << val << std::endl;
- }
- void Caller2()
- {
- ClassName foo(42);
- // Works for inline as well.
- std::cout << "Mystery Function got " << foo.FunctionName(11,13) << std::endl;
- }
- int main(int argc, char** argv)
- {
- Caller1();
- Caller2();
- return 0;
- }
- Reporter: ClassName::FunctionName() is being called by Caller1() in test.cpp:56
- Mystery Function got 261
- Reporter: ClassName::FunctionName() is being called by Caller2() in test.cpp:64
- Mystery Function got 702
- #include <cstdio>
- void foobar(const char* str = __builtin_FUNCTION()){
- std::printf("called by %sn", str);
- }
- int main(){
- foobar();
- return 0;
- }
- called by main
- // What: Track last few lines in loci of control, gpl/moshahmed_at_gmail
- // Test: gcc -Wall -g -lm -std=c11 track.c
- #include <stdio.h>
- #include <string.h>
- #define _DEBUG
- #ifdef _DEBUG
- #define lsize 255 /* const int lsize=255; -- C++ */
- struct locs {
- int line[lsize];
- char *file[lsize];
- char *func[lsize];
- int cur; /* cur=0; C++ */
- } locs;
- #define track do {
- locs.line[locs.cur]=__LINE__ ;
- locs.file[locs.cur]=(char*)__FILE__ ;
- locs.func[locs.cur]=(char*) __builtin_FUNCTION() /* __PRETTY_FUNCTION__ -- C++ */ ;
- locs.cur=(locs.cur+1) % lsize;
- } while(0);
- void track_start(){
- memset(&locs,0, sizeof locs);
- }
- void track_print(){
- int i, k;
- for (i=0; i<lsize; i++){
- k = (locs.cur+i) % lsize;
- if (locs.file[k]){
- fprintf(stderr,"%d: %s:%d %sn",
- k, locs.file[k],
- locs.line[k], locs.func[k]);
- }
- }
- }
- #else
- #define track do {} while(0)
- #define track_start() (void)0
- #define track_print() (void)0
- #endif
- // Sample usage.
- void bar(){ track ; }
- void foo(){ track ; bar(); }
- int main(){
- int k;
- track_start();
- for (k=0;k<2;k++)
- foo();
- track;
- track_print();
- return 0;
- }
Add Comment
Please, Sign In to add comment