Support disable logging information (#254)

* Support disable logging information

* Support disable logging information
This commit is contained in:
Jason
2022-09-20 15:44:10 +08:00
committed by GitHub
parent 9c150f0bfb
commit 74f1143aba
8 changed files with 39 additions and 17 deletions

View File

@@ -166,7 +166,7 @@ std::map<std::string, float> FastDeployModel::PrintStatisInfoOfRuntime() {
std::map<std::string, float> statis_info_of_runtime_dict;
if (time_of_runtime_.size() < 10) {
FDWARNING << "PrintStatisInfoOfRuntime require the runtime ran 10 times at "
std::cout << "[FastDeploy] [WARNING] PrintStatisInfoOfRuntime require the runtime ran 10 times at "
"least, but now you only ran "
<< time_of_runtime_.size() << " times." << std::endl;
}

View File

@@ -75,7 +75,7 @@ class FASTDEPLOY_DECL FastDeployModel {
#define TIMERECORD_END(id, prefix) \
if (DebugEnabled()) { \
tc_##id.End(); \
FDLogger() << __FILE__ << "(" << __LINE__ << "):" << __FUNCTION__ << " " \
FDINFO << __FILE__ << "(" << __LINE__ << "):" << __FUNCTION__ << " " \
<< prefix << " duration = " << tc_##id.Duration() << "s." \
<< std::endl; \
}

View File

@@ -114,11 +114,11 @@ bool CheckModelFormat(const std::string& model_file,
Frontend GuessModelFormat(const std::string& model_file) {
if (model_file.size() > 8 &&
model_file.substr(model_file.size() - 8, 8) == ".pdmodel") {
FDLogger() << "Model Format: PaddlePaddle." << std::endl;
FDINFO << "Model Format: PaddlePaddle." << std::endl;
return Frontend::PADDLE;
} else if (model_file.size() > 5 &&
model_file.substr(model_file.size() - 5, 5) == ".onnx") {
FDLogger() << "Model Format: ONNX." << std::endl;
FDINFO << "Model Format: ONNX." << std::endl;
return Frontend::ONNX;
}

View File

@@ -137,6 +137,10 @@ PYBIND11_MODULE(@PY_LIBRARY_NAME@, m) {
"Make programer easier to deploy deeplearning model, save time to save "
"the world!";
pybind11::class_<FDLogger>(m, "FDLogger")
.def_readwrite_static("disable_info", &FDLogger::disable_info)
.def_readwrite_static("disable_warning", &FDLogger::disable_warning);
BindRuntime(m);
BindFDModel(m);
#ifdef ENABLE_VISION

View File

@@ -38,7 +38,7 @@ class FASTDEPLOY_DECL TimeCounter {
if (!print_out) {
return;
}
FDLogger() << prefix << " duration = " << Duration() << "s." << std::endl;
std::cout << prefix << " duration = " << Duration() << "s." << std::endl;
}
private:

View File

@@ -16,14 +16,17 @@
namespace fastdeploy {
FDLogger::FDLogger(bool verbose, const std::string& prefix) {
verbose_ = verbose;
bool FDLogger::disable_info = false;
bool FDLogger::disable_warning = false;
FDLogger::FDLogger(int level, const std::string& prefix) {
line_ = "";
level_ = level;
prefix_ = prefix;
}
FDLogger& FDLogger::operator<<(std::ostream& (*os)(std::ostream&)) {
if (!verbose_) {
if (!verbose()) {
return *this;
}
std::cout << prefix_ << " " << line_ << std::endl;

View File

@@ -37,16 +37,31 @@ namespace fastdeploy {
class FASTDEPLOY_DECL FDLogger {
public:
static bool disable_info;
static bool disable_warning;
FDLogger() {
line_ = "";
prefix_ = "[FastDeploy]";
verbose_ = true;
level_ = 0;
}
// 0: INFO
// 1: WARNING
// 2: ERROR
explicit FDLogger(int level = 0, const std::string& prefix = "[FastDeploy]");
bool verbose() {
if (disable_info && level_ == 0) {
return false;
}
if (disable_warning && level_ == 1) {
return false;
}
return true;
}
explicit FDLogger(bool verbose, const std::string& prefix = "[FastDeploy]");
template <typename T>
FDLogger& operator<<(const T& val) {
if (!verbose_) {
if (!verbose()) {
return *this;
}
std::stringstream ss;
@@ -56,7 +71,7 @@ class FASTDEPLOY_DECL FDLogger {
}
FDLogger& operator<<(std::ostream& (*os)(std::ostream&));
~FDLogger() {
if (!verbose_ && line_ != "") {
if (!verbose() && line_ != "") {
std::cout << line_ << std::endl;
}
}
@@ -64,7 +79,7 @@ class FASTDEPLOY_DECL FDLogger {
private:
std::string line_;
std::string prefix_;
bool verbose_ = true;
int level_ = 0;
};
FASTDEPLOY_DECL bool ReadBinaryFromFile(const std::string& file,
@@ -75,15 +90,15 @@ FASTDEPLOY_DECL bool ReadBinaryFromFile(const std::string& file,
#endif
#define FDERROR \
FDLogger(true, "[ERROR]") << __REL_FILE__ << "(" << __LINE__ \
FDLogger(2, "[ERROR]") << __REL_FILE__ << "(" << __LINE__ \
<< ")::" << __FUNCTION__ << "\t"
#define FDWARNING \
FDLogger(true, "[WARNING]") << __REL_FILE__ << "(" << __LINE__ \
FDLogger(1, "[WARNING]") << __REL_FILE__ << "(" << __LINE__ \
<< ")::" << __FUNCTION__ << "\t"
#define FDINFO \
FDLogger(true, "[INFO]") << __REL_FILE__ << "(" << __LINE__ \
FDLogger(0, "[INFO]") << __REL_FILE__ << "(" << __LINE__ \
<< ")::" << __FUNCTION__ << "\t"
#define FDASSERT(condition, format, ...) \

View File

@@ -17,7 +17,7 @@ import os
import sys
from .c_lib_wrap import (Frontend, Backend, FDDataType, TensorInfo, Device,
is_built_with_gpu, is_built_with_ort,
FDLogger, is_built_with_gpu, is_built_with_ort,
is_built_with_paddle, is_built_with_trt,
get_default_cuda_directory)
from .runtime import Runtime, RuntimeOption