1
0

log.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * <library name="log"
  3. * version="1.0"
  4. * description="Adds better logging functions to PAWN with different log levels."
  5. * author="dy1zan" author_url="https://github.com/dy1zan"
  6. * >
  7. *
  8. * <section>
  9. * Log levels
  10. * </section>
  11. * <ul>
  12. * <symbol name="LOGL_DEBUG">Debug</symbol>
  13. * <symbol name="LOGL_INFO">Information</symbol>
  14. * <symbol name="LOGL_WARNING">Warning</symbol>
  15. * <symbol name="LOGL_ERROR">Error</symbol>
  16. * </ul>
  17. * <section>
  18. * Functions
  19. * </section>
  20. * <ul>
  21. * <symbol name="log_level">Defines the log level to use.</symbol>
  22. * <symbol name="log_debug">Prints a debug message if the log level is set to LOGL_DEBUG.</symbol>
  23. * <symbol name="log_info">Prints an information message.</symbol>
  24. * <symbol name="log_warning">Prints a warning message with a backtrace.</symbol>
  25. * <symbol name="log_error">Prints an error message with a backtrace, and if DEVMODE is defined as "1", shutdowns the server.</symbol>
  26. * </ul>
  27. * <section>
  28. * Examples
  29. * </section>
  30. * <subsection>
  31. * Debug messages
  32. * </subsection>
  33. * <p>The following sets the log level (ideally in OnScriptInit()), and then prints a debug message.
  34. * <code>
  35. * Log:level(LOGL_DEBUG);<br />
  36. * Log:debug("vehicles.inc", "Created %p's vehicle (%s) with vehicle SQL ID=%d", playerid, vehiclename, sqlid);
  37. * </code>
  38. * </p>
  39. *
  40. * </library>
  41. *//** */
  42. #if defined _log_included
  43. #endinput
  44. #endif
  45. #define _log_included
  46. // Dependencies
  47. #include <a_samp>
  48. #include <crashdetect>
  49. #include <formatex>
  50. #include <YSI\y_va>
  51. // Define the namespace
  52. #define Log: log_
  53. // Definitions
  54. #define MAX_LOG_LENGTH 512
  55. enum {
  56. LOGL_DEBUG = 1,
  57. LOGL_INFO,
  58. LOGL_WARNING,
  59. LOGL_ERROR,
  60. LOGL_NONE
  61. }
  62. static LOG_LEVEL = LOGL_DEBUG;
  63. // Forward declarations
  64. forward Log:level(level);
  65. forward Log:debug(prefix[], text[], va_args<>);
  66. forward Log:info(prefix[], text[], va_args<>);
  67. forward Log:warning(prefix[], text[], va_args<>);
  68. forward Log:error(prefix[], text[], va_args<>);
  69. public Log:level(level) {
  70. LOG_LEVEL = level;
  71. Log:info("log.inc", "The log level has been set to %d.", level);
  72. }
  73. /*------------------------------------------------------------*//**
  74. * <remarks>
  75. * Prints a debug message in the console/log. This is later to
  76. * be changed to print to a new file, instead of server_log.txt
  77. * </remarks>
  78. * <seealso name="log"/>
  79. *//*------------------------------------------------------------**/
  80. public Log:debug(prefix[], text[], {Float,_}:...) {
  81. if(LOG_LEVEL != LOGL_DEBUG) {
  82. return;
  83. }
  84. new
  85. out[MAX_LOG_LENGTH]
  86. ;
  87. formatex(out, _, text, ___(2));
  88. formatex(out, _, "DEBUG [%s]: %s", prefix, out);
  89. print(out);
  90. }
  91. /*------------------------------------------------------------*//**
  92. * <remarks>Prints an informative message in the console/log.</remarks>
  93. * <seealso name="log"/>
  94. *//*------------------------------------------------------------**/
  95. public Log:info(prefix[], text[], {Float,_}:...) {
  96. if(LOG_LEVEL > LOGL_INFO) {
  97. return;
  98. }
  99. new
  100. out[MAX_LOG_LENGTH]
  101. ;
  102. formatex(out, _, text, ___(2));
  103. formatex(out, _, "INFO [%s]: %s", prefix, out);
  104. print(out);
  105. }
  106. /*------------------------------------------------------------*//**
  107. * <remarks>Prints a warning message in the console/log.</remarks>
  108. * <seealso name="log"/>
  109. *//*------------------------------------------------------------**/
  110. public Log:warning(prefix[], text[], {Float,_}:...) {
  111. if(LOG_LEVEL > LOGL_WARNING) {
  112. return;
  113. }
  114. new
  115. out[MAX_LOG_LENGTH]
  116. ;
  117. formatex(out, _, text, ___(2));
  118. formatex(out, _, "WARNING [%s]: %s", prefix, out);
  119. print(out);
  120. PrintAmxBacktrace();
  121. }
  122. /*------------------------------------------------------------*//**
  123. * <remarks>
  124. * Prints an error message in the console/log. If DEVMODE is defined as <i>1</i>,
  125. * this will shutdown the server
  126. * </remarks>
  127. * <seealso name="log"/>
  128. *//*------------------------------------------------------------**/
  129. public Log:error(prefix[], text[], {Float,_}:...) {
  130. if(LOG_LEVEL > LOGL_ERROR) {
  131. return;
  132. }
  133. new
  134. out[MAX_LOG_LENGTH]
  135. ;
  136. formatex(out, _, text, ___(2));
  137. formatex(out, _, "ERROR [%s]: %s", prefix, out);
  138. print(out);
  139. PrintAmxBacktrace();
  140. #if defined DEVMODE
  141. #if DEVMODE == 1
  142. //Generate crash, credits to Southclaws
  143. new File:f = fopen("nonexistentfile", io_read), tmp[1];
  144. fread(f, tmp);
  145. fclose(f);
  146. #endif
  147. #endif
  148. }