njin
Engine 2D gọn nhẹ để tự tay làm game theo ý mình. Vibe coding cũng ổn luôn
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
26namespace njin {
29
40
46using log_sink = void (*)(log_level level, const char *file, i32 line,
47 const char *msg, void *user);
48
54
58
63
67void log_set_sink(log_sink sink, void *user = nullptr);
68
72const char *log_level_name(log_level level);
73
81void 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
const char * log_level_name(log_level level)
Tên của một mức, ví dụ "WARN".
void log_set_level(log_level level)
Bỏ qua các dòng log có mức thấp hơn level.
log_level log_get_level()
Mức tối thiểu hiện tại.
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
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
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.
void log_set_sink(log_sink sink, void *user=nullptr)
Thay nơi nhận log.
bool log_enabled(log_level level)
Đúng nếu một dòng log ở mức level sẽ được ghi.
@ log_info
Sự kiện bình thường.
Definition njin_log.h:34
@ log_debug
Thông tin để gỡ lỗi.
Definition njin_log.h:33
@ log_off
Chỉ dùng với log_set_level() để tắt toàn bộ log.
Definition njin_log.h:38
@ log_trace
Chi tiết nhất, thường chỉ dùng khi lần theo lỗi.
Definition njin_log.h:32
@ log_warn
Có vấn đề nhưng chương trình vẫn chạy tiếp.
Definition njin_log.h:35
@ log_fatal
Lỗi không thể tiếp tục. NJIN_FATAL() dừng chương trình.
Definition njin_log.h:37
@ log_error
Một thao tác đã thất bại.
Definition njin_log.h:36
std::int32_t i32
Số nguyên có dấu 32 bit.
Definition _types.h:14