Update logging system

- Update to fmt 6.0.0
- Updated logging system to be macro based
- Fixed a bug with including
This commit is contained in:
zhupengfei
2019-09-05 23:05:16 +08:00
parent 7941811fd8
commit ccffd51904
5 changed files with 45 additions and 31 deletions
+1
View File
@@ -6,6 +6,7 @@ add_library(common STATIC
common_types.h
file_util.cpp
file_util.h
logging/log.cpp
logging/log.h
misc.cpp
scope_exit.h
+18
View File
@@ -0,0 +1,18 @@
// Copyright 2019 threeSD Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <chrono>
#include "common/logging/log.h"
#include "common/string_util.h"
std::uint64_t GetLoggingTime() {
static auto time_origin = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() -
time_origin)
.count();
}
std::string StandardizeLogClass(const std::string& log_class) {
return Common::ReplaceAll(log_class, "_", ".");
}
+23 -28
View File
@@ -4,51 +4,46 @@
// This is simplified version of Citra's logging system.
// Only stderr/stderr output is enabled and color is implemented
// for the UNIX-like.
// with fmt tools.
#pragma once
#include <chrono>
#include <cstdio>
#include <string>
#include <fmt/color.h>
#include <fmt/format.h>
#include "common/string_util.h"
#include <iostream>
std::uint64_t GetLoggingTime();
std::string StandardizeLogClass(const std::string& log_class);
template <typename... Args>
void PrintLog(std::FILE* f, const std::string& log_class, const std::string& level,
const std::string& color, const std::string& file, int line, const std::string& func,
const std::string& format, Args&&... args) {
static auto time_origin = std::chrono::steady_clock::now();
const u64 us = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - time_origin)
.count();
const auto real_class = Common::ReplaceAll(log_class, "_", ".");
try {
fmt::print(f, "\x1b{}[{:12.6f}] {} <{}> {}:{}:{}: " + format + "\x1b[0m\n", color,
us / 1000000.0, real_class, level, file, line, func, args...);
fflush(stderr);
} catch (...) {
std::cerr << "(unexpected) fmt failed with exception" << std::endl;
// TODO: Use a standard variant of ##__VA_ARGS__?
#define LOG_PRINT(log_class, level, text_style, file, line, func, format, ...) \
{ \
fmt::print(stderr, text_style, "[{:12.6f}] {} <{}> {}:{}:{}: " format "\n", \
GetLoggingTime() / 1000000.0, StandardizeLogClass(log_class), level, file, \
line, func, ##__VA_ARGS__); \
fflush(stderr); \
}
}
#ifdef _DEBUG
#define LOG_TRACE(log_class, ...) \
(void(0)) // PrintLog(stderr, #log_class, "Trace", "[1;30m", __FILE__, __LINE__, __func__,
// __VA_ARGS__)
LOG_PRINT(#log_class, "Trace", fmt::fg(fmt::terminal_color::bright_black), __FILE__, __LINE__, \
__func__, __VA_ARGS__)
#else
#define LOG_TRACE(log_class, fmt, ...) (void(0))
#endif
#define LOG_DEBUG(log_class, ...) \
PrintLog(stderr, #log_class, "Debug", "[0;36m", __FILE__, __LINE__, __func__, __VA_ARGS__)
LOG_PRINT(#log_class, "Debug", fmt::fg(fmt::terminal_color::cyan), __FILE__, __LINE__, \
__func__, __VA_ARGS__)
#define LOG_INFO(log_class, ...) \
PrintLog(stderr, #log_class, "Info", "[0;37m", __FILE__, __LINE__, __func__, __VA_ARGS__)
LOG_PRINT(#log_class, "Info", fmt::fg(fmt::terminal_color::white), __FILE__, __LINE__, \
__func__, __VA_ARGS__)
#define LOG_WARNING(log_class, ...) \
PrintLog(stderr, #log_class, "Warning", "[1;33m", __FILE__, __LINE__, __func__, __VA_ARGS__)
LOG_PRINT(#log_class, "Warning", fmt::fg(fmt::terminal_color::bright_yellow), __FILE__, \
__LINE__, __func__, __VA_ARGS__)
#define LOG_ERROR(log_class, ...) \
PrintLog(stderr, #log_class, "Error", "[1;31m", __FILE__, __LINE__, __func__, __VA_ARGS__)
LOG_PRINT(#log_class, "Error", fmt::fg(fmt::terminal_color::bright_red), __FILE__, __LINE__, \
__func__, __VA_ARGS__)
#define LOG_CRITICAL(log_class, ...) \
PrintLog(stderr, #log_class, "Critical", "[1;35m", __FILE__, __LINE__, __func__, __VA_ARGS__)
LOG_PRINT(#log_class, "Critical", fmt::fg(fmt::terminal_color::bright_magenta), __FILE__, \
__LINE__, __func__, __VA_ARGS__)