render-test.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "render.h"
  5. #include "terminal.h"
  6. /* Log level guideline:
  7. * - ZF_LOG_FATAL - happened something impossible and absolutely unexpected.
  8. * Process can't continue and must be terminated.
  9. * Example: division by zero, unexpected modifications from other thread.
  10. * - ZF_LOG_ERROR - happened something possible, but highly unexpected. The
  11. * process is able to recover and continue execution.
  12. * Example: out of memory (could also be FATAL if not handled properly).
  13. * - ZF_LOG_WARN - happened something that *usually* should not happen and
  14. * significantly changes application behavior for some period of time.
  15. * Example: configuration file not found, auth error.
  16. * - ZF_LOG_INFO - happened significant life cycle event or major state
  17. * transition.
  18. * Example: app started, user logged in.
  19. * - ZF_LOG_DEBUG - minimal set of events that could help to reconstruct the
  20. * execution path. Usually disabled in release builds.
  21. * - ZF_LOG_VERBOSE - all other events. Usually disabled in release builds.
  22. *
  23. * *Ideally*, log file of debugged, well tested, production ready application
  24. * should be empty or very small. Choosing a right log level is as important as
  25. * providing short and self descriptive log message.
  26. */
  27. /*
  28. #define ZF_LOG_VERBOSE 1
  29. #define ZF_LOG_DEBUG 2
  30. #define ZF_LOG_INFO 3
  31. #define ZF_LOG_WARN 4
  32. #define ZF_LOG_ERROR 5
  33. #define ZF_LOG_FATAL 6
  34. */
  35. // LOGGING with file output
  36. #include "zf_log.h"
  37. FILE *g_log_file;
  38. static void file_output_callback(const zf_log_message *msg, void *arg) {
  39. (void)arg;
  40. *msg->p = '\n';
  41. fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_log_file);
  42. fflush(g_log_file);
  43. }
  44. static void file_output_close(void) { fclose(g_log_file); }
  45. static void file_output_open(const char *const log_path) {
  46. g_log_file = fopen(log_path, "a");
  47. if (!g_log_file) {
  48. ZF_LOGW("Failed to open log file %s", log_path);
  49. return;
  50. }
  51. atexit(file_output_close);
  52. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
  53. }
  54. struct console_details console;
  55. int main(int argc, char *argv[]) {
  56. file_output_open("render.log");
  57. render(1, "^P2HELLO^P3 THERE^P2\n", 22);
  58. render(1, "^P2^Fbat.^P1\n^P1OK.\n", 21);
  59. return 0;
  60. }