SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
sbgl_log.c
Go to the documentation of this file.
1#include "sbgl_internal_log.h"
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <time.h>
6#include <stdint.h>
7
9 .minLevel = SBGL_LOG_INFO,
10 .categoryMask = 0xFFFFFFFF,
11 .fileEnabled = false,
12 .filePath = NULL,
13 .maxFiles = 3,
14 .maxFileSize = 10 * 1024 * 1024,
15};
16
17static FILE* s_file = NULL;
18
19static const char* const SBGL_RESULT_STRINGS[] = {
20 [0] = "SBGL_SUCCESS",
21 [1] = "SBGL_ERROR_NULL_CONTEXT",
22 [2] = "SBGL_ERROR_INVALID_ARGUMENT",
23 [3] = "SBGL_ERROR_INITIALIZATION_FAILED",
24 [4] = "SBGL_ERROR_WINDOW_CREATION_FAILED",
25 [5] = "SBGL_ERROR_GRAPHICS_FAILED",
26 [6] = "SBGL_ERROR_OUT_OF_MEMORY",
27 [7] = "SBGL_ERROR_DEVICE_BUSY",
28 [8] = "SBGL_ERROR_INVALID_HANDLE",
29 [9] = "SBGL_ERROR_SWAPCHAIN_FAILED",
30 [10] = "SBGL_ERROR_SHADER_FAILED",
31 [11] = "SBGL_ERROR_PIPELINE_FAILED",
32 [12] = "SBGL_ERROR_PLATFORM_FAILED",
33};
34
35#define VK_RESULT_COUNT 23
36static const int32_t VK_RESULT_CODES[] = {
37 0, 1, 2, 3, 4, 5,
38 -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11,
39 -13, -14, -15, -16, -17, -18
40};
41
42static const char* const VK_RESULT_STRINGS[] = {
43 "VK_SUCCESS",
44 "VK_NOT_READY",
45 "VK_TIMEOUT",
46 "VK_EVENT_SET",
47 "VK_EVENT_RESET",
48 "VK_INCOMPLETE",
49 "VK_ERROR_OUT_OF_HOST_MEMORY",
50 "VK_ERROR_OUT_OF_DEVICE_MEMORY",
51 "VK_ERROR_INITIALIZATION_FAILED",
52 "VK_ERROR_DEVICE_LOST",
53 "VK_ERROR_MEMORY_MAP_FAILED",
54 "VK_ERROR_LAYER_NOT_PRESENT",
55 "VK_ERROR_EXTENSION_NOT_PRESENT",
56 "VK_ERROR_FEATURE_NOT_PRESENT",
57 "VK_ERROR_INCOMPATIBLE_DRIVER",
58 "VK_ERROR_TOO_MANY_OBJECTS",
59 "VK_ERROR_FORMAT_NOT_FOUND",
60 "VK_ERROR_SURFACE_LOST_KHR",
61 "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR",
62 "VK_ERROR_OUT_OF_DATE_KHR",
63 "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR",
64 "VK_ERROR_VALIDATION_FAILED_EXT",
65 "VK_ERROR_INVALID_SHADER_NV",
66};
67
68static const char* level_strings[] = {
69 [SBGL_LOG_DEBUG] = "DEBUG",
70 [SBGL_LOG_INFO] = "INFO",
71 [SBGL_LOG_WARN] = "WARN",
72 [SBGL_LOG_ERROR] = "ERROR",
73 [SBGL_LOG_CRITICAL] = "CRITICAL",
74};
75
76static const char* category_strings[] = {
77 [SBGL_LOG_CAT_CORE] = "CORE",
78 [SBGL_LOG_CAT_PLATFORM] = "PLATFORM",
79 [SBGL_LOG_CAT_GFX] = "GFX",
80 [SBGL_LOG_CAT_INPUT] = "INPUT",
81 [SBGL_LOG_CAT_VOXEL] = "VOXEL",
82};
83
84static void rotate_logs(void) {
85 if (!s_config.filePath || s_config.maxFiles == 0) return;
86
87 char old_path[512];
88 char new_path[512];
89
90 for (int i = s_config.maxFiles - 1; i > 0; i--) {
91 snprintf(old_path, sizeof(old_path), "%s.%d", s_config.filePath, i - 1);
92 snprintf(new_path, sizeof(new_path), "%s.%d", s_config.filePath, i);
93
94 rename(old_path, new_path);
95 }
96
97 if (s_file) {
98 fclose(s_file);
99 s_file = NULL;
100 }
101
102 rename(s_config.filePath, old_path);
103}
104
105static void open_log_file(void) {
106 if (!s_config.filePath) return;
107
108 if (s_file) {
109 fclose(s_file);
110 s_file = NULL;
111 }
112
113 s_file = fopen(s_config.filePath, "a");
114}
115
117 sbgl_LogLevel level,
118 sbgl_LogCategory category,
119 const char* file,
120 int line,
121 const char* function,
122 const char* message
123) {
124 if (level < s_config.minLevel) return;
125 if (!((1u << category) & s_config.categoryMask)) return;
126
127 time_t now = time(NULL);
128 struct tm* tm_info = localtime(&now);
129 char time_buf[32];
130 strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", tm_info);
131
132 char out[1024];
133 int len = snprintf(out, sizeof(out), "[%s] [%s] [%s] %s:%d (%s): %s\n",
134 time_buf,
135 level_strings[level],
136 category_strings[category],
137 file,
138 line,
139 function,
140 message
141 );
142
143 if (len < 0) return;
144
145 if (s_config.fileEnabled) {
146 if (!s_file) {
148 }
149
150 if (s_file) {
151 if (ftell(s_file) > (long)s_config.maxFileSize) {
152 rotate_logs();
154 }
155
156 if (s_file) {
157 fwrite(out, 1, len, s_file);
158 fflush(s_file);
159 }
160 }
161 }
162
163 FILE* dst = (level >= SBGL_LOG_ERROR) ? stderr : stdout;
164 fwrite(out, 1, len, dst);
165}
166
168 s_config.minLevel = minLevel;
169}
170
171void sbgl_LogSetOutput(const char* path) {
172 if (!path) {
173 s_config.fileEnabled = false;
174 if (s_file) {
175 fclose(s_file);
176 s_file = NULL;
177 }
178 return;
179 }
180
181 s_config.fileEnabled = true;
182 s_config.filePath = path;
184}
185
186void sbgl_LogSetFileRotation(uint32_t maxFiles, size_t maxFileSize) {
187 s_config.maxFiles = maxFiles;
188 s_config.maxFileSize = maxFileSize;
189}
190
191const char* sbgl_ResultToString(int result) {
192 if (result >= 0 && result < (int)(sizeof(SBGL_RESULT_STRINGS) / sizeof(SBGL_RESULT_STRINGS[0]))) {
193 return SBGL_RESULT_STRINGS[result];
194 }
195 return "SBGL_UNKNOWN_ERROR";
196}
197
198const char* sbgl_VkResultToString(int32_t vkResult) {
199 for (int i = 0; i < VK_RESULT_COUNT; i++) {
200 if (VK_RESULT_CODES[i] == vkResult) {
201 return VK_RESULT_STRINGS[i];
202 }
203 }
204 return "VK_UNKNOWN";
205}
sbgl_LogCategory
Logging categories for component filtering.
@ SBGL_LOG_CAT_PLATFORM
@ SBGL_LOG_CAT_INPUT
@ SBGL_LOG_CAT_GFX
@ SBGL_LOG_CAT_CORE
@ SBGL_LOG_CAT_VOXEL
sbgl_LogLevel
Logging severity levels.
@ SBGL_LOG_CRITICAL
@ SBGL_LOG_WARN
@ SBGL_LOG_ERROR
@ SBGL_LOG_INFO
@ SBGL_LOG_DEBUG
static const char *const VK_RESULT_STRINGS[]
Definition sbgl_log.c:42
static const int32_t VK_RESULT_CODES[]
Definition sbgl_log.c:36
static const char * level_strings[]
Definition sbgl_log.c:68
void sbgl_LogSetOutput(const char *path)
Definition sbgl_log.c:171
static void open_log_file(void)
Definition sbgl_log.c:105
static sbgl_LogConfig s_config
Definition sbgl_log.c:8
static void rotate_logs(void)
Definition sbgl_log.c:84
static const char *const SBGL_RESULT_STRINGS[]
Definition sbgl_log.c:19
#define VK_RESULT_COUNT
Definition sbgl_log.c:35
void sbgl_internal_log_impl(sbgl_LogLevel level, sbgl_LogCategory category, const char *file, int line, const char *function, const char *message)
Definition sbgl_log.c:116
void sbgl_LogSetFileRotation(uint32_t maxFiles, size_t maxFileSize)
Definition sbgl_log.c:186
static FILE * s_file
Definition sbgl_log.c:17
static const char * category_strings[]
Definition sbgl_log.c:76
const char * sbgl_ResultToString(int result)
Definition sbgl_log.c:191
void sbgl_LogSetLevel(sbgl_LogLevel minLevel)
Definition sbgl_log.c:167
const char * sbgl_VkResultToString(int32_t vkResult)
Definition sbgl_log.c:198
Logging configuration.
const char * filePath
sbgl_LogLevel minLevel