refactor: improve logging (#301)
This commit is contained in:
@@ -73,16 +73,31 @@ template <>
|
||||
struct std::formatter<clice::json::Value> : std::formatter<llvm::StringRef> {
|
||||
using Base = std::formatter<llvm::StringRef>;
|
||||
|
||||
int indent = 0;
|
||||
|
||||
template <typename ParseContext>
|
||||
constexpr auto parse(ParseContext& ctx) {
|
||||
return Base::parse(ctx);
|
||||
auto it = ctx.begin();
|
||||
auto end = ctx.end();
|
||||
if(it == end) {
|
||||
return it;
|
||||
}
|
||||
|
||||
int parsed_indent = 0;
|
||||
while(it != end && *it >= '0' && *it <= '9') {
|
||||
parsed_indent = parsed_indent * 10 + (*it - '0');
|
||||
++it;
|
||||
}
|
||||
indent = parsed_indent;
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
template <typename FormatContext>
|
||||
auto format(const clice::json::Value& value, FormatContext& ctx) const {
|
||||
llvm::SmallString<128> buffer;
|
||||
llvm::raw_svector_ostream os{buffer};
|
||||
os << value;
|
||||
llvm::json::OStream(os, indent).value(value);
|
||||
return Base::format(buffer, ctx);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9,15 +9,22 @@ using Level = spdlog::level::level_enum;
|
||||
using ColorMode = spdlog::color_mode;
|
||||
|
||||
struct Options {
|
||||
/// The logging level.
|
||||
Level level = Level::info;
|
||||
|
||||
/// The logging color.
|
||||
ColorMode color = ColorMode::automatic;
|
||||
|
||||
/// If enable, we will record the logs of console sink and replay it
|
||||
/// when create a new sink,
|
||||
bool replay_console = true;
|
||||
};
|
||||
|
||||
extern Options options;
|
||||
|
||||
void create_stderr_logger(std::string_view name, const Options& options);
|
||||
void stderr_logger(std::string_view name, const Options& options);
|
||||
|
||||
void create_file_loggger(std::string_view name, std::string_view dir, const Options& options);
|
||||
void file_loggger(std::string_view name, std::string_view dir, const Options& options);
|
||||
|
||||
template <typename... Args>
|
||||
struct logging_rformat {
|
||||
@@ -72,10 +79,27 @@ void warn(logging_format<Args...> fmt, Args&&... args) {
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void fatal [[noreturn]] (logging_format<Args...> fmt, Args&&... args) {
|
||||
void err(logging_format<Args...> fmt, Args&&... args) {
|
||||
logging::log(spdlog::level::err, fmt.location, fmt.str, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void critical [[noreturn]] (logging_format<Args...> fmt, Args&&... args) {
|
||||
logging::log(spdlog::level::critical, fmt.location, fmt.str, std::forward<Args>(args)...);
|
||||
spdlog::shutdown();
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
} // namespace clice::logging
|
||||
|
||||
#define LOGGING_MESSAGE(name, fmt, ...) \
|
||||
if(clice::logging::options.level <= clice::logging::Level::name) { \
|
||||
clice::logging::name(fmt __VA_OPT__(, ) __VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOGGING_TRACE(fmt, ...) LOGGING_MESSAGE(trace, fmt, __VA_ARGS__)
|
||||
#define LOGGING_DEBUG(fmt, ...) LOGGING_MESSAGE(debug, fmt, __VA_ARGS__)
|
||||
#define LOGGING_INFO(fmt, ...) LOGGING_MESSAGE(info, fmt, __VA_ARGS__)
|
||||
#define LOGGING_WARN(fmt, ...) LOGGING_MESSAGE(warn, fmt, __VA_ARGS__)
|
||||
#define LOGGING_ERROR(fmt, ...) LOGGING_MESSAGE(err, fmt, __VA_ARGS__)
|
||||
#define LOGGING_FATAL(fmt, ...) clice::logging::critical(fmt __VA_OPT__(, ) __VA_ARGS__);
|
||||
|
||||
Reference in New Issue
Block a user