You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.1 KiB

  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 LogCustom(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. int main(int argc, char* argv[])
  36. {
  37. // Initialization
  38. //--------------------------------------------------------------------------------------
  39. const int screenWidth = 800;
  40. const int screenHeight = 450;
  41. // First thing we do is setting our custom logger to ensure everything raylib logs
  42. // will use our own logger instead of its internal one
  43. SetTraceLogCallback(LogCustom);
  44. InitWindow(screenWidth, screenHeight, "raylib [core] example - custom logging");
  45. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  46. //--------------------------------------------------------------------------------------
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. // TODO: Update your variables here
  53. //----------------------------------------------------------------------------------
  54. // Draw
  55. //----------------------------------------------------------------------------------
  56. BeginDrawing();
  57. ClearBackground(RAYWHITE);
  58. DrawText("Check out the console output to see the custom logger in action!", 60, 200, 20, LIGHTGRAY);
  59. EndDrawing();
  60. //----------------------------------------------------------------------------------
  61. }
  62. // De-Initialization
  63. //--------------------------------------------------------------------------------------
  64. CloseWindow(); // Close window and OpenGL context
  65. //--------------------------------------------------------------------------------------
  66. return 0;
  67. }