njin
Engine 2D gọn nhẹ để tự tay làm game theo ý mình. Vibe coding cũng ổn luôn
Toggle main menu visibility
Loading...
Searching...
No Matches
njin_log.h
1
#pragma once
2
#include "_types.h"
3
#include <cstdio>
4
#include <cstdlib>
5
6
// Usage:
7
// NJIN_INFO("player spawned at %.1f, %.1f", pos.x, pos.y);
8
// NJIN_WARN("texture missing: %s", path);
9
// njin::log_set_level(njin::log_warn); // hide trace/debug/info
10
//
11
// Output: [ 1.234] WARN file.cpp:42 message
12
// The window/audio backend's own messages go through the same logger, tagged
13
// "njin" like the engine's own: njin seals it off from game code (see
14
// src/engine/guard/), so the log should not name it either.
15
17
#if defined(__MINGW_PRINTF_FORMAT)
18
#define NJIN_PRINTF(fmt, args) __attribute__((format(__MINGW_PRINTF_FORMAT, fmt, args)))
19
#elif defined(__GNUC__) || defined(__clang__)
20
#define NJIN_PRINTF(fmt, args) __attribute__((format(printf, fmt, args)))
21
#else
22
#define NJIN_PRINTF(fmt, args)
23
#endif
25
26
namespace
njin {
29
31
enum
log_level
{
32
log_trace
,
33
log_debug
,
34
log_info
,
35
log_warn
,
36
log_error
,
37
log_fatal
,
38
log_off
39
};
40
46
using
log_sink
= void (*)(
log_level
level,
const
char
*file,
i32
line,
47
const
char
*msg,
void
*user);
48
53
void
log_set_level
(
log_level
level);
54
57
log_level
log_get_level
();
58
62
bool
log_enabled
(
log_level
level);
63
67
void
log_set_sink
(
log_sink
sink,
void
*user =
nullptr
);
68
72
const
char
*
log_level_name
(
log_level
level);
73
81
void
log_write
(
log_level
level,
const
char
*file,
i32
line,
const
char
*fmt,
82
...) NJIN_PRINTF(4, 5);
84
}
// namespace njin
85
88
90
#define NJIN_LOG(level, ...) \
91
::njin::log_write((level), __FILE__, __LINE__, __VA_ARGS__)
92
94
#define NJIN_TRACE(...) NJIN_LOG(::njin::log_trace, __VA_ARGS__)
96
#define NJIN_DEBUG(...) NJIN_LOG(::njin::log_debug, __VA_ARGS__)
98
#define NJIN_INFO(...) NJIN_LOG(::njin::log_info, __VA_ARGS__)
100
#define NJIN_WARN(...) NJIN_LOG(::njin::log_warn, __VA_ARGS__)
102
#define NJIN_ERROR(...) NJIN_LOG(::njin::log_error, __VA_ARGS__)
104
#define NJIN_FATAL(...) \
105
do { \
106
NJIN_LOG(::njin::log_fatal, __VA_ARGS__); \
107
std::abort(); \
108
} while (0)
109
njin::log_level_name
const char * log_level_name(log_level level)
Tên của một mức, ví dụ "WARN".
njin::log_set_level
void log_set_level(log_level level)
Bỏ qua các dòng log có mức thấp hơn level.
njin::log_get_level
log_level log_get_level()
Mức tối thiểu hiện tại.
njin::log_level
log_level
Mức độ của một dòng log, từ chi tiết nhất đến nghiêm trọng nhất.
Definition
njin_log.h:31
njin::log_sink
void(*)(log_level level, const char *file, i32 line, const char *msg, void *user) log_sink
Hàm nhận mọi dòng log vượt qua bộ lọc mức độ.
Definition
njin_log.h:46
njin::log_write
void log_write(log_level level, const char *file, i32 line, const char *fmt,...)
Ghi một dòng log theo định dạng printf.
njin::log_set_sink
void log_set_sink(log_sink sink, void *user=nullptr)
Thay nơi nhận log.
njin::log_enabled
bool log_enabled(log_level level)
Đúng nếu một dòng log ở mức level sẽ được ghi.
njin::log_info
@ log_info
Sự kiện bình thường.
Definition
njin_log.h:34
njin::log_debug
@ log_debug
Thông tin để gỡ lỗi.
Definition
njin_log.h:33
njin::log_off
@ log_off
Chỉ dùng với log_set_level() để tắt toàn bộ log.
Definition
njin_log.h:38
njin::log_trace
@ log_trace
Chi tiết nhất, thường chỉ dùng khi lần theo lỗi.
Definition
njin_log.h:32
njin::log_warn
@ log_warn
Có vấn đề nhưng chương trình vẫn chạy tiếp.
Definition
njin_log.h:35
njin::log_fatal
@ log_fatal
Lỗi không thể tiếp tục. NJIN_FATAL() dừng chương trình.
Definition
njin_log.h:37
njin::log_error
@ log_error
Một thao tác đã thất bại.
Definition
njin_log.h:36
njin::i32
std::int32_t i32
Số nguyên có dấu 32 bit.
Definition
_types.h:14
njin_log.h
Được biên soạn bởi
1.18.0