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*
*log
fc_patch/*
test

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

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

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

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

@ -1,8 +1,8 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
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)
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)
set(csrc
log_impl.cc
glog_utils.cc
)
set(csrc
log_impl.cc
glog_utils.cc
)
add_library(base ${csrc})
else() # UNIX
set(csrc)
endif()
add_library(base ${csrc})
add_library(base INTERFACE)
endif()

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

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

@ -8,84 +8,98 @@ static char __progname[] = "paddlespeech";
namespace log {
std::mutex LogMessage::lock_;
std::mutex LogMessage::lock_;
std::string LogMessage::s_debug_logfile_("");
std::string LogMessage::s_info_logfile_("");
std::string LogMessage::s_warning_logfile_("");
std::string LogMessage::s_error_logfile_("");
std::string LogMessage::s_fatal_logfile_("");
void LogMessage::get_curr_proc_info(std::string* pid, std::string* proc_name) {
std::stringstream ss;
ss << getpid();
ss >> *pid;
*proc_name = ::ppspeech::__progname;
void LogMessage::get_curr_proc_info(std::string* pid, std::string* proc_name) {
std::stringstream ss;
ss << getpid();
ss >> *pid;
*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) {
if (FLAGS_logtostderr == 0){
stream_ = std::shared_ptr<std::ostream>(&std::cout);
} else if (FLAGS_logtostderr == 1){
stream_ = std::shared_ptr<std::ostream>(&std::cerr);
}else if (out_to_file_){
// logfile
lock_.lock();
init(file, line);
}
}
LogMessage:: ~LogMessage(){
stream() << std::endl;
if(out_to_file_){
lock_.unlock();
}
if (level_ == FATAL){
std::abort();
}
}
void LogMessage::init(const char* file, int line) {
time_t t = time(0);
char tmp[100];
strftime(tmp, sizeof(tmp), "%Y%m%d-%H%M%S", localtime(&t));
if (s_info_logfile_.empty()) {
std::string pid;
std::string proc_name;
get_curr_proc_info(&pid, &proc_name);
s_debug_logfile_ = std::string("log." + proc_name + ".log.DEBUG." + tmp + "." + pid);
s_info_logfile_ = std::string("log." + proc_name + ".log.INFO." + 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;
if (level_ == DEBUG) {
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);
} else if (level_ == INFO) {
// 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);
} else if (level_ == WARNING) {
// 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);
} else if (level_ == ERROR) {
// 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);
} else {
// 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_ = &ofs;
stream() << tmp << " " << file << " line " << line << "; ";
stream() << std::flush;
}
} // namespace log
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) {
if (FLAGS_logtostderr == 0) {
stream_ = std::shared_ptr<std::ostream>(&std::cout);
} else if (FLAGS_logtostderr == 1) {
stream_ = std::shared_ptr<std::ostream>(&std::cerr);
} else if (out_to_file_) {
// logfile
lock_.lock();
init(file, line);
}
}
LogMessage::~LogMessage() {
stream() << std::endl;
if (out_to_file_) {
lock_.unlock();
}
if (level_ == FATAL) {
std::abort();
}
}
void LogMessage::init(const char* file, int line) {
time_t t = time(0);
char tmp[100];
strftime(tmp, sizeof(tmp), "%Y%m%d-%H%M%S", localtime(&t));
if (s_info_logfile_.empty()) {
std::string pid;
std::string proc_name;
get_curr_proc_info(&pid, &proc_name);
s_debug_logfile_ =
std::string("log." + proc_name + ".log.DEBUG." + tmp + "." + pid);
s_info_logfile_ =
std::string("log." + proc_name + ".log.INFO." + 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;
if (level_ == DEBUG) {
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);
} else if (level_ == INFO) {
// 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);
} else if (level_ == WARNING) {
// 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);
} else if (level_ == ERROR) {
// 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);
} else {
// 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_ = &ofs;
stream() << tmp << " " << file << " line " << line << "; ";
stream() << std::flush;
}
} // namespace log
} // namespace ppspeech

@ -13,27 +13,30 @@
// limitations under the License.
// 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
#include <fstream>
#include <thread>
#include <iostream>
#include <mutex>
#include <string>
#include <sstream>
#include <iostream>
#include <string>
#include <thread>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include "base/common.h"
#include "base/macros.h"
#ifndef WITH_GLOG
#include "base/glog_utils.h"
#endif
DECLARE_int32(logtostderr);
namespace ppspeech{
namespace ppspeech {
namespace log {
@ -47,57 +50,60 @@ enum Severity {
};
class LogMessage {
public:
static void get_curr_proc_info(std::string* pid, std::string* proc_name);
public:
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();
std::ostream& stream() { return *stream_; }
std::ostream& stream() { return *stream_; }
private:
void init(const char* file, int line);
private:
void init(const char* file, int line);
private:
std::shared_ptr<std::ostream> stream_;
Severity level_;
bool verbose_;
bool out_to_file_;
private:
std::shared_ptr<std::ostream> stream_;
Severity level_;
bool verbose_;
bool out_to_file_;
static std::mutex lock_; // stream write lock
static std::string s_debug_logfile_;
static std::string s_info_logfile_;
static std::string s_warning_logfile_;
static std::string s_error_logfile_;
static std::string s_fatal_logfile_;
static std::mutex lock_; // stream write lock
static std::string s_debug_logfile_;
static std::string s_info_logfile_;
static std::string s_warning_logfile_;
static std::string s_error_logfile_;
static std::string s_fatal_logfile_;
DISALLOW_COPY_AND_ASSIGN(LogMessage);
DISALLOW_COPY_AND_ASSIGN(LogMessage);
};
} //namespace log
} //namespace ppspeech
} // namespace log
} // namespace ppspeech
#ifndef NDEBUG
#define DLOG_DEBUG ppspeech::log::LogMessage \
(__FILE__, __LINE__, ppspeech::log::DEBUG, false)
#define DLOG_DEBUG \
ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::DEBUG, false)
#else
#define DLOG_DEBUG ppspeech::log::LogMessage \
(__FILE__, __LINE__, ppspeech::log::DEBUG, true)
#define DLOG_DEBUG \
ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::DEBUG, true)
#endif
#define DLOG_INFO ppspeech::log::LogMessage \
(__FILE__, __LINE__, ppspeech::log::INFO, true)
#define DLOG_WARNING ppspeech::log::LogMessage \
(__FILE__, __LINE__, ppspeech::log::WARNING, true)
#define DLOG_ERROR ppspeech::log::LogMessage \
(__FILE__, __LINE__, ppspeech::log::ERROR, true)
#define DLOG_FATAL ppspeech::log::LogMessage \
(__FILE__, __LINE__, ppspeech::log::FATAL, true)
#define DLOG_INFO \
ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::INFO, true)
#define DLOG_WARNING \
ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::WARNING, true)
#define DLOG_ERROR \
ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::ERROR, true)
#define DLOG_FATAL \
ppspeech::log::LogMessage(__FILE__, __LINE__, ppspeech::log::FATAL, true)
#define DLOG_0 DLOG_DEBUG
#define DLOG_1 DLOG_INFO
@ -110,8 +116,11 @@ class LogMessage {
#define VLOG(verboselevel) LOG(verboselevel)
#define CHECK(exp) ppspeech::log::LogMessage \
(__FILE__, __LINE__, ppspeech::log::FATAL, !(exp)).stream() << "Check Failed: " #exp
#define CHECK(exp) \
ppspeech::log::LogMessage( \
__FILE__, __LINE__, ppspeech::log::FATAL, !(exp)) \
.stream() \
<< "Check Failed: " #exp
#define CHECK_EQ(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)
#else // NDEBUG
#define DCHECK(condition) \
while (false) \
CHECK(condition)
while (false) CHECK(condition)
#define DCHECK_EQ(val1, val2) \
while (false) \
CHECK_EQ(val1, val2)
while (false) CHECK_EQ(val1, val2)
#define DCHECK_NE(val1, val2) \
while (false) \
CHECK_NE(val1, val2)
while (false) CHECK_NE(val1, val2)
#define DCHECK_LE(val1, val2) \
while (false) \
CHECK_LE(val1, val2)
while (false) CHECK_LE(val1, val2)
#define DCHECK_LT(val1, val2) \
while (false) \
CHECK_LT(val1, val2)
while (false) CHECK_LT(val1, val2)
#define DCHECK_GE(val1, val2) \
while (false) \
CHECK_GE(val1, val2)
while (false) CHECK_GE(val1, val2)
#define DCHECK_GT(val1, val2) \
while (false) \
CHECK_GT(val1, val2)
while (false) CHECK_GT(val1, val2)
#define DCHECK_STREQ(str1, str2) \
while (false) \
CHECK_STREQ(str1, str2)
while (false) CHECK_STREQ(str1, str2)
#endif

@ -24,7 +24,7 @@
#endif
namespace ppspeech {
// kSpaceSymbol in UTF-8 is: ▁
const char kSpaceSymbo[] = "\xe2\x96\x81";

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

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

@ -584,7 +584,7 @@ template void VectorBase<double>::CopyColFromMat(const MatrixBase<double> &mat,
// Real *data = data_;
//// implement the function according to a dimension cutoff for computation
///efficiency
/// efficiency
// if (num_rows <= 64) {
// cblas_Xscal(dim, beta, data, 1);
// const Real *m_data = M.Data();
@ -605,7 +605,7 @@ template void VectorBase<double>::CopyColFromMat(const MatrixBase<double> &mat,
// MatrixIndexT num_cols = M.NumCols();
//// implement the function according to a dimension cutoff for computation
///efficiency
/// efficiency
// if (num_cols <= 64) {
// for (MatrixIndexT i = 0; i < dim_; i++) {
// 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 +=
// N_col_stride, data++) {
//*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)
# 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