core_custom_logging.c 3.1 KB

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