core_custom_logging.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Custom logging
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 2.5
  8. *
  9. * Example contributed by Pablo Marcos Oltra (@pamarcos) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2018-2024 Pablo Marcos Oltra (@pamarcos) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include <stdio.h> // Required for: fopen(), fclose(), fputc(), fwrite(), printf(), fprintf(), funopen()
  19. #include <time.h> // Required for: time_t, tm, time(), localtime(), strftime()
  20. // Custom logging function
  21. void CustomLog(int msgType, const char *text, va_list args)
  22. {
  23. char timeStr[64] = { 0 };
  24. time_t now = time(NULL);
  25. struct tm *tm_info = localtime(&now);
  26. strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tm_info);
  27. printf("[%s] ", timeStr);
  28. switch (msgType)
  29. {
  30. case LOG_INFO: printf("[INFO] : "); break;
  31. case LOG_ERROR: printf("[ERROR]: "); break;
  32. case LOG_WARNING: printf("[WARN] : "); break;
  33. case LOG_DEBUG: printf("[DEBUG]: "); break;
  34. default: break;
  35. }
  36. vprintf(text, args);
  37. printf("\n");
  38. }
  39. //------------------------------------------------------------------------------------
  40. // Program main entry point
  41. //------------------------------------------------------------------------------------
  42. int main(void)
  43. {
  44. // Initialization
  45. //--------------------------------------------------------------------------------------
  46. const int screenWidth = 800;
  47. const int screenHeight = 450;
  48. // Set custom logger
  49. SetTraceLogCallback(CustomLog);
  50. InitWindow(screenWidth, screenHeight, "raylib [core] example - custom logging");
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. // TODO: Update your variables here
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. DrawText("Check out the console output to see the custom logger in action!", 60, 200, 20, LIGHTGRAY);
  65. EndDrawing();
  66. //----------------------------------------------------------------------------------
  67. }
  68. // De-Initialization
  69. //--------------------------------------------------------------------------------------
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }