SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
sbgl_log.c File Reference
#include "sbgl_internal_log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
Include dependency graph for sbgl_log.c:

Go to the source code of this file.

Macros

#define VK_RESULT_COUNT   23
 

Functions

static void rotate_logs (void)
 
static void open_log_file (void)
 
void sbgl_internal_log_impl (sbgl_LogLevel level, sbgl_LogCategory category, const char *file, int line, const char *function, const char *message)
 
void sbgl_LogSetLevel (sbgl_LogLevel minLevel)
 
void sbgl_LogSetOutput (const char *path)
 
void sbgl_LogSetFileRotation (uint32_t maxFiles, size_t maxFileSize)
 
const char * sbgl_ResultToString (int result)
 
const char * sbgl_VkResultToString (int32_t vkResult)
 

Variables

static sbgl_LogConfig s_config
 
static FILE * s_file = NULL
 
static const char *const SBGL_RESULT_STRINGS []
 
static const int32_t VK_RESULT_CODES []
 
static const char *const VK_RESULT_STRINGS []
 
static const char * level_strings []
 
static const char * category_strings []
 

Macro Definition Documentation

◆ VK_RESULT_COUNT

#define VK_RESULT_COUNT   23

Definition at line 35 of file sbgl_log.c.

Function Documentation

◆ open_log_file()

static void open_log_file ( void )
static

Definition at line 105 of file sbgl_log.c.

105 {
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}
static sbgl_LogConfig s_config
Definition sbgl_log.c:8
static FILE * s_file
Definition sbgl_log.c:17
const char * filePath

◆ rotate_logs()

static void rotate_logs ( void )
static

Definition at line 84 of file sbgl_log.c.

84 {
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}

◆ sbgl_internal_log_impl()

void sbgl_internal_log_impl ( sbgl_LogLevel level,
sbgl_LogCategory category,
const char * file,
int line,
const char * function,
const char * message )

Definition at line 116 of file sbgl_log.c.

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}
@ SBGL_LOG_ERROR
static const char * level_strings[]
Definition sbgl_log.c:68
static void open_log_file(void)
Definition sbgl_log.c:105
static void rotate_logs(void)
Definition sbgl_log.c:84
static const char * category_strings[]
Definition sbgl_log.c:76
sbgl_LogLevel minLevel

◆ sbgl_LogSetFileRotation()

void sbgl_LogSetFileRotation ( uint32_t maxFiles,
size_t maxFileSize )

Definition at line 186 of file sbgl_log.c.

186 {
187 s_config.maxFiles = maxFiles;
188 s_config.maxFileSize = maxFileSize;
189}

◆ sbgl_LogSetLevel()

void sbgl_LogSetLevel ( sbgl_LogLevel minLevel)

Definition at line 167 of file sbgl_log.c.

167 {
168 s_config.minLevel = minLevel;
169}

◆ sbgl_LogSetOutput()

void sbgl_LogSetOutput ( const char * path)

Definition at line 171 of file sbgl_log.c.

171 {
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}

◆ sbgl_ResultToString()

const char * sbgl_ResultToString ( int result)

Definition at line 191 of file sbgl_log.c.

191 {
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}
static const char *const SBGL_RESULT_STRINGS[]
Definition sbgl_log.c:19

◆ sbgl_VkResultToString()

const char * sbgl_VkResultToString ( int32_t vkResult)

Definition at line 198 of file sbgl_log.c.

198 {
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}
static const char *const VK_RESULT_STRINGS[]
Definition sbgl_log.c:42
static const int32_t VK_RESULT_CODES[]
Definition sbgl_log.c:36
#define VK_RESULT_COUNT
Definition sbgl_log.c:35

Variable Documentation

◆ category_strings

const char* category_strings[]
static
Initial value:
= {
[SBGL_LOG_CAT_CORE] = "CORE",
[SBGL_LOG_CAT_PLATFORM] = "PLATFORM",
[SBGL_LOG_CAT_GFX] = "GFX",
[SBGL_LOG_CAT_INPUT] = "INPUT",
[SBGL_LOG_CAT_VOXEL] = "VOXEL",
}
@ SBGL_LOG_CAT_PLATFORM
@ SBGL_LOG_CAT_INPUT
@ SBGL_LOG_CAT_GFX
@ SBGL_LOG_CAT_CORE
@ SBGL_LOG_CAT_VOXEL

