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