2
0

ZeroTierDebug.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. /**
  9. * @file
  10. *
  11. * Debug macros
  12. */
  13. #ifndef ZT_DEBUG_H
  14. #define ZT_DEBUG_H
  15. #if defined(__linux__) || defined(__APPLE__)
  16. #include <pthread.h>
  17. #include <sys/syscall.h>
  18. #include <unistd.h>
  19. #endif
  20. #include <string.h>
  21. #define ZT_MSG_INFO true
  22. #define ZT_COLOR true
  23. // Debug output colors
  24. #if defined(__APPLE__)
  25. #include "TargetConditionals.h"
  26. #endif
  27. #if defined(ZT_COLOR) && ! defined(_WIN32) && ! defined(__ANDROID__) && ! defined(TARGET_OS_IPHONE) && ! defined(TARGET_IPHONE_SIMULATOR) && ! defined(__APP_FRAMEWORK__)
  28. #define ZT_RED "\x1B[31m"
  29. #define ZT_GRN "\x1B[32m"
  30. #define ZT_YEL "\x1B[33m"
  31. #define ZT_BLU "\x1B[34m"
  32. #define ZT_MAG "\x1B[35m"
  33. #define ZT_CYN "\x1B[36m"
  34. #define ZT_WHT "\x1B[37m"
  35. #define ZT_RESET "\x1B[0m"
  36. #else
  37. #define ZT_RED
  38. #define ZT_GRN
  39. #define ZT_YEL
  40. #define ZT_BLU
  41. #define ZT_MAG
  42. #define ZT_CYN
  43. #define ZT_WHT
  44. #define ZT_RESET
  45. #endif
  46. #define ZT_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) // short
  47. #ifdef __linux__
  48. #define ZT_THREAD_ID (long)0 // syscall(SYS_gettid)
  49. #endif
  50. #ifdef __APPLE__
  51. #define ZT_THREAD_ID (long)0 // (long)gettid()
  52. #endif
  53. #ifdef __FreeBSD__
  54. #define ZT_THREAD_ID (long)0 // (long)gettid()
  55. #endif
  56. #ifdef _WIN32
  57. #define ZT_THREAD_ID (long)0 //
  58. #endif
  59. #if defined(__JNI_LIB__)
  60. #include <jni.h>
  61. #endif
  62. #if defined(__ANDROID__)
  63. #include <android/log.h>
  64. #define ZT_LOG_TAG "ZTSDK"
  65. #endif
  66. #if defined(ZT_DEBUG_TRACE)
  67. #if ZT_MSG_INFO == true
  68. #if defined(__ANDROID__)
  69. #define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, "INFO : %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
  70. #endif
  71. #if defined(_WIN32)
  72. #define DEBUG_INFO(fmt, ...) fprintf(stderr, ZT_GRN "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__)
  73. #endif
  74. #if defined(__linux__) or defined(__APPLE__) or defined(__FreeBSD__)
  75. #define DEBUG_INFO(fmt, args...) fprintf(stderr, ZT_GRN "INFO [%ld]: %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
  76. #endif
  77. #else
  78. #define DEBUG_INFO(fmt, args...)
  79. #endif
  80. #else // blank
  81. #if defined(_WIN32)
  82. #define DEBUG_INFO(...)
  83. #else
  84. #define DEBUG_INFO(fmt, args...)
  85. #endif
  86. #endif
  87. #endif // _H