log.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /******************************************************************************
  2. *
  3. * Copyright 2014 Google, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #pragma once
  19. /*
  20. * TODO(armansito): Work-around until we figure out a way to generate logs in a
  21. * platform-independent manner.
  22. */
  23. #if defined(OS_GENERIC)
  24. /* syslog didn't work well here since we would be redefining LOG_DEBUG. */
  25. #include <stdio.h>
  26. #define LOGWRAPPER(tag, fmt, args...) \
  27. fprintf(stderr, "%s: " fmt "\n", tag, ##args)
  28. #define LOG_VERBOSE(...) LOGWRAPPER(__VA_ARGS__)
  29. #define LOG_DEBUG(...) LOGWRAPPER(__VA_ARGS__)
  30. #define LOG_INFO(...) LOGWRAPPER(__VA_ARGS__)
  31. #define LOG_WARN(...) LOGWRAPPER(__VA_ARGS__)
  32. #define LOG_ERROR(...) LOGWRAPPER(__VA_ARGS__)
  33. #define LOG_EVENT_INT(...)
  34. #else /* !defined(OS_GENERIC) */
  35. #include <log/log.h>
  36. /**
  37. * These log statements are effectively executing only ALOG(_________, tag, fmt,
  38. * ## args ).
  39. * fprintf is only to cause compilation error when LOG_TAG is not provided,
  40. * which breaks build on Linux (for OS_GENERIC).
  41. */
  42. #if LOG_NDEBUG
  43. #define LOG_VERBOSE(tag, fmt, args...) \
  44. do { \
  45. (true) ? ((int)0) : fprintf(stderr, "%s" fmt, tag, ##args); \
  46. } while (0)
  47. #else // LOG_NDEBUG
  48. #define LOG_VERBOSE(tag, fmt, args...) \
  49. do { \
  50. (true) ? ALOG(LOG_VERBOSE, tag, fmt, ##args) \
  51. : fprintf(stderr, "%s" fmt, tag, ##args); \
  52. } while (0)
  53. #endif // !LOG_NDEBUG
  54. #define LOG_DEBUG(tag, fmt, args...) \
  55. do { \
  56. (true) ? ALOG(LOG_DEBUG, tag, fmt, ##args) \
  57. : fprintf(stderr, "%s" fmt, tag, ##args); \
  58. } while (0)
  59. #define LOG_INFO(tag, fmt, args...) \
  60. do { \
  61. (true) ? ALOG(LOG_INFO, tag, fmt, ##args) \
  62. : fprintf(stderr, "%s" fmt, tag, ##args); \
  63. } while (0)
  64. #define LOG_WARN(tag, fmt, args...) \
  65. do { \
  66. (true) ? ALOG(LOG_WARN, tag, fmt, ##args) \
  67. : fprintf(stderr, "%s" fmt, tag, ##args); \
  68. } while (0)
  69. #define LOG_ERROR(tag, fmt, args...) \
  70. do { \
  71. (true) ? ALOG(LOG_ERROR, tag, fmt, ##args) \
  72. : fprintf(stderr, "%s" fmt, tag, ##args); \
  73. } while (0)
  74. #endif /* defined(OS_GENERIC) */