chenjunfu2-nbt-cpp v2.1.3
一个基于CPP20的NBT(Named Binary Tag)库
载入中...
搜索中...
未找到
NBT_Print.hpp
浏览该文件的文档.
1#pragma once
2
3#include <format>
4#include <stdio.h>
5
8
11{
15};
16
17
24{
28#if defined(_MSC_VER) && _MSC_VER < 1935 //旧版本MSVC 1935-不支持
29#define format_string _Fmt_string //使用MSVC库内部类型
30#endif
31
32private:
33 FILE *pfOutputInfo = NULL;
34 FILE *pfOutputWarn = NULL;
35 FILE *pfOutputErr = NULL;
36
37public:
39
40public:
41
49 NBT_Print(FILE *_pfOutputInfo = stdout, FILE *_pfOutputWarn = stderr, FILE *_pfOutputErr = stderr)
50 : pfOutputInfo(_pfOutputInfo)
51 , pfOutputWarn(_pfOutputWarn)
52 , pfOutputErr(_pfOutputErr)
53 {}
54
56 ~NBT_Print(void) = default;
57
66 template<typename... Args>
67 void operator()(Level lvl, const std::format_string<Args...> fmt, Args&&... args) noexcept
68 {
69 FILE *pfOutput = NULL;
70 switch (lvl)
71 {
72 case Level::Info:
73 pfOutput = pfOutputInfo;
74 break;
75 case Level::Warn:
76 pfOutput = pfOutputWarn;
77 break;
78 case Level::Err:
79 pfOutput = pfOutputErr;
80 break;
81 default:
82 //错误的等级不进行赋值,保持pfOutput为NULL,在后续跳过输出
83 break;
84 }
85
86 if (pfOutput == NULL)//为NULL跳过
87 {
88 return;
89 }
90
91 try
92 {
93 auto tmp = std::format(std::move(fmt), std::forward<Args>(args)...);
94 fwrite(tmp.data(), sizeof(tmp.data()[0]), tmp.size(), pfOutput);
95 }
96 catch (const std::exception &e)
97 {
98 fprintf(stderr, "ErrInfo Exception: \"%s\"\n", e.what());
99 }
100 catch (...)
101 {
102 fprintf(stderr, "ErrInfo Exception: \"Unknown Error\"\n");
103 }
104 }
105
113 template<typename... Args>
114 void operator()(const std::format_string<Args...> fmt, Args&&... args) noexcept
115 {
116 return operator()(Level::Info, std::move(fmt), std::forward<Args>(args)...);
117 }
118};
119
123{
124public:
126
127public:
129 NBT_NoPrint(void) noexcept
130 {}
131
133 ~NBT_NoPrint(void) noexcept
134 {}
135
141 template<typename... Args>
142 void operator()(Level lvl, const std::format_string<Args...> fmt, Args&&... args) const noexcept
143 {
144 return;
145 }
146
151 template<typename... Args>
152 void operator()(const std::format_string<Args...> fmt, Args&&... args) const noexcept
153 {
154 return;
155 }
156};
NBT_Print_Level
用于指示信息打印等级的枚举
定义 NBT_Print.hpp:11
@ Info
普通信息
定义 NBT_Print.hpp:12
@ Warn
警告信息
定义 NBT_Print.hpp:13
@ Err
错误信息
定义 NBT_Print.hpp:14
NBT_Print_Level Level
信息等级,直接映射NBT_Print_Level
定义 NBT_Print.hpp:125
~NBT_NoPrint(void) noexcept
默认析构函数
定义 NBT_Print.hpp:133
void operator()(Level lvl, const std::format_string< Args... > fmt, Args &&... args) const noexcept
仿函数调用,接受打印等级和格式化字符串,忽略所有参数。
定义 NBT_Print.hpp:142
void operator()(const std::format_string< Args... > fmt, Args &&... args) const noexcept
仿函数调用,默认 Info 等级,忽略所有参数。
定义 NBT_Print.hpp:152
NBT_NoPrint(void) noexcept
默认构造函数
定义 NBT_Print.hpp:129
~NBT_Print(void)=default
默认析构
void operator()(const std::format_string< Args... > fmt, Args &&... args) noexcept
函数调用运算符重载,用于将类作为仿函数调用,默认使用Info等级输出信息
定义 NBT_Print.hpp:114
void operator()(Level lvl, const std::format_string< Args... > fmt, Args &&... args) noexcept
函数调用运算符重载,用于将类作为仿函数调用,通过使用指定等级输出信息
定义 NBT_Print.hpp:67
NBT_Print(FILE *_pfOutputInfo=stdout, FILE *_pfOutputWarn=stderr, FILE *_pfOutputErr=stderr)
通过c文件对象构造
定义 NBT_Print.hpp:49
NBT_Print_Level Level
信息等级,直接映射NBT_Print_Level
定义 NBT_Print.hpp:38