Guest User

Untitled

a guest
May 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. // orignal function name was 'FunctionName'
  2. void FunctionNameReal(...)
  3. {
  4. // Do Something
  5. }
  6.  
  7. #undef FunctionName
  8. #define FunctionName printf("Calling FunctionName from %sn",__FUNCTION__);FunctionNameReal
  9.  
  10. #include <iostream>
  11.  
  12. int FunctionName(int one, int two)
  13. {
  14. static int calls=0;
  15. return (++calls+one)*two;
  16. }
  17.  
  18. #ifdef REPORT
  19. // class to capture the caller and print it.
  20. class Reporter
  21. {
  22. public:
  23. Reporter(std::string Caller, std::string File, int Line)
  24. : caller_(Caller)
  25. , file_(File)
  26. , line_(Line)
  27. {}
  28.  
  29. int operator()(int one, int two)
  30. {
  31. std::cout
  32. << "Reporter: FunctionName() is being called by "
  33. << caller_ << "() in " << file_ << ":" << line_ << std::endl;
  34. // can use the original name here, as it is still defined
  35. return FunctionName(one,two);
  36. }
  37. private:
  38. std::string caller_;
  39. std::string file_;
  40. int line_;
  41.  
  42. };
  43.  
  44. // remove the symbol for the function, then define a new version that instead
  45. // creates a stack temporary instance of Reporter initialized with the caller
  46. # undef FunctionName
  47. # define FunctionName Reporter(__FUNCTION__,__FILE__,__LINE__)
  48. #endif
  49.  
  50.  
  51. void Caller1()
  52. {
  53. int val = FunctionName(7,9); // <-- works for captured return value
  54. std::cout << "Mystery Function got " << val << std::endl;
  55. }
  56.  
  57. void Caller2()
  58. {
  59. // Works for inline as well.
  60. std::cout << "Mystery Function got " << FunctionName(11,13) << std::endl;
  61. }
  62.  
  63. int main(int argc, char** argv)
  64. {
  65. Caller1();
  66. Caller2();
  67. return 0;
  68. }
  69.  
  70. Reporter: FunctionName() is being called by Caller1() in test.cpp:44
  71. Mystery Function got 72
  72. Reporter: FunctionName() is being called by Caller2() in test.cpp:51
  73. Mystery Function got 169
  74.  
  75. void Caller2()
  76. {
  77. std::cout << "Mystery Function got " << Reporter(__FUNCTION__,"test.cpp",51)(11,13) << std::endl;
  78. }
  79.  
  80. #include <iostream>
  81.  
  82. class ClassName
  83. {
  84. public:
  85. explicit ClassName(int Member)
  86. : member_(Member)
  87. {}
  88.  
  89. int FunctionName(int one, int two)
  90. {
  91. return (++member_+one)*two;
  92. }
  93.  
  94. private:
  95. int member_;
  96. };
  97.  
  98. #ifdef REPORT
  99. // class to capture the caller and print it.
  100. class ClassNameDecorator
  101. {
  102. public:
  103. ClassNameDecorator( int Member)
  104. : className_(Member)
  105. {}
  106.  
  107. ClassNameDecorator& FunctionName(std::string Caller, std::string File, int Line)
  108. {
  109. std::cout
  110. << "Reporter: ClassName::FunctionName() is being called by "
  111. << Caller << "() in " << File << ":" << Line << std::endl;
  112. return *this;
  113. }
  114. int operator()(int one, int two)
  115. {
  116. return className_.FunctionName(one,two);
  117. }
  118. private:
  119. ClassName className_;
  120. };
  121.  
  122.  
  123. // remove the symbol for the function, then define a new version that instead
  124. // creates a stack temporary instance of ClassNameDecorator.
  125. // FunctionName is then replaced with a version that takes the caller information
  126. // and uses Method Chaining to allow operator() to be invoked with the original
  127. // parameters.
  128. # undef ClassName
  129. # define ClassName ClassNameDecorator
  130. # undef FunctionName
  131. # define FunctionName FunctionName(__FUNCTION__,__FILE__,__LINE__)
  132. #endif
  133.  
  134.  
  135. void Caller1()
  136. {
  137. ClassName foo(21);
  138. int val = foo.FunctionName(7,9); // <-- works for captured return value
  139. std::cout << "Mystery Function got " << val << std::endl;
  140. }
  141.  
  142. void Caller2()
  143. {
  144. ClassName foo(42);
  145. // Works for inline as well.
  146. std::cout << "Mystery Function got " << foo.FunctionName(11,13) << std::endl;
  147. }
  148.  
  149. int main(int argc, char** argv)
  150. {
  151. Caller1();
  152. Caller2();
  153. return 0;
  154. }
  155.  
  156. Reporter: ClassName::FunctionName() is being called by Caller1() in test.cpp:56
  157. Mystery Function got 261
  158. Reporter: ClassName::FunctionName() is being called by Caller2() in test.cpp:64
  159. Mystery Function got 702
  160.  
  161. #include <cstdio>
  162.  
  163. void foobar(const char* str = __builtin_FUNCTION()){
  164. std::printf("called by %sn", str);
  165. }
  166.  
  167. int main(){
  168. foobar();
  169. return 0;
  170. }
  171.  
  172. called by main
  173.  
  174. // What: Track last few lines in loci of control, gpl/moshahmed_at_gmail
  175. // Test: gcc -Wall -g -lm -std=c11 track.c
  176. #include <stdio.h>
  177. #include <string.h>
  178.  
  179. #define _DEBUG
  180. #ifdef _DEBUG
  181. #define lsize 255 /* const int lsize=255; -- C++ */
  182. struct locs {
  183. int line[lsize];
  184. char *file[lsize];
  185. char *func[lsize];
  186. int cur; /* cur=0; C++ */
  187. } locs;
  188.  
  189. #define track do {
  190. locs.line[locs.cur]=__LINE__ ;
  191. locs.file[locs.cur]=(char*)__FILE__ ;
  192. locs.func[locs.cur]=(char*) __builtin_FUNCTION() /* __PRETTY_FUNCTION__ -- C++ */ ;
  193. locs.cur=(locs.cur+1) % lsize;
  194. } while(0);
  195.  
  196. void track_start(){
  197. memset(&locs,0, sizeof locs);
  198. }
  199.  
  200. void track_print(){
  201. int i, k;
  202. for (i=0; i<lsize; i++){
  203. k = (locs.cur+i) % lsize;
  204. if (locs.file[k]){
  205. fprintf(stderr,"%d: %s:%d %sn",
  206. k, locs.file[k],
  207. locs.line[k], locs.func[k]);
  208. }
  209. }
  210. }
  211. #else
  212. #define track do {} while(0)
  213. #define track_start() (void)0
  214. #define track_print() (void)0
  215. #endif
  216.  
  217.  
  218. // Sample usage.
  219. void bar(){ track ; }
  220. void foo(){ track ; bar(); }
  221.  
  222. int main(){
  223. int k;
  224. track_start();
  225. for (k=0;k<2;k++)
  226. foo();
  227. track;
  228. track_print();
  229. return 0;
  230. }
Add Comment
Please, Sign In to add comment