core_custom_logging.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Custom logging
  4. *
  5. * Welcome to raylib!
  6. *
  7. * To test examples, just press F6 and execute raylib_compile_execute script
  8. * Note that compiled executable is placed in the same folder as .c file
  9. *
  10. * You can find all basic examples on C:\raylib\raylib\examples folder or
  11. * raylib official webpage: www.raylib.com
  12. *
  13. * Enjoy using raylib. :)
  14. *
  15. * This example has been created using raylib 2.0 (www.raylib.com)
  16. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  17. *
  18. * Copyright (c) 2018 Ramon Santamaria (@raysan5) and Pablo Marcos Oltra (@pamarcos)
  19. *
  20. ********************************************************************************************/
  21. #include "raylib.h"
  22. #include <stdio.h> // Required for: fopen(), fclose(), fputc(), fwrite(), printf(), fprintf(), funopen()
  23. #include <time.h> // Required for: time_t, tm, time(), localtime(), strftime()
  24. void logCustom(int msgType, const char *text, va_list args)
  25. {
  26. char timeStr[64];
  27. time_t now = time(NULL);
  28. struct tm *tm_info = localtime(&now);
  29. strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tm_info);
  30. printf("[%s] ", timeStr);
  31. switch (msgType)
  32. {
  33. case LOG_INFO: printf("[INFO] : "); break;
  34. case LOG_ERROR: printf("[ERROR]: "); break;
  35. case LOG_WARNING: printf("[WARN] : "); break;
  36. case LOG_DEBUG: printf("[DEBUG]: "); break;
  37. default: break;
  38. }
  39. vprintf(text, args);
  40. printf("\n");
  41. }
  42. int main(int argc, char* argv[])
  43. {
  44. // Initialization
  45. //--------------------------------------------------------------------------------------
  46. int screenWidth = 800;
  47. int screenHeight = 450;
  48. // First thing we do is setting our custom logger to ensure everything raylib logs
  49. // will use our own logger instead of its internal one
  50. SetTraceLogCallback(logCustom);
  51. InitWindow(screenWidth, screenHeight, "raylib [core] example - custom logging");
  52. SetTargetFPS(60);
  53. //--------------------------------------------------------------------------------------
  54. // Main game loop
  55. while (!WindowShouldClose()) // Detect window close button or ESC key
  56. {
  57. // Update
  58. //----------------------------------------------------------------------------------
  59. // TODO: Update your variables here
  60. //----------------------------------------------------------------------------------
  61. // Draw
  62. //----------------------------------------------------------------------------------
  63. BeginDrawing();
  64. ClearBackground(RAYWHITE);
  65. DrawText("Check out the console output to see the custom logger in action!", 60, 200, 20, LIGHTGRAY);
  66. EndDrawing();
  67. //----------------------------------------------------------------------------------
  68. }
  69. // De-Initialization
  70. //--------------------------------------------------------------------------------------
  71. CloseWindow(); // Close window and OpenGL context
  72. //--------------------------------------------------------------------------------------
  73. return 0;
  74. }