refactor logging module; build pass on linux and andorid

pull/2986/head
Hui Zhang 3 years ago
parent 7489e399f1
commit f51a0523a0

@ -4,3 +4,4 @@ engine/common/base/log.h
tools/valgrind* tools/valgrind*
*log *log
fc_patch/* fc_patch/*
test

@ -63,9 +63,7 @@ option(WITH_TESTING "unit test" ON)
############################################################################### ###############################################################################
include(gflags) include(gflags)
include(glog) include(glog)
add_dependencies(glog gflags)
# gtest # gtest
if(WITH_TESTING) if(WITH_TESTING)

@ -4,17 +4,18 @@ if(ANDROID)
else() # UNIX else() # UNIX
add_definitions(-DWITH_GLOG) add_definitions(-DWITH_GLOG)
FetchContent_Declare( FetchContent_Declare(
extern_glog glog
URL https://paddleaudio.bj.bcebos.com/build/glog-0.4.0.zip URL https://paddleaudio.bj.bcebos.com/build/glog-0.4.0.zip
URL_HASH SHA256=9e1b54eb2782f53cd8af107ecf08d2ab64b8d0dc2b7f5594472f3bd63ca85cdc URL_HASH SHA256=9e1b54eb2782f53cd8af107ecf08d2ab64b8d0dc2b7f5594472f3bd63ca85cdc
) )
FetchContent_MakeAvailable(extern_glog) FetchContent_MakeAvailable(glog)
include_directories(${glog_BINARY_DIR} ${glog_SOURCE_DIR}/src) include_directories(${glog_BINARY_DIR} ${glog_SOURCE_DIR}/src)
endif() endif()
add_library(glog INTERFACE)
if(ANDROID) if(ANDROID)
add_library(extern_glog INTERFACE)
else() # UNIX else() # UNIX
add_dependencies(gtest extern_glog) add_dependencies(glog gflags)
add_library(extern_glog ALIAS glog)
endif() endif()

@ -4,20 +4,22 @@ include(FetchContent)
if(ANDROID) if(ANDROID)
else() # UNIX else() # UNIX
FetchContent_Declare( FetchContent_Declare(
extern_gtest gtest
URL https://paddleaudio.bj.bcebos.com/build/gtest-release-1.11.0.zip URL https://paddleaudio.bj.bcebos.com/build/gtest-release-1.11.0.zip
URL_HASH SHA256=353571c2440176ded91c2de6d6cd88ddd41401d14692ec1f99e35d013feda55a URL_HASH SHA256=353571c2440176ded91c2de6d6cd88ddd41401d14692ec1f99e35d013feda55a
) )
FetchContent_MakeAvailable(extern_gtest) FetchContent_MakeAvailable(gtest)
include_directories(${gtest_BINARY_DIR} ${gtest_SOURCE_DIR}/src) include_directories(${gtest_BINARY_DIR} ${gtest_SOURCE_DIR}/src)
endif() endif()
add_library(gtest INTERFACE)
if(ANDROID) if(ANDROID)
add_library(extern_gtest INTERFACE)
else() # UNIX else() # UNIX
add_dependencies(gtest extern_gtest) add_dependencies(gtest gflags gflog)
add_library(extern_gtest ALIAS gtest)
endif() endif()
if(WITH_TESTING) if(WITH_TESTING)

@ -17,6 +17,8 @@ function(pps_summary)
message(STATUS "*************PaddleSpeech Building Summary**********") message(STATUS "*************PaddleSpeech Building Summary**********")
message(STATUS " CMake version : ${CMAKE_VERSION}") message(STATUS " CMake version : ${CMAKE_VERSION}")
message(STATUS " CMake command : ${CMAKE_COMMAND}") message(STATUS " CMake command : ${CMAKE_COMMAND}")
message(STATUS " UNIX : ${UNIX}")
message(STATUS " ANDROID : ${ANDROID}")
message(STATUS " System : ${CMAKE_SYSTEM_NAME}") message(STATUS " System : ${CMAKE_SYSTEM_NAME}")
message(STATUS " C++ compiler : ${CMAKE_CXX_COMPILER}") message(STATUS " C++ compiler : ${CMAKE_CXX_COMPILER}")
message(STATUS " C++ compiler version : ${CMAKE_CXX_COMPILER_VERSION}") message(STATUS " C++ compiler version : ${CMAKE_CXX_COMPILER_VERSION}")

@ -1,8 +1,8 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR) cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
add_executable(glog_main ${CMAKE_CURRENT_SOURCE_DIR}/glog_main.cc) add_executable(glog_main ${CMAKE_CURRENT_SOURCE_DIR}/glog_main.cc)
target_link_libraries(glog_main glog) target_link_libraries(glog_main extern_glog)
add_executable(glog_logtostderr_main ${CMAKE_CURRENT_SOURCE_DIR}/glog_logtostderr_main.cc) add_executable(glog_logtostderr_main ${CMAKE_CURRENT_SOURCE_DIR}/glog_logtostderr_main.cc)
target_link_libraries(glog_logtostderr_main glog) target_link_libraries(glog_logtostderr_main extern_glog)

@ -31,12 +31,12 @@ message(STATUS "Generated ${CMAKE_CURRENT_SOURCE_DIR}/log.h")
if(ANDROID) if(ANDROID)
set(csrc set(csrc
log_impl.cc log_impl.cc
glog_utils.cc glog_utils.cc
) )
add_library(base ${csrc})
else() # UNIX else() # UNIX
set(csrc) set(csrc)
add_library(base INTERFACE)
endif() endif()
add_library(base ${csrc})

@ -2,11 +2,11 @@
#include "base/glog_utils.h" #include "base/glog_utils.h"
namespace google { namespace google {
void InitGoogleLogging(const char* name) { void InitGoogleLogging(const char* name) {
LOG(INFO) << "dummpy InitGoogleLogging."; LOG(INFO) << "dummpy InitGoogleLogging.";
} }
void InstallFailureSignalHandler(){ void InstallFailureSignalHandler() {
LOG(INFO) << "dummpy InstallFailureSignalHandler."; LOG(INFO) << "dummpy InstallFailureSignalHandler.";
} }
} // namespace google } // namespace google

@ -3,7 +3,7 @@
#include "base/common.h" #include "base/common.h"
namespace google { namespace google {
void InitGoogleLogging(const char* name); void InitGoogleLogging(const char* name);
void InstallFailureSignalHandler(); void InstallFailureSignalHandler();
} // namespace google } // namespace google

@ -15,39 +15,43 @@ std::string LogMessage::s_warning_logfile_("");
std::string LogMessage::s_error_logfile_(""); std::string LogMessage::s_error_logfile_("");
std::string LogMessage::s_fatal_logfile_(""); std::string LogMessage::s_fatal_logfile_("");
void LogMessage::get_curr_proc_info(std::string* pid, std::string* proc_name) { void LogMessage::get_curr_proc_info(std::string* pid, std::string* proc_name) {
std::stringstream ss; std::stringstream ss;
ss << getpid(); ss << getpid();
ss >> *pid; ss >> *pid;
*proc_name = ::ppspeech::__progname; *proc_name = ::ppspeech::__progname;
} }
LogMessage::LogMessage(const char* file, int line, Severity level, bool verbose, bool out_to_file /* = false */) : level_(level), verbose_(verbose), out_to_file_(out_to_file) { LogMessage::LogMessage(const char* file,
int line,
if (FLAGS_logtostderr == 0){ Severity level,
bool verbose,
bool out_to_file /* = false */)
: level_(level), verbose_(verbose), out_to_file_(out_to_file) {
if (FLAGS_logtostderr == 0) {
stream_ = std::shared_ptr<std::ostream>(&std::cout); stream_ = std::shared_ptr<std::ostream>(&std::cout);
} else if (FLAGS_logtostderr == 1){ } else if (FLAGS_logtostderr == 1) {
stream_ = std::shared_ptr<std::ostream>(&std::cerr); stream_ = std::shared_ptr<std::ostream>(&std::cerr);
}else if (out_to_file_){ } else if (out_to_file_) {
// logfile // logfile
lock_.lock(); lock_.lock();
init(file, line); init(file, line);
} }
} }
LogMessage:: ~LogMessage(){ LogMessage::~LogMessage() {
stream() << std::endl; stream() << std::endl;
if(out_to_file_){ if (out_to_file_) {
lock_.unlock(); lock_.unlock();
} }
if (level_ == FATAL){ if (level_ == FATAL) {
std::abort(); std::abort();
} }
} }
void LogMessage::init(const char* file, int line) { void LogMessage::init(const char* file, int line) {
time_t t = time(0); time_t t = time(0);
char tmp[100]; char tmp[100];
strftime(tmp, sizeof(tmp), "%Y%m%d-%H%M%S", localtime(&t)); strftime(tmp, sizeof(tmp), "%Y%m%d-%H%M%S", localtime(&t));
@ -57,35 +61,45 @@ std::string LogMessage::s_fatal_logfile_("");
std::string proc_name; std::string proc_name;
get_curr_proc_info(&pid, &proc_name); get_curr_proc_info(&pid, &proc_name);
s_debug_logfile_ = std::string("log." + proc_name + ".log.DEBUG." + tmp + "." + pid); s_debug_logfile_ =
s_info_logfile_ = std::string("log." + proc_name + ".log.INFO." + tmp + "." + pid); std::string("log." + proc_name + ".log.DEBUG." + tmp + "." + pid);
s_warning_logfile_ = std::string("log." + proc_name + ".log.WARNING." + tmp + "." + pid); s_info_logfile_ =
s_error_logfile_ = std::string("log." + proc_name + ".log.ERROR." + tmp + "." + pid); std::string("log." + proc_name + ".log.INFO." + tmp + "." + pid);
s_fatal_logfile_ = std::string("log." + proc_name + ".log.FATAL." + tmp + "." + pid); s_warning_logfile_ =
std::string("log." + proc_name + ".log.WARNING." + tmp + "." + pid);
s_error_logfile_ =
std::string("log." + proc_name + ".log.ERROR." + tmp + "." + pid);
s_fatal_logfile_ =
std::string("log." + proc_name + ".log.FATAL." + tmp + "." + pid);
} }
std::ofstream ofs; std::ofstream ofs;
if (level_ == DEBUG) { if (level_ == DEBUG) {
stream_ = std::make_shared<std::ofstream>(s_debug_logfile_.c_str(), std::ios::out | std::ios::app); stream_ = std::make_shared<std::ofstream>(
s_debug_logfile_.c_str(), std::ios::out | std::ios::app);
// ofs.open(s_debug_logfile_.c_str(), std::ios::out | std::ios::app); // ofs.open(s_debug_logfile_.c_str(), std::ios::out | std::ios::app);
} else if (level_ == INFO) { } else if (level_ == INFO) {
// ofs.open(s_warning_logfile_.c_str(), std::ios::out | std::ios::app); // ofs.open(s_warning_logfile_.c_str(), std::ios::out | std::ios::app);
stream_ = std::make_shared<std::ofstream>(s_warning_logfile_.c_str(), std::ios::out | std::ios::app); stream_ = std::make_shared<std::ofstream>(
s_warning_logfile_.c_str(), std::ios::out | std::ios::app);
} else if (level_ == WARNING) { } else if (level_ == WARNING) {
// ofs.open(s_warning_logfile_.c_str(), std::ios::out | std::ios::app); // ofs.open(s_warning_logfile_.c_str(), std::ios::out | std::ios::app);
stream_ = std::make_shared<std::ofstream>(s_warning_logfile_.c_str(), std::ios::out | std::ios::app); stream_ = std::make_shared<std::ofstream>(
s_warning_logfile_.c_str(), std::ios::out | std::ios::app);
} else if (level_ == ERROR) { } else if (level_ == ERROR) {
// ofs.open(s_error_logfile_.c_str(), std::ios::out | std::ios::app); // ofs.open(s_error_logfile_.c_str(), std::ios::out | std::ios::app);
stream_ = std::make_shared<std::ofstream>(s_error_logfile_.c_str(), std::ios::out | std::ios::app); stream_ = std::make_shared<std::ofstream>(
s_error_logfile_.c_str(), std::ios::out | std::ios::app);
} else { } else {
// ofs.open(s_fatal_logfile_.c_str(), std::ios::out | std::ios::app); // ofs.open(s_fatal_logfile_.c_str(), std::ios::out | std::ios::app);
stream_ = std::make_shared<std::ofstream>(s_fatal_logfile_.c_str(), std::ios::out | std::ios::app); stream_ = std::make_shared<std::ofstream>(
s_fatal_logfile_.c_str(), std::ios::out | std::ios::app);
} }
// stream_ = &ofs; // stream_ = &ofs;
stream() << tmp << " " << file << " line " << line << "; "; stream() << tmp << " " << file << " line " << line << "; ";
stream() << std::flush; stream() << std::flush;
} }
} // namespace log } // namespace log
} // namespace ppspeech } // namespace ppspeech

