common.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef COMMON_H_
  2. #define COMMON_H_
  3. // Foundation is nice to have
  4. #ifdef __OBJC__
  5. #import <Foundation/Foundation.h>
  6. #endif
  7. // Filename macro
  8. #ifndef __FILENAME__
  9. #define __FILENAME__ ((strrchr(__FILE__, '/') ?: __FILE__ - 1) + 1)
  10. #endif
  11. #define __SRC_FILENAME__ \
  12. ((common_strrstr(__FILE__, "/src/") ?: __FILE__ - 1) + 1)
  13. // log info, warning and error message
  14. // DLOG(format[, ...]) -- log a debug message
  15. #if defined(__OBJC__)
  16. #define _LOG(prefixch, fmt, ...) \
  17. NSLog((NSString*)(CFSTR("%c [%s:%d] " fmt)), prefixch, \
  18. __SRC_FILENAME__, __LINE__, ##__VA_ARGS__)
  19. #else
  20. #define _LOG(prefixch, fmt, ...) \
  21. fprintf(stderr, "%c [%s:%d] " fmt, prefixch, \
  22. __SRC_FILENAME__, __LINE__, ##__VA_ARGS__)
  23. #endif
  24. #ifdef LOG_SILENT
  25. #define ILOG(...) do{}while(0)
  26. #else
  27. #define ILOG(...) _LOG('I', __VA_ARGS__)
  28. #endif
  29. #define WLOG(...) _LOG('W', __VA_ARGS__)
  30. #define ELOG(...) _LOG('E', __VA_ARGS__)
  31. // Debug/development utilities
  32. #if !defined(NDEBUG)
  33. #ifndef _DEBUG
  34. #define _DEBUG 1
  35. #endif
  36. // shorthand to include and evaluate <x> only for debug builds
  37. #define IFDEBUG(x) do{ x }while(0)
  38. #define DLOG(...) _LOG('D', __VA_ARGS__)
  39. #define DLOG_TRACE() _LOG('T', "%s", __func__)
  40. // log an expression
  41. #ifdef __OBJC__
  42. // trace "<ObjCClass: 0xAddress> selector"
  43. #define DLOG_TRACE_M() \
  44. _LOG('T', "%@ %@", self, NSStringFromSelector(_cmd));
  45. NSString *VTPG_DDToStringFromTypeAndValue(const char *tc, void *v);
  46. #define DLOG_EXPR(_X_) do{\
  47. __typeof__(_X_) _Y_ = (_X_);\
  48. const char * _TYPE_CODE_ = @encode(__typeof__(_X_));\
  49. NSString *_STR_ = VTPG_DDToStringFromTypeAndValue(_TYPE_CODE_, &_Y_);\
  50. if(_STR_){\
  51. NSLog(@"X [%s:%d] %s = %@", __SRC_FILENAME__, __LINE__, #_X_, _STR_);\
  52. }else{\
  53. NSLog(@"Unknown _TYPE_CODE_: %s for expression %s in function %s, file %s, line %d",\
  54. _TYPE_CODE_, #_X_, __func__, __SRC_FILENAME__, __LINE__);\
  55. }}while(0)
  56. #else // __OBJC__
  57. #define DLOG_EXPR(_X_) fprintf(stderr, "%s [%d] X [%s:%d] %s = %s\n",\
  58. __FILENAME__, getpid(), __SRC_FILENAME__, __LINE__, \
  59. #_X_, "<TODO:common.h>")
  60. // TODO eval expression ---------------^
  61. #endif // __OBJC__
  62. #else // !defined(NDEBUG)
  63. #define IFDEBUG(x) do{}while(0)
  64. #define DLOG(...) do{}while(0)
  65. #define DLOG_TRACE() do{}while(0)
  66. #define DLOG_EXPR(...) do{}while(0)
  67. #endif // !defined(NDEBUG)
  68. // libbase compatible assertion macros
  69. #define DCHECK assert
  70. #define DCHECK_OP(op, val1, val2) assert((val1) op (val2))
  71. #define DCHECK_EQ(val1, val2) DCHECK_OP(==, val1, val2)
  72. #define DCHECK_NE(val1, val2) DCHECK_OP(!=, val1, val2)
  73. #define DCHECK_LE(val1, val2) DCHECK_OP(<=, val1, val2)
  74. #define DCHECK_LT(val1, val2) DCHECK_OP(< , val1, val2)
  75. #define DCHECK_GE(val1, val2) DCHECK_OP(>=, val1, val2)
  76. #define DCHECK_GT(val1, val2) DCHECK_OP(> , val1, val2)
  77. // log an error and exit when reaching unimplemented parts
  78. #import <err.h>
  79. #define NOTIMPLEMENTED() errx(4, "[not implemented] %s (%s:%d)", \
  80. __PRETTY_FUNCTION__, __SRC_FILENAME__, __LINE__)
  81. #define NOTREACHED() assert(NO && "Should not have been reached")
  82. // strrstr
  83. #ifdef __cplusplus
  84. extern "C" {
  85. #endif
  86. const char *common_strrstr(const char *string, const char *find);
  87. #ifdef __cplusplus
  88. }
  89. #endif
  90. #endif // COMMON_H_