file_output.c 884 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #if defined(_WIN32) || defined(_WIN64)
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #endif
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "zf_log.h"
  7. FILE *g_log_file;
  8. static void file_output_callback(const zf_log_message *msg, void *arg)
  9. {
  10. (void)arg;
  11. *msg->p = '\n';
  12. fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_log_file);
  13. fflush(g_log_file);
  14. }
  15. static void file_output_close(void)
  16. {
  17. fclose(g_log_file);
  18. }
  19. static void file_output_open(const char *const log_path)
  20. {
  21. g_log_file = fopen(log_path, "a");
  22. if (!g_log_file)
  23. {
  24. ZF_LOGW("Failed to open log file %s", log_path);
  25. return;
  26. }
  27. atexit(file_output_close);
  28. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
  29. }
  30. int main(int argc, char *argv[])
  31. {
  32. file_output_open("example.log");
  33. ZF_LOGI("Writing number of arguments to log file: %i", argc);
  34. ZF_LOGI_MEM(argv, argc * sizeof(*argv), "argv pointers:");
  35. return 0;
  36. }