Definition at line 76 of file sbgl_log.c.

76 {
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};

◆ level_strings

const char* level_strings[]
static
Initial value:
= {
[SBGL_LOG_DEBUG] = "DEBUG",
[SBGL_LOG_INFO] = "INFO",
[SBGL_LOG_WARN] = "WARN",
[SBGL_LOG_ERROR] = "ERROR",
[SBGL_LOG_CRITICAL] = "CRITICAL",
}
@ SBGL_LOG_CRITICAL
@ SBGL_LOG_WARN
@ SBGL_LOG_INFO
@ SBGL_LOG_DEBUG

Definition at line 68 of file sbgl_log.c.

68 {
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};

◆ s_config

sbgl_LogConfig s_config
static
Initial value:
= {
.minLevel = SBGL_LOG_INFO,
.categoryMask = 0xFFFFFFFF,
.fileEnabled = false,
.filePath = NULL,
.maxFiles = 3,
.maxFileSize = 10 * 1024 * 1024,
}

Definition at line 8 of file sbgl_log.c.

8 {
9 .minLevel = SBGL_LOG_INFO,
10 .categoryMask = 0xFFFFFFFF,
11 .fileEnabled = false,
12 .filePath = NULL,
13 .maxFiles = 3,
14 .maxFileSize = 10 * 1024 * 1024,
15};

◆ s_file

FILE* s_file = NULL
static

Definition at line 17 of file sbgl_log.c.

◆ SBGL_RESULT_STRINGS

const char* const SBGL_RESULT_STRINGS[]
static
Initial value:
= {
[0] = "SBGL_SUCCESS",
[1] = "SBGL_ERROR_NULL_CONTEXT",
[2] = "SBGL_ERROR_INVALID_ARGUMENT",
[3] = "SBGL_ERROR_INITIALIZATION_FAILED",
[4] = "SBGL_ERROR_WINDOW_CREATION_FAILED",
[5] = "SBGL_ERROR_GRAPHICS_FAILED",
[6] = "SBGL_ERROR_OUT_OF_MEMORY",
[7] = "SBGL_ERROR_DEVICE_BUSY",
[8] = "SBGL_ERROR_INVALID_HANDLE",
[9] = "SBGL_ERROR_SWAPCHAIN_FAILED",
[10] = "SBGL_ERROR_SHADER_FAILED",
[11] = "SBGL_ERROR_PIPELINE_FAILED",
[12] = "SBGL_ERROR_PLATFORM_FAILED",
}

Definition at line 19 of file sbgl_log.c.

19 {
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};

◆ VK_RESULT_CODES

const int32_t VK_RESULT_CODES[]
static
Initial value:
= {
0, 1, 2, 3, 4, 5,
-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11,
-13, -14, -15, -16, -17, -18
}

Definition at line 36 of file sbgl_log.c.

36 {
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};

◆ VK_RESULT_STRINGS

const char* const VK_RESULT_STRINGS[]
static
Initial value:
= {
"VK_SUCCESS",
"VK_NOT_READY",
"VK_TIMEOUT",
"VK_EVENT_SET",
"VK_EVENT_RESET",
"VK_INCOMPLETE",
"VK_ERROR_OUT_OF_HOST_MEMORY",
"VK_ERROR_OUT_OF_DEVICE_MEMORY",
"VK_ERROR_INITIALIZATION_FAILED",
"VK_ERROR_DEVICE_LOST",
"VK_ERROR_MEMORY_MAP_FAILED",
"VK_ERROR_LAYER_NOT_PRESENT",
"VK_ERROR_EXTENSION_NOT_PRESENT",
"VK_ERROR_FEATURE_NOT_PRESENT",
"VK_ERROR_INCOMPATIBLE_DRIVER",
"VK_ERROR_TOO_MANY_OBJECTS",
"VK_ERROR_FORMAT_NOT_FOUND",
"VK_ERROR_SURFACE_LOST_KHR",
"VK_ERROR_NATIVE_WINDOW_IN_USE_KHR",
"VK_ERROR_OUT_OF_DATE_KHR",
"VK_ERROR_INCOMPATIBLE_DISPLAY_KHR",
"VK_ERROR_VALIDATION_FAILED_EXT",
"VK_ERROR_INVALID_SHADER_NV",
}

Definition at line 42 of file sbgl_log.c.

42 {
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};