y_debug.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 and timers.
  10. 2 - Remote functions.
  11. 3 - Stock functions.
  12. 4 - Static functions.
  13. 5 - Code.
  14. 6 - Loops.
  15. 7 - Extra loop code.
  16. If you use P:0 you get an optional debug print controlled by the global
  17. state ysi_debug - which is either on or off.
  18. Legal:
  19. Version: MPL 1.1
  20. The contents of this file are subject to the Mozilla Public License Version
  21. 1.1 (the "License"); you may not use this file except in compliance with
  22. the License. You may obtain a copy of the License at
  23. http://www.mozilla.org/MPL/
  24. Software distributed under the License is distributed on an "AS IS" basis,
  25. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  26. for the specific language governing rights and limitations under the
  27. License.
  28. The Original Code is the YSI debug include.
  29. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  30. Portions created by the Initial Developer are Copyright (C) 2011
  31. the Initial Developer. All Rights Reserved.
  32. Contributors:
  33. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  34. Thanks:
  35. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  36. ZeeX - Very productive conversations.
  37. koolk - IsPlayerinAreaEx code.
  38. TheAlpha - Danish translation.
  39. breadfish - German translation.
  40. Fireburn - Dutch translation.
  41. yom - French translation.
  42. 50p - Polish translation.
  43. Zamaroht - Spanish translation.
  44. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  45. for me to strive to better.
  46. Pixels^ - Running XScripters where the idea was born.
  47. Matite - Pestering me to release it and using it.
  48. Very special thanks to:
  49. Thiadmer - PAWN, whose limits continue to amaze me!
  50. Kye/Kalcor - SA:MP.
  51. SA:MP Team past, present and future - SA:MP.
  52. Version:
  53. 1.0
  54. Changelog:
  55. 06/08/10:
  56. Added new syntax.
  57. Added level 0 debugging with state controlled functions.
  58. 15/04/07:
  59. First version.
  60. Functions:
  61. Public:
  62. -
  63. Core:
  64. -
  65. Stock:
  66. -
  67. Static:
  68. -
  69. Inline:
  70. Debug_Code - Runs defined code if a certain level is active.
  71. Debug_Print - Prints the formatted string provided at the given level.
  72. API:
  73. -
  74. Callbacks:
  75. -
  76. Definitions:
  77. P:<0-6> - Print a message.
  78. P:C - Run debug code.
  79. P:E - Print an error message.
  80. P:W - Print a warning message.
  81. Enums:
  82. -
  83. Macros:
  84. -
  85. Tags:
  86. -
  87. Variables:
  88. Global:
  89. -
  90. Static:
  91. -
  92. Commands:
  93. -
  94. Compile options:
  95. _DEBUG - Debugging level to use.
  96. Operators:
  97. -
  98. </remarks>
  99. \**--------------------------------------------------------------------------**/
  100. #if defined _INC_y_debug
  101. #endinput
  102. #endif
  103. #define _INC_y_debug
  104. #include "..\YSI_Internal\y_version"
  105. #include "..\YSI_Internal\y_funcinc"
  106. #include "..\YSI_Server\y_scriptinit"
  107. #if !defined _DEBUG
  108. #define _DEBUG 0
  109. #endif
  110. stock YSI_gDebugLevel = 0;
  111. #define P:%1(%2); Debug_Print%1(%2);
  112. #define C:%1(%2); Debug_Code%1(%2);
  113. /**--------------------------------------------------------------------------**\
  114. <summary>Debug_Code</summary>
  115. <param name="level">Debug level to run the code at.</param>
  116. <param name="code">Code to run.</param>
  117. <returns>
  118. -
  119. </returns>
  120. <remarks>
  121. Code is not a variable, it's a code chunk and may be written as so:
  122. Debug_Code1(if (bla == 2) { bla++; printf("%d", bla); });
  123. The code must all be on one line to avoid errors.
  124. This isn't really a function as the first parameter is part of the name.
  125. </remarks>
  126. \**--------------------------------------------------------------------------**/
  127. #if _DEBUG == -1
  128. #define Debug_Code1(%1); { if (YSI_gDebugLevel >= 1) { %1 }}
  129. #define Debug_Code2(%1); { if (YSI_gDebugLevel >= 2) { %1 }}
  130. #define Debug_Code3(%1); { if (YSI_gDebugLevel >= 3) { %1 }}
  131. #define Debug_Code4(%1); { if (YSI_gDebugLevel >= 4) { %1 }}
  132. #define Debug_Code5(%1); { if (YSI_gDebugLevel >= 5) { %1 }}
  133. #define Debug_Code6(%1); { if (YSI_gDebugLevel >= 6) { %1 }}
  134. #define Debug_Code7(%1); { if (YSI_gDebugLevel >= 7) { %1 }}
  135. #else
  136. #if _DEBUG >= 1
  137. #define Debug_Code1(%1); %1
  138. #else
  139. #define Debug_Code1(%1);
  140. #endif
  141. #if _DEBUG >= 2
  142. #define Debug_Code2(%1); %1
  143. #else
  144. #define Debug_Code2(%1);
  145. #endif
  146. #if _DEBUG >= 3
  147. #define Debug_Code3(%1); %1
  148. #else
  149. #define Debug_Code3(%1);
  150. #endif
  151. #if _DEBUG >= 4
  152. #define Debug_Code4(%1); %1
  153. #else
  154. #define Debug_Code4(%1);
  155. #endif
  156. #if _DEBUG >= 5
  157. #define Debug_Code5(%1); %1
  158. #else
  159. #define Debug_Code5(%1);
  160. #endif
  161. #if _DEBUG >= 6
  162. #define Debug_Code6(%1); %1
  163. #else
  164. #define Debug_Code6(%1);
  165. #endif
  166. #if _DEBUG >= 7
  167. #define Debug_Code7(%1); %1
  168. #else
  169. #define Debug_Code7(%1);
  170. #endif
  171. #endif
  172. #if _DEBUG != 0
  173. #define Debug_CodeX(%1); %1
  174. #else
  175. #define Debug_CodeX(%1);
  176. #endif
  177. /**--------------------------------------------------------------------------**\
  178. <summary>Debug_Print</summary>
  179. <param name="level">Debug level to print at.</param>
  180. <param name="format[]">Format.</param>
  181. <param name="..."></param>
  182. <returns>
  183. -
  184. </returns>
  185. <remarks>
  186. This isn't really a function as the first parameter is part of the name:
  187. Debug_Print4("variables: %d, %d", i, j);
  188. </remarks>
  189. \**--------------------------------------------------------------------------**/
  190. #if _DEBUG == -1
  191. #define Debug_Print1(%1); { if (YSI_gDebugLevel >= 1) printf(%1); }
  192. #define Debug_Print2(%1); { if (YSI_gDebugLevel >= 2) printf(%1); }
  193. #define Debug_Print3(%1); { if (YSI_gDebugLevel >= 3) printf(%1); }
  194. #define Debug_Print4(%1); { if (YSI_gDebugLevel >= 4) printf(%1); }
  195. #define Debug_Print5(%1); { if (YSI_gDebugLevel >= 5) printf(%1); }
  196. #define Debug_Print6(%1); { if (YSI_gDebugLevel >= 6) printf(%1); }
  197. #define Debug_Print7(%1); { if (YSI_gDebugLevel >= 7) printf(%1); }
  198. #else
  199. #if _DEBUG >= 1
  200. #define Debug_Print1(%1); printf(%1);
  201. #else
  202. #define Debug_Print1(%1);
  203. #endif
  204. #if _DEBUG >= 2
  205. #define Debug_Print2(%1); printf(%1);
  206. #else
  207. #define Debug_Print2(%1);
  208. #endif
  209. #if _DEBUG >= 3
  210. #define Debug_Print3(%1); printf(%1);
  211. #else
  212. #define Debug_Print3(%1);
  213. #endif
  214. #if _DEBUG >= 4
  215. #define Debug_Print4(%1); printf(%1);
  216. #else
  217. #define Debug_Print4(%1);
  218. #endif
  219. #if _DEBUG >= 5
  220. #define Debug_Print5(%1); printf(%1);
  221. #else
  222. #define Debug_Print5(%1);
  223. #endif
  224. #if _DEBUG >= 6
  225. #define Debug_Print6(%1); printf(%1);
  226. #else
  227. #define Debug_Print6(%1);
  228. #endif
  229. #if _DEBUG >= 7
  230. #define Debug_Print7(%1); printf(%1);
  231. #else
  232. #define Debug_Print7(%1);
  233. #endif
  234. #endif
  235. #define Debug_PrintE(%1); \
  236. Debug_Print0("\7\7\7*** YSI Error: " #%1);
  237. #define Debug_PrintW(%1); \
  238. Debug_Print0("\7*** YSI Warning: " #%1);
  239. #define Debug_PrintI(%1); \
  240. Debug_Print0("*** YSI Info: " #%1);
  241. #define Debug_PrintF(%1); \
  242. Debug_Print0("\7\7\7\7\7*** YSI Fatal Error: " #%1);
  243. #define Debug_PrintC(%1); \
  244. Debug_CodeX(%1);
  245. stock Debug_Print0(const str[], {Float,_}:...) <ysi_debug : on>
  246. {
  247. static tmp1, tmp2;
  248. #emit POP.pri
  249. #emit STOR.pri tmp1
  250. #emit POP.alt
  251. #emit STOR.alt tmp2
  252. #emit SYSREQ.C printf
  253. #emit PUSH tmp2
  254. #emit PUSH tmp1
  255. #pragma unused str
  256. return 0;
  257. }
  258. stock Debug_Print0(const str[], {Float,_}:...) <>
  259. {
  260. #pragma unused str
  261. return 0;
  262. }
  263. stock Debug_PrintArray(arr[], size)
  264. {
  265. new
  266. str[96];
  267. switch (size)
  268. {
  269. case 0:
  270. str = "<>";
  271. case 1:
  272. format(str, sizeof (str), "<%d>", arr[0]);
  273. case 2:
  274. format(str, sizeof (str), "<%d, %d>", arr[0], arr[1]);
  275. case 3:
  276. format(str, sizeof (str), "<%d, %d, %d>", arr[0], arr[1], arr[2]);
  277. case 4:
  278. format(str, sizeof (str), "<%d, %d, %d, %d>", arr[0], arr[1], arr[2], arr[3]);
  279. case 5:
  280. format(str, sizeof (str), "<%d, %d, %d, %d, %d>", arr[0], arr[1], arr[2], arr[3], arr[4]);
  281. default:
  282. format(str, sizeof (str), "<%d, %d, %d, %d, %d, ... (+ %d)>", arr[0], arr[1], arr[2], arr[3], arr[4], size - 5);
  283. }
  284. return str;
  285. }
  286. public OnScriptInit()
  287. {
  288. Debug_SetState();
  289. new
  290. s;
  291. // Test the ADDRESS of the variable, not the value.
  292. #emit CONST.pri YSI_FILTERSCRIPT
  293. #emit STOR.S.pri s
  294. if (s)
  295. {
  296. //goto Debug_OnScriptInit_no_fault();
  297. #if defined Debug_OnScriptInit
  298. return Debug_OnScriptInit();
  299. #else
  300. return 1;
  301. #endif
  302. }
  303. P:F("YSI_FILTERSCRIPT == 0");
  304. while (s != 10000000) ++s;
  305. #emit CONST.pri 0
  306. #emit SCTRL 6
  307. return 1;
  308. }
  309. static stock Debug_SetState() <ysi_debug : off>
  310. {
  311. }
  312. static stock Debug_SetState() <>
  313. {
  314. state ysi_debug : on;
  315. }
  316. #undef OnScriptInit
  317. #define OnScriptInit Debug_OnScriptInit
  318. #if defined Debug_OnScriptInit
  319. forward Debug_OnScriptInit();
  320. #endif
  321. stock DebugLevel(level = -1)
  322. {
  323. if (0 <= level <= 7)
  324. {
  325. YSI_gDebugLevel = level;
  326. }
  327. return YSI_gDebugLevel;
  328. }