DDLogMacros.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2016, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. // Disable legacy macros
  16. #ifndef DD_LEGACY_MACROS
  17. #define DD_LEGACY_MACROS 0
  18. #endif
  19. #import "DDLog.h"
  20. /**
  21. * The constant/variable/method responsible for controlling the current log level.
  22. **/
  23. #ifndef LOG_LEVEL_DEF
  24. #define LOG_LEVEL_DEF ddLogLevel
  25. #endif
  26. /**
  27. * Whether async should be used by log messages, excluding error messages that are always sent sync.
  28. **/
  29. #ifndef LOG_ASYNC_ENABLED
  30. #define LOG_ASYNC_ENABLED YES
  31. #endif
  32. /**
  33. * These are the two macros that all other macros below compile into.
  34. * These big multiline macros makes all the other macros easier to read.
  35. **/
  36. #define LOG_MACRO(isAsynchronous, lvl, flg, ctx, atag, fnct, frmt, ...) \
  37. [DDLog log : isAsynchronous \
  38. level : lvl \
  39. flag : flg \
  40. context : ctx \
  41. file : __FILE__ \
  42. function : fnct \
  43. line : __LINE__ \
  44. tag : atag \
  45. format : (frmt), ## __VA_ARGS__]
  46. #define LOG_MACRO_TO_DDLOG(ddlog, isAsynchronous, lvl, flg, ctx, atag, fnct, frmt, ...) \
  47. [ddlog log : isAsynchronous \
  48. level : lvl \
  49. flag : flg \
  50. context : ctx \
  51. file : __FILE__ \
  52. function : fnct \
  53. line : __LINE__ \
  54. tag : atag \
  55. format : (frmt), ## __VA_ARGS__]
  56. /**
  57. * Define version of the macro that only execute if the log level is above the threshold.
  58. * The compiled versions essentially look like this:
  59. *
  60. * if (logFlagForThisLogMsg & ddLogLevel) { execute log message }
  61. *
  62. * When LOG_LEVEL_DEF is defined as ddLogLevel.
  63. *
  64. * As shown further below, Lumberjack actually uses a bitmask as opposed to primitive log levels.
  65. * This allows for a great amount of flexibility and some pretty advanced fine grained logging techniques.
  66. *
  67. * Note that when compiler optimizations are enabled (as they are for your release builds),
  68. * the log messages above your logging threshold will automatically be compiled out.
  69. *
  70. * (If the compiler sees LOG_LEVEL_DEF/ddLogLevel declared as a constant, the compiler simply checks to see
  71. * if the 'if' statement would execute, and if not it strips it from the binary.)
  72. *
  73. * We also define shorthand versions for asynchronous and synchronous logging.
  74. **/
  75. #define LOG_MAYBE(async, lvl, flg, ctx, tag, fnct, frmt, ...) \
  76. do { if(lvl & flg) LOG_MACRO(async, lvl, flg, ctx, tag, fnct, frmt, ##__VA_ARGS__); } while(0)
  77. #define LOG_MAYBE_TO_DDLOG(ddlog, async, lvl, flg, ctx, tag, fnct, frmt, ...) \
  78. do { if(lvl & flg) LOG_MACRO_TO_DDLOG(ddlog, async, lvl, flg, ctx, tag, fnct, frmt, ##__VA_ARGS__); } while(0)
  79. /**
  80. * Ready to use log macros with no context or tag.
  81. **/
  82. #define DDLogError(frmt, ...) LOG_MAYBE(NO, LOG_LEVEL_DEF, DDLogFlagError, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)
  83. #define DDLogWarn(frmt, ...) LOG_MAYBE(LOG_ASYNC_ENABLED, LOG_LEVEL_DEF, DDLogFlagWarning, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)
  84. #define DDLogInfo(frmt, ...) LOG_MAYBE(LOG_ASYNC_ENABLED, LOG_LEVEL_DEF, DDLogFlagInfo, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)
  85. #define DDLogDebug(frmt, ...) LOG_MAYBE(LOG_ASYNC_ENABLED, LOG_LEVEL_DEF, DDLogFlagDebug, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)
  86. #define DDLogVerbose(frmt, ...) LOG_MAYBE(LOG_ASYNC_ENABLED, LOG_LEVEL_DEF, DDLogFlagVerbose, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)
  87. #define DDLogErrorToDDLog(ddlog, frmt, ...) LOG_MAYBE_TO_DDLOG(ddlog, NO, LOG_LEVEL_DEF, DDLogFlagError, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)
  88. #define DDLogWarnToDDLog(ddlog, frmt, ...) LOG_MAYBE_TO_DDLOG(ddlog, LOG_ASYNC_ENABLED, LOG_LEVEL_DEF, DDLogFlagWarning, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)
  89. #define DDLogInfoToDDLog(ddlog, frmt, ...) LOG_MAYBE_TO_DDLOG(ddlog, LOG_ASYNC_ENABLED, LOG_LEVEL_DEF, DDLogFlagInfo, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)
  90. #define DDLogDebugToDDLog(ddlog, frmt, ...) LOG_MAYBE_TO_DDLOG(ddlog, LOG_ASYNC_ENABLED, LOG_LEVEL_DEF, DDLogFlagDebug, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)
  91. #define DDLogVerboseToDDLog(ddlog, frmt, ...) LOG_MAYBE_TO_DDLOG(ddlog, LOG_ASYNC_ENABLED, LOG_LEVEL_DEF, DDLogFlagVerbose, 0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)