custom_output.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <assert.h>
  2. #if defined(_WIN32) || defined(_WIN64)
  3. #include <windows.h>
  4. #define OUTPUT_DEBUG_STRING
  5. #else
  6. #include <syslog.h>
  7. #define OUTPUT_SYSLOG
  8. #endif
  9. #include <zf_log.h>
  10. #ifdef OUTPUT_SYSLOG
  11. static int syslog_level(const int lvl)
  12. {
  13. switch (lvl)
  14. {
  15. case ZF_LOG_VERBOSE:
  16. return LOG_DEBUG;
  17. case ZF_LOG_DEBUG:
  18. return LOG_DEBUG;
  19. case ZF_LOG_INFO:
  20. return LOG_INFO;
  21. case ZF_LOG_WARN:
  22. return LOG_WARNING;
  23. case ZF_LOG_ERROR:
  24. return LOG_ERR;
  25. case ZF_LOG_FATAL:
  26. return LOG_EMERG;
  27. default:
  28. assert(!"can't be");
  29. return LOG_EMERG;
  30. }
  31. }
  32. #endif
  33. static void custom_output_callback(const zf_log_message *msg, void *arg)
  34. {
  35. (void)arg;
  36. /* p points to the log message end. By default, message is not terminated
  37. * with 0, but it has some space allocated for EOL area, so there is always
  38. * some place for terminating zero in the end (see ZF_LOG_EOL_SZ define in
  39. * zf_log.c).
  40. */
  41. *msg->p = 0;
  42. #if defined(OUTPUT_DEBUG_STRING)
  43. OutputDebugStringA(msg->buf);
  44. #elif defined(OUTPUT_SYSLOG)
  45. syslog(syslog_level(msg->lvl), "%s", msg->tag_b);
  46. #else
  47. #error Unsupported platform
  48. #endif
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. #if defined(OUTPUT_SYSLOG)
  53. openlog("custom_output", LOG_CONS|LOG_PERROR|LOG_PID, LOG_USER);
  54. #endif
  55. const unsigned put_mask =
  56. #if defined(OUTPUT_SYSLOG)
  57. ZF_LOG_PUT_STD & !ZF_LOG_PUT_CTX;
  58. #else
  59. ZF_LOG_PUT_STD;
  60. #endif
  61. ;
  62. zf_log_set_output_v(put_mask, 0, custom_output_callback);
  63. ZF_LOGI("Number of arguments goes into custom output: %i", argc);
  64. ZF_LOGI_MEM(argv, argc * sizeof(*argv), "and argv pointers as well:");
  65. #if defined(OUTPUT_SYSLOG)
  66. closelog();
  67. #endif
  68. return 0;
  69. }