@ -13,27 +13,30 @@
// limitations under the License. // limitations under the License.
// modified from https://github.com/Dounm/dlog // modified from https://github.com/Dounm/dlog
// modified form https://android.googlesource.com/platform/art/+/806defa/src/logging.h // modified form
// https://android.googlesource.com/platform/art/+/806defa/src/logging.h
#pragma once #pragma once
#include <fstream> #include <fstream>
#include <thread> #include <iostream>
#include <mutex> #include <mutex>
#include <string>
#include <sstream> #include <sstream>
#include <iostream> #include <string>
#include <thread>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "base/common.h" #include "base/common.h"
#include "base/macros.h" #include "base/macros.h"
#ifndef WITH_GLOG
#include "base/glog_utils.h" #include "base/glog_utils.h"
#endif
DECLARE_int32(logtostderr); DECLARE_int32(logtostderr);
namespace ppspeech{ namespace ppspeech {
namespace log { namespace log {
@ -50,7 +53,11 @@ class LogMessage {
public: public:
static void get_curr_proc_info(std::string* pid, std::string* proc_name); static void get_curr_proc_info(std::string* pid, std::string* proc_name);
LogMessage(const char* file, int line, Severity level, bool verbose, bool out_to_file = false); LogMessage(const char* file,
int line,
Severity level,
bool verbose,
bool out_to_file = false);
~LogMessage(); ~LogMessage();
@ -76,28 +83,27 @@ class LogMessage {
}; };
} //namespace log } // namespace log
} //namespace ppspeech
} // namespace ppspeech
#ifndef NDEBUG #ifndef NDEBUG
#define DLOG_DEBUG ppspeech::log::LogMessage \ #define DLOG_DEBUG \
(__FILE__, __LINE__, ppspeech::log::DEBUG, false) ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::DEBUG, false)
#else #else
#define DLOG_DEBUG ppspeech::log::LogMessage \ #define DLOG_DEBUG \
(__FILE__, __LINE__, ppspeech::log::DEBUG, true) ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::DEBUG, true)
#endif #endif
#define DLOG_INFO ppspeech::log::LogMessage \ #define DLOG_INFO \
(__FILE__, __LINE__, ppspeech::log::INFO, true) ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::INFO, true)
#define DLOG_WARNING ppspeech::log::LogMessage \ #define DLOG_WARNING \
(__FILE__, __LINE__, ppspeech::log::WARNING, true) ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::WARNING, true)
#define DLOG_ERROR ppspeech::log::LogMessage \ #define DLOG_ERROR \
(__FILE__, __LINE__, ppspeech::log::ERROR, true) ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::ERROR, true)
#define DLOG_FATAL ppspeech::log::LogMessage \ #define DLOG_FATAL \
(__FILE__, __LINE__, ppspeech::log::FATAL, true) ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::FATAL, true)
#define DLOG_0 DLOG_DEBUG #define DLOG_0 DLOG_DEBUG
#define DLOG_1 DLOG_INFO #define DLOG_1 DLOG_INFO
@ -110,8 +116,11 @@ class LogMessage {
#define VLOG(verboselevel) LOG(verboselevel) #define VLOG(verboselevel) LOG(verboselevel)
#define CHECK(exp) ppspeech::log::LogMessage \ #define CHECK(exp) \
(__FILE__, __LINE__, ppspeech::log::FATAL, !(exp)).stream() << "Check Failed: " #exp ppspeech::log::LogMessage( \
__FILE__, __LINE__, ppspeech::log::FATAL, !(exp)) \
.stream() \
<< "Check Failed: " #exp
#define CHECK_EQ(x, y) CHECK((x) == (y)) #define CHECK_EQ(x, y) CHECK((x) == (y))
#define CHECK_NE(x, y) CHECK((x) != (y)) #define CHECK_NE(x, y) CHECK((x) != (y))
@ -129,27 +138,19 @@ class LogMessage {
#define DCHECK_GT(x, y) CHECK_GT(x, y) #define DCHECK_GT(x, y) CHECK_GT(x, y)
#else // NDEBUG #else // NDEBUG
#define DCHECK(condition) \ #define DCHECK(condition) \
while (false) \ while (false) CHECK(condition)
CHECK(condition)
#define DCHECK_EQ(val1, val2) \ #define DCHECK_EQ(val1, val2) \
while (false) \ while (false) CHECK_EQ(val1, val2)
CHECK_EQ(val1, val2)
#define DCHECK_NE(val1, val2) \ #define DCHECK_NE(val1, val2) \
while (false) \ while (false) CHECK_NE(val1, val2)
CHECK_NE(val1, val2)
#define DCHECK_LE(val1, val2) \ #define DCHECK_LE(val1, val2) \
while (false) \ while (false) CHECK_LE(val1, val2)
CHECK_LE(val1, val2)
#define DCHECK_LT(val1, val2) \ #define DCHECK_LT(val1, val2) \
while (false) \ while (false) CHECK_LT(val1, val2)
CHECK_LT(val1, val2)
#define DCHECK_GE(val1, val2) \ #define DCHECK_GE(val1, val2) \
while (false) \ while (false) CHECK_GE(val1, val2)
CHECK_GE(val1, val2)
#define DCHECK_GT(val1, val2) \ #define DCHECK_GT(val1, val2) \
while (false) \ while (false) CHECK_GT(val1, val2)
CHECK_GT(val1, val2)
#define DCHECK_STREQ(str1, str2) \ #define DCHECK_STREQ(str1, str2) \
while (false) \ while (false) CHECK_STREQ(str1, str2)
CHECK_STREQ(str1, str2)
#endif #endif

@ -24,5 +24,5 @@ set(BINS
foreach(bin_name IN LISTS BINS) foreach(bin_name IN LISTS BINS)
add_executable(${bin_name} ${CMAKE_CURRENT_SOURCE_DIR}/${bin_name}.cc) add_executable(${bin_name} ${CMAKE_CURRENT_SOURCE_DIR}/${bin_name}.cc)
target_include_directories(${bin_name} PRIVATE ${SPEECHX_ROOT} ${SPEECHX_ROOT}/kaldi) target_include_directories(${bin_name} PRIVATE ${SPEECHX_ROOT} ${SPEECHX_ROOT}/kaldi)
target_link_libraries(${bin_name} PUBLIC frontend base utils kaldi-util gflags glog) target_link_libraries(${bin_name} PUBLIC frontend base utils kaldi-util gflags extern_glog)
endforeach() endforeach()

@ -16,7 +16,6 @@
#include "base/flags.h" #include "base/flags.h"
#include "base/log.h" #include "base/log.h"
#include "base/log_impl.h"
#include "frontend/audio_cache.h" #include "frontend/audio_cache.h"
#include "frontend/data_cache.h" #include "frontend/data_cache.h"
#include "frontend/fbank.h" #include "frontend/fbank.h"

@ -584,7 +584,7 @@ template void VectorBase<double>::CopyColFromMat(const MatrixBase<double> &mat,
// Real *data = data_; // Real *data = data_;
//// implement the function according to a dimension cutoff for computation //// implement the function according to a dimension cutoff for computation
///efficiency /// efficiency
// if (num_rows <= 64) { // if (num_rows <= 64) {
// cblas_Xscal(dim, beta, data, 1); // cblas_Xscal(dim, beta, data, 1);
// const Real *m_data = M.Data(); // const Real *m_data = M.Data();
@ -605,7 +605,7 @@ template void VectorBase<double>::CopyColFromMat(const MatrixBase<double> &mat,
// MatrixIndexT num_cols = M.NumCols(); // MatrixIndexT num_cols = M.NumCols();
//// implement the function according to a dimension cutoff for computation //// implement the function according to a dimension cutoff for computation
///efficiency /// efficiency
// if (num_cols <= 64) { // if (num_cols <= 64) {
// for (MatrixIndexT i = 0; i < dim_; i++) { // for (MatrixIndexT i = 0; i < dim_; i++) {
// double sum = 0.0; // double sum = 0.0;
@ -1224,7 +1224,7 @@ void Vector<Real>::Swap(Vector<Real> *other) {
// for (MatrixIndexT i = 0; i < dim; i++, Mdata += M_row_stride, Ndata += // for (MatrixIndexT i = 0; i < dim; i++, Mdata += M_row_stride, Ndata +=
// N_col_stride, data++) { // N_col_stride, data++) {
//*data = beta * *data + alpha * cblas_Xdot(M_col_dim, Mdata, M_col_stride, //*data = beta * *data + alpha * cblas_Xdot(M_col_dim, Mdata, M_col_stride,
//Ndata, N_row_stride); // Ndata, N_row_stride);
//} //}
//} //}

@ -0,0 +1,29 @@
#pragma once
#include <unistd.h>
#include <string>
namespace ppspeech {
pid_t GetPid() {
return getpid();
}
void GetPrpocessName(const pid_t pid, std::string& name){
char tmp[256];
sprintf(tmp, "/proc/%d/cmdline", pid);
FILE* f = fopen(tmp, "r");
if(f){
size_t size;
size = fread(name.data(), sizeof(char), sizeof(tmp), f);
if(size > 0){
if ('\n' == tmp[size-1]){
tmp
}
}
}
}
} // namespace ppspeech

@ -1,4 +1,4 @@
add_executable(infer_onnx_silero_vad ${CMAKE_CURRENT_SOURCE_DIR}/infer_onnx_silero_vad.cc wav.h vad.cc vad.h) add_executable(infer_onnx_silero_vad ${CMAKE_CURRENT_SOURCE_DIR}/infer_onnx_silero_vad.cc wav.h vad.cc vad.h)
# FastDeploy # FastDeploy
target_link_libraries(infer_onnx_silero_vad ${FASTDEPLOY_LIBS} gflags glog) target_link_libraries(infer_onnx_silero_vad ${FASTDEPLOY_LIBS} gflags extern_glog)

Loading…
Cancel
Save