core_custom_logging.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * Copyright (c) 2018 Ramon Santamaria (@raysan5) and Pablo Marcos Oltra (@pamarcos)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #include <stdio.h> // Required for: fopen(), fclose(), fputc(), fwrite(), printf(), fprintf(), funopen()
  13. #include <time.h> // Required for: time_t, tm, time(), localtime(), strftime()
  14. // Custom logging funtion
  15. void LogCustom(int msgType, const char *text, va_list args)
  16. {
  17. char timeStr[64];
  18. time_t now = time(NULL);
  19. struct tm *tm_info = localtime(&now);
  20. strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tm_info);
  21. printf("[%s] ", timeStr);
  22. switch (msgType)
  23. {
  24. case LOG_INFO: printf("[INFO] : "); break;
  25. case LOG_ERROR: printf("[ERROR]: "); break;
  26. case LOG_WARNING: printf("[WARN] : "); break;
  27. case LOG_DEBUG: printf("[DEBUG]: "); break;
  28. default: break;
  29. }
  30. vprintf(text, args);
  31. printf("\n");
  32. }
  33. int main(int argc, char* argv[])
  34. {
  35. // Initialization
  36. //--------------------------------------------------------------------------------------
  37. int screenWidth = 800;
  38. int screenHeight = 450;
  39. // First thing we do is setting our custom logger to ensure everything raylib logs
  40. // will use our own logger instead of its internal one
  41. SetTraceLogCallback(LogCustom);
  42. InitWindow(screenWidth, screenHeight, "raylib [core] example - custom logging");
  43. SetTargetFPS(60);
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. // TODO: Update your variables here
  51. //----------------------------------------------------------------------------------
  52. // Draw
  53. //----------------------------------------------------------------------------------
  54. BeginDrawing();
  55. ClearBackground(RAYWHITE);
  56. DrawText("Check out the console output to see the custom logger in action!", 60, 200, 20, LIGHTGRAY);
  57. EndDrawing();
  58. //----------------------------------------------------------------------------------
  59. }
  60. // De-Initialization
  61. //--------------------------------------------------------------------------------------
  62. CloseWindow(); // Close window and OpenGL context
  63. //--------------------------------------------------------------------------------------
  64. return 0;
  65. }