Advertisement
Guest User

logger

a guest
Nov 25th, 2018
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. // built-in include guard removal
  2. // just in case the user has a local dependency with the same file name
  3. #if defined _inc_logger
  4. #undef _inc_logger
  5. #endif
  6. // custom include-guard to ensure we don't duplicate
  7. #if defined _logger_included
  8. #endinput
  9. #endif
  10. #define _logger_included
  11.  
  12. #include <a_samp>
  13. #include <crashdetect>
  14.  
  15.  
  16. // An event represents a single log event
  17. // a log event contains one or more fields
  18. // a field is composed of a name, an equals sign and a value
  19. // if a value is a string, it is enclosed in quotes
  20. // if a value is a string that contains quotes, they are escaped
  21.  
  22. #if !defined MAX_EVENT_FIELDS
  23. #define MAX_EVENT_FIELDS (8)
  24. #endif
  25.  
  26. #if !defined MAX_FIELD_NAME
  27. #define MAX_FIELD_NAME (16)
  28. #endif
  29.  
  30. #if !defined MAX_FIELD_VALUE
  31. #define MAX_FIELD_VALUE (256)
  32. #endif
  33.  
  34. #define MAX_FIELD_LENGTH (MAX_FIELD_NAME + 3 + MAX_FIELD_VALUE)
  35. #define MAX_EVENT_LENGTH (MAX_EVENT_FIELDS * MAX_FIELD_LENGTH)
  36.  
  37. forward LOGGER_FIELD:_i(field[], value);
  38. forward LOGGER_FIELD:_f(field[], Float:value);
  39. forward LOGGER_FIELD:_s(field[], value[]);
  40.  
  41. // log is a structured log event printer, it takes a text parameter which describes the event
  42. // followed by zero or more `LOGGER_FIELD` strings which are generated using `_i`, `_f` and `_s`.
  43. // example:
  44. // log("an event has happened with values",
  45. // _i("worldid", 4),
  46. // _f("health", 64.5),
  47. // _s("message", "tim said \"hello\".")
  48. // );
  49. // this would be formatted as:
  50. // text="an event has happened with values" worldid=4 health=64.500000 message="tim said \"hello\"."
  51. // quotes in messages are escaped in order to make parsing the logs easier.
  52. stock log(text[], LOGGER_FIELD:...) {
  53. new total = numargs();
  54.  
  55. if (total == 1) {
  56. print(_:_s("text", text));
  57. return;
  58. }
  59.  
  60. new result[MAX_EVENT_LENGTH];
  61. strcat(result, _:_s("text", text));
  62.  
  63. for(new arg = 1; arg < total && arg < MAX_EVENT_FIELDS; ++arg) {
  64. new field[MAX_FIELD_LENGTH];
  65. for(new ch; ch < MAX_FIELD_LENGTH; ++ch) {
  66. field[ch] = getarg(arg, ch);
  67. if(field[ch] == EOS) {
  68. strcat(result, " ");
  69. strcat(result, field);
  70. break;
  71. }
  72. }
  73. }
  74.  
  75. print(result);
  76. }
  77.  
  78. // dbg is a conditional version of log, it behaves the same but with one extra parameter which is
  79. // the name of the debug handler. Internally, this is just an SVar and simply checks if the value
  80. // is non-zero before continuing the print the event.
  81. stock dbg(handler[], text[], LOGGER_FIELD:...) {
  82. if(GetSVarInt(handler) == 0) {
  83. return;
  84. }
  85.  
  86. new total = numargs();
  87.  
  88. if (total == 2) {
  89. print(_:_s("text", text));
  90. return;
  91. }
  92.  
  93. new result[MAX_EVENT_LENGTH];
  94. strcat(result, _:_s("text", text));
  95.  
  96. for(new arg = 2; arg < total && arg < MAX_EVENT_FIELDS; ++arg) {
  97. new field[MAX_FIELD_LENGTH];
  98. for(new ch; ch < MAX_FIELD_LENGTH; ++ch) {
  99. field[ch] = getarg(arg, ch);
  100. if(field[ch] == EOS) {
  101. strcat(result, " ");
  102. strcat(result, field);
  103. break;
  104. }
  105. }
  106. }
  107.  
  108. // todo: process crashdetect backtrace to grab call site
  109.  
  110. print(result);
  111. }
  112.  
  113. stock err(text[], LOGGER_FIELD:...) {
  114. new total = numargs();
  115.  
  116. if (total == 1) {
  117. print(_:_s("text", text));
  118. return;
  119. }
  120.  
  121. new result[MAX_EVENT_LENGTH];
  122. strcat(result, _:_s("text", text));
  123.  
  124. for(new arg = 1; arg < total && arg < MAX_EVENT_FIELDS; ++arg) {
  125. new field[MAX_FIELD_LENGTH];
  126. for(new ch; ch < MAX_FIELD_LENGTH; ++ch) {
  127. field[ch] = getarg(arg, ch);
  128. if(field[ch] == EOS) {
  129. strcat(result, " ");
  130. strcat(result, field);
  131. break;
  132. }
  133. }
  134. }
  135.  
  136. print(result);
  137. PrintAmxBacktrace();
  138. }
  139.  
  140. stock fatal(text[], LOGGER_FIELD:...) {
  141. new total = numargs();
  142.  
  143. if (total == 1) {
  144. print(_:_s("text", text));
  145. return;
  146. }
  147.  
  148. new result[MAX_EVENT_LENGTH];
  149. strcat(result, text);
  150.  
  151. for(new arg = 1; arg < total && arg < MAX_EVENT_FIELDS; ++arg) {
  152. new field[MAX_FIELD_LENGTH];
  153. for(new ch; ch < MAX_FIELD_LENGTH; ++ch) {
  154. field[ch] = getarg(arg, ch);
  155. if(field[ch] == EOS) {
  156. strcat(result, " ");
  157. strcat(result, field);
  158. break;
  159. }
  160. }
  161. }
  162.  
  163. print(result);
  164. PrintAmxBacktrace();
  165.  
  166. // trigger a crash to escape the gamemode
  167. new File:f = fopen("nonexistentfile", io_read), tmp[1];
  168. fread(f, tmp);
  169. fclose(f);
  170. }
  171.  
  172. stock logger_debug(handler[], bool:toggle) {
  173. if(toggle) {
  174. SetSVarInt(handler, 1);
  175. } else {
  176. DeleteSVar(handler);
  177. }
  178. return 1;
  179. }
  180.  
  181. // _i is a log field converter for integers, it takes a named integer and
  182. // returns a log field representation of it.
  183. stock LOGGER_FIELD:_i(field[], value) {
  184. new result[MAX_FIELD_NAME + 1 + 12];
  185. format(result, sizeof(result), "%s=%d", field, value);
  186. return LOGGER_FIELD:result;
  187. }
  188.  
  189. // _f is a log field converter for floats, it takes a named float and
  190. // returns a log field representation of it.
  191. stock LOGGER_FIELD:_f(field[], Float:value) {
  192. new result[MAX_FIELD_NAME + 1 + 64];
  193. format(result, sizeof(result), "%s=%f", field, value);
  194. return LOGGER_FIELD:result;
  195. }
  196.  
  197. // _i is a log field converter for strings, it takes a named string and
  198. // returns a log field representation of it.
  199. stock LOGGER_FIELD:_s(field[], value[]) {
  200. new quoted[MAX_FIELD_VALUE];
  201. quote_escape(value, MAX_FIELD_VALUE, quoted, MAX_FIELD_VALUE);
  202.  
  203. new result[MAX_FIELD_NAME + 3 + MAX_FIELD_VALUE];
  204. format(result, sizeof(result), "%s=\"%s\"", field, quoted);
  205. return LOGGER_FIELD:result;
  206. }
  207.  
  208. // quote_escape returns a copy of value with all quotes escaped with backslashes
  209. stock quote_escape(input[], inputLen, output[], outputLen) {
  210. new j;
  211.  
  212. for(new i; input[i] != EOS && i < inputLen && j < (outputLen - 1); ++i) {
  213. if(input[i] == '"') {
  214. output[j++] = '\\';
  215. }
  216. output[j++] = input[i];
  217. }
  218. output[j] = EOS;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement