HTTPLogging.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * In order to provide fast and flexible logging, this project uses Cocoa Lumberjack.
  3. *
  4. * The Google Code page has a wealth of documentation if you have any questions.
  5. * https://github.com/robbiehanson/CocoaLumberjack
  6. *
  7. * Here's what you need to know concerning how logging is setup for CocoaHTTPServer:
  8. *
  9. * There are 4 log levels:
  10. * - Error
  11. * - Warning
  12. * - Info
  13. * - Verbose
  14. *
  15. * In addition to this, there is a Trace flag that can be enabled.
  16. * When tracing is enabled, it spits out the methods that are being called.
  17. *
  18. * Please note that tracing is separate from the log levels.
  19. * For example, one could set the log level to warning, and enable tracing.
  20. *
  21. * All logging is asynchronous, except errors.
  22. * To use logging within your own custom files, follow the steps below.
  23. *
  24. * Step 1:
  25. * Import this header in your implementation file:
  26. *
  27. * #import "HTTPLogging.h"
  28. *
  29. * Step 2:
  30. * Define your logging level in your implementation file:
  31. *
  32. * // Log levels: off, error, warn, info, verbose
  33. * static const int httpLogLevel = HTTP_LOG_LEVEL_VERBOSE;
  34. *
  35. * If you wish to enable tracing, you could do something like this:
  36. *
  37. * // Debug levels: off, error, warn, info, verbose
  38. * static const int httpLogLevel = HTTP_LOG_LEVEL_INFO | HTTP_LOG_FLAG_TRACE;
  39. *
  40. * Step 3:
  41. * Replace your NSLog statements with HTTPLog statements according to the severity of the message.
  42. *
  43. * NSLog(@"Fatal error, no dohickey found!"); -> HTTPLogError(@"Fatal error, no dohickey found!");
  44. *
  45. * HTTPLog works exactly the same as NSLog.
  46. * This means you can pass it multiple variables just like NSLog.
  47. **/
  48. #import "DDLog.h"
  49. // Define logging context for every log message coming from the HTTP server.
  50. // The logging context can be extracted from the DDLogMessage from within the logging framework,
  51. // which gives loggers, formatters, and filters the ability to optionally process them differently.
  52. #define HTTP_LOG_CONTEXT 80
  53. // Configure log levels.
  54. #define HTTP_LOG_FLAG_ERROR (1 << 0) // 0...00001
  55. #define HTTP_LOG_FLAG_WARN (1 << 1) // 0...00010
  56. #define HTTP_LOG_FLAG_INFO (1 << 2) // 0...00100
  57. #define HTTP_LOG_FLAG_VERBOSE (1 << 3) // 0...01000
  58. #define HTTP_LOG_LEVEL_OFF 0 // 0...00000
  59. #define HTTP_LOG_LEVEL_ERROR (HTTP_LOG_LEVEL_OFF | HTTP_LOG_FLAG_ERROR) // 0...00001
  60. #define HTTP_LOG_LEVEL_WARN (HTTP_LOG_LEVEL_ERROR | HTTP_LOG_FLAG_WARN) // 0...00011
  61. #define HTTP_LOG_LEVEL_INFO (HTTP_LOG_LEVEL_WARN | HTTP_LOG_FLAG_INFO) // 0...00111
  62. #define HTTP_LOG_LEVEL_VERBOSE (HTTP_LOG_LEVEL_INFO | HTTP_LOG_FLAG_VERBOSE) // 0...01111
  63. // Setup fine grained logging.
  64. // The first 4 bits are being used by the standard log levels (0 - 3)
  65. //
  66. // We're going to add tracing, but NOT as a log level.
  67. // Tracing can be turned on and off independently of log level.
  68. #define HTTP_LOG_FLAG_TRACE (1 << 4) // 0...10000
  69. // Setup the usual boolean macros.
  70. #define HTTP_LOG_ERROR (httpLogLevel & HTTP_LOG_FLAG_ERROR)
  71. #define HTTP_LOG_WARN (httpLogLevel & HTTP_LOG_FLAG_WARN)
  72. #define HTTP_LOG_INFO (httpLogLevel & HTTP_LOG_FLAG_INFO)
  73. #define HTTP_LOG_VERBOSE (httpLogLevel & HTTP_LOG_FLAG_VERBOSE)
  74. #define HTTP_LOG_TRACE (httpLogLevel & HTTP_LOG_FLAG_TRACE)
  75. // Configure asynchronous logging.
  76. // We follow the default configuration,
  77. // but we reserve a special macro to easily disable asynchronous logging for debugging purposes.
  78. #define HTTP_LOG_ASYNC_ENABLED YES
  79. #define HTTP_LOG_ASYNC_ERROR ( NO && HTTP_LOG_ASYNC_ENABLED)
  80. #define HTTP_LOG_ASYNC_WARN (YES && HTTP_LOG_ASYNC_ENABLED)
  81. #define HTTP_LOG_ASYNC_INFO (YES && HTTP_LOG_ASYNC_ENABLED)
  82. #define HTTP_LOG_ASYNC_VERBOSE (YES && HTTP_LOG_ASYNC_ENABLED)
  83. #define HTTP_LOG_ASYNC_TRACE (YES && HTTP_LOG_ASYNC_ENABLED)
  84. // Define logging primitives.
  85. #define HTTPLogError(frmt, ...) LOG_OBJC_MAYBE(HTTP_LOG_ASYNC_ERROR, httpLogLevel, HTTP_LOG_FLAG_ERROR, \
  86. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)
  87. #define HTTPLogWarn(frmt, ...) LOG_OBJC_MAYBE(HTTP_LOG_ASYNC_WARN, httpLogLevel, HTTP_LOG_FLAG_WARN, \
  88. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)
  89. #define HTTPLogInfo(frmt, ...) LOG_OBJC_MAYBE(HTTP_LOG_ASYNC_INFO, httpLogLevel, HTTP_LOG_FLAG_INFO, \
  90. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)
  91. #define HTTPLogVerbose(frmt, ...) LOG_OBJC_MAYBE(HTTP_LOG_ASYNC_VERBOSE, httpLogLevel, HTTP_LOG_FLAG_VERBOSE, \
  92. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)
  93. #define HTTPLogTrace() LOG_OBJC_MAYBE(HTTP_LOG_ASYNC_TRACE, httpLogLevel, HTTP_LOG_FLAG_TRACE, \
  94. HTTP_LOG_CONTEXT, @"%@[%p]: %@", THIS_FILE, self, THIS_METHOD)
  95. #define HTTPLogTrace2(frmt, ...) LOG_OBJC_MAYBE(HTTP_LOG_ASYNC_TRACE, httpLogLevel, HTTP_LOG_FLAG_TRACE, \
  96. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)
  97. #define HTTPLogCError(frmt, ...) LOG_C_MAYBE(HTTP_LOG_ASYNC_ERROR, httpLogLevel, HTTP_LOG_FLAG_ERROR, \
  98. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)
  99. #define HTTPLogCWarn(frmt, ...) LOG_C_MAYBE(HTTP_LOG_ASYNC_WARN, httpLogLevel, HTTP_LOG_FLAG_WARN, \
  100. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)
  101. #define HTTPLogCInfo(frmt, ...) LOG_C_MAYBE(HTTP_LOG_ASYNC_INFO, httpLogLevel, HTTP_LOG_FLAG_INFO, \
  102. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)
  103. #define HTTPLogCVerbose(frmt, ...) LOG_C_MAYBE(HTTP_LOG_ASYNC_VERBOSE, httpLogLevel, HTTP_LOG_FLAG_VERBOSE, \
  104. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)
  105. #define HTTPLogCTrace() LOG_C_MAYBE(HTTP_LOG_ASYNC_TRACE, httpLogLevel, HTTP_LOG_FLAG_TRACE, \
  106. HTTP_LOG_CONTEXT, @"%@[%p]: %@", THIS_FILE, self, __FUNCTION__)
  107. #define HTTPLogCTrace2(frmt, ...) LOG_C_MAYBE(HTTP_LOG_ASYNC_TRACE, httpLogLevel, HTTP_LOG_FLAG_TRACE, \
  108. HTTP_LOG_CONTEXT, frmt, ##__VA_ARGS__)