y_debug.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*----------------------------------------------------------------------------*-
  2. ==============================
  3. Y Sever Includes - Debug Setup
  4. ==============================
  5. Description:
  6. Ensures debug levels are set and defines debug functions.
  7. General debug levels:
  8. 0 - No debug information.
  9. 1 - Callbacks start and finish.
  10. 2 - Major functions called.
  11. 3 - Major control flow.
  12. 4 - Minor control flow/functions.
  13. 5 - Code steps.
  14. If you use P:0 you get an optional debug print controlled by the global
  15. state ysi_debug - which is either on or off.
  16. Legal:
  17. Version: MPL 1.1
  18. The contents of this file are subject to the Mozilla Public License Version
  19. 1.1 (the "License"); you may not use this file except in compliance with
  20. the License. You may obtain a copy of the License at
  21. http://www.mozilla.org/MPL/
  22. Software distributed under the License is distributed on an "AS IS" basis,
  23. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  24. for the specific language governing rights and limitations under the
  25. License.
  26. The Original Code is the SA:MP script information include.
  27. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  28. Portions created by the Initial Developer are Copyright (C) 2008
  29. the Initial Developer. All Rights Reserved.
  30. Contributors:
  31. ZeeX, koolk
  32. Thanks:
  33. Peter, Cam - Support.
  34. ZeeX - Very productive conversations.
  35. koolk - IsPlayerinAreaEx code.
  36. TheAlpha - Danish translation.
  37. breadfish - German translation.
  38. Fireburn - Dutch translation.
  39. yom - French translation.
  40. 50p - Polish translation.
  41. Zamaroht - Spanish translation.
  42. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  43. for me to strive to better.
  44. Pixels^ - Running XScripters where the idea was born.
  45. Matite - Pestering me to release it and using it.
  46. Very special thanks to:
  47. Thiadmer - PAWN.
  48. Kye/Kalcor - SA:MP.
  49. SA:MP Team past, present and future - SA:MP.
  50. Version:
  51. 1.0
  52. Changelog:
  53. 06/08/10:
  54. Added new syntax.
  55. Added level 0 debugging with state controlled functions.
  56. 15/04/07:
  57. First version.
  58. Functions:
  59. Public:
  60. -
  61. Core:
  62. -
  63. Stock:
  64. -
  65. Static:
  66. -
  67. Inline:
  68. Debug_Code - Runs defined code if a certain level is active.
  69. Debug_Print - Prints the formatted string provided at the given level.
  70. API:
  71. -
  72. Callbacks:
  73. -
  74. Definitions:
  75. DBGP - Simple Debug_Print wrapper.
  76. DBGC - Simple Debug_Code wrapper.
  77. Enums:
  78. -
  79. Macros:
  80. -
  81. Tags:
  82. -
  83. Variables:
  84. Global:
  85. -
  86. Static:
  87. -
  88. Commands:
  89. -
  90. Compile options:
  91. _DEBUG - Debugging level to use.
  92. Operators:
  93. -
  94. -*----------------------------------------------------------------------------*/
  95. #include <YSI\internal\y_version>
  96. #include <YSI\internal\y_funcinc>
  97. #if !defined _DEBUG
  98. #define _DEBUG 0
  99. #endif
  100. #define P:%1(%2); Debug_Print%1(%2);
  101. #define C:%1(%2); Debug_Code%1(%2);
  102. /*----------------------------------------------------------------------------*-
  103. Function:
  104. Debug_Code
  105. Params:
  106. level - Debug level to run the code at.
  107. code - Code to run.
  108. Return:
  109. -
  110. Notes:
  111. Code is not a variable, it's a code chunk and may be written as so:
  112. Debug_Code1(if (bla == 2) { bla++; printf("%d", bla); });
  113. The code must all be on one line to avoid errors.
  114. This isn't really a function as the first parameter is part of the name.
  115. -*----------------------------------------------------------------------------*/
  116. #define DBGC1 Debug_Code1
  117. #define DBGC2 Debug_Code2
  118. #define DBGC3 Debug_Code3
  119. #define DBGC4 Debug_Code4
  120. #define DBGC5 Debug_Code5
  121. #if _DEBUG >= 1
  122. #define Debug_Code1(%1); \
  123. %1
  124. #else
  125. #define Debug_Code1(%1);
  126. #endif
  127. #if _DEBUG >= 2
  128. #define Debug_Code2(%1); \
  129. %1
  130. #else
  131. #define Debug_Code2(%1);
  132. #endif
  133. #if _DEBUG >= 3
  134. #define Debug_Code3(%1); \
  135. %1
  136. #else
  137. #define Debug_Code3(%1);
  138. #endif
  139. #if _DEBUG >= 4
  140. #define Debug_Code4(%1); \
  141. %1
  142. #else
  143. #define Debug_Code4(%1);
  144. #endif
  145. #if _DEBUG >= 5
  146. #define Debug_Code5(%1); \
  147. %1
  148. #else
  149. #define Debug_Code5(%1);
  150. #endif
  151. /*----------------------------------------------------------------------------*-
  152. Function:
  153. Debug_Print
  154. Params:
  155. level - Debug level to print at.
  156. format[] - Format.
  157. ...
  158. Return:
  159. -
  160. Notes:
  161. This isn't really a function as the first parameter is part of the name:
  162. Debug_Print4("variables: %d, %d", i, j);
  163. -*----------------------------------------------------------------------------*/
  164. #define DBGP1 Debug_Print1
  165. #define DBGP2 Debug_Print2
  166. #define DBGP3 Debug_Print3
  167. #define DBGP4 Debug_Print4
  168. #define DBGP5 Debug_Print5
  169. #if _DEBUG >= 1
  170. #define Debug_Print1(%1); \
  171. printf(%1);
  172. #else
  173. #define Debug_Print1(%1);
  174. #endif
  175. #if _DEBUG >= 2
  176. #define Debug_Print2(%1); \
  177. printf(%1);
  178. #else
  179. #define Debug_Print2(%1);
  180. #endif
  181. #if _DEBUG >= 3
  182. #define Debug_Print3(%1); \
  183. printf(%1);
  184. #else
  185. #define Debug_Print3(%1);
  186. #endif
  187. #if _DEBUG >= 4
  188. #define Debug_Print4(%1); \
  189. printf(%1);
  190. #else
  191. #define Debug_Print4(%1);
  192. #endif
  193. #if _DEBUG >= 5
  194. #define Debug_Print5(%1); \
  195. printf(%1);
  196. #else
  197. #define Debug_Print5(%1);
  198. #endif
  199. stock Debug_Print0(str[], {Float,_}:...) <ysi_debug:on>
  200. {
  201. // This uses the variable parameter passing method based on code by Zeex.
  202. // See page 15 of the code optimisations topic.
  203. new
  204. n = (numargs() - 1) * 4;
  205. if (n)
  206. {
  207. new
  208. arg_start,
  209. arg_end;
  210. // Load the real address of the last static parameter. Do this by
  211. // loading the address of the parameter and then adding the value of
  212. // [FRM] (frame pointer).
  213. #emit CONST.alt str
  214. #emit LCTRL 5
  215. #emit ADD
  216. #emit STOR.S.pri arg_start
  217. // Load the address of the last variable parameter. Do this by adding
  218. // the number of variables on the value just loaded.
  219. #emit LOAD.S.alt n
  220. #emit ADD
  221. #emit STOR.S.pri arg_end
  222. // Push the variable arguments. This is done by loading the value of
  223. // each one in reverse order and pushing them. I'd love to be able to
  224. // rewrite this to use the values of pri and alt for comparison, instead
  225. // of having or constantly reload two variables.
  226. do
  227. {
  228. #emit LOAD.I
  229. #emit PUSH.pri
  230. arg_end -= 4;
  231. #emit LOAD.S.pri arg_end
  232. }
  233. while (arg_end > arg_start);
  234. // Push the static parameter.
  235. #emit PUSH.S str
  236. // Now push the number of parameters sent and call the function.
  237. n += 4;
  238. #emit PUSH.S n
  239. #emit SYSREQ.C printf
  240. // Clear the stack, including the return.
  241. n += 4;
  242. #emit LCTRL 4
  243. #emit LOAD.S.alt n
  244. #emit ADD
  245. #emit SCTRL 4
  246. }
  247. else
  248. {
  249. print(str);
  250. }
  251. }
  252. stock Debug_Print0(str[], {Float,_}:...) <ysi_debug:off>
  253. {
  254. #pragma unused str
  255. }
  256. stock Debug_Print0(str[], {Float,_}:...) <>
  257. {
  258. #pragma unused str
  259. }
  260. #if _DEBUG > 0
  261. #define Debug_Print0(%1); \
  262. printf(%1);
  263. #endif