#include "log.hpp"
|
|
#include <string>
|
#include <vector>
|
#include <memory>
|
#include <unistd.h>
|
#include <sys/stat.h>
|
|
#include <stdio.h>
|
#include <stdarg.h>
|
|
#include "../../thirdparty/spdlog/spdlog.h"
|
|
namespace logif{
|
|
static bool log_run = false;
|
|
static const char log_name[] = "business_log";
|
|
const static int kRotateLogFileSize = 1048576 * 5;
|
const static int kRotateLogFileCount = 3;
|
|
static void rotateLog(const char *file_name, const int file_size,
|
const int file_count, const bool show_stdout){
|
|
std::vector<spdlog::sink_ptr> sinks;
|
if(show_stdout){
|
sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>());
|
}
|
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(file_name, file_size, file_count));
|
auto m_logger = std::make_shared<spdlog::logger>(log_name, begin(sinks), end(sinks));
|
m_logger->set_pattern("[%Y-%m-%d %H:%M:%S] %v");
|
spdlog::register_logger(m_logger);
|
}
|
|
static std::string makeTimeStamp(){
|
time_t now;
|
struct tm *now_time;
|
time(&now);
|
now_time = localtime(&now);
|
|
char filename[64];
|
snprintf(filename, 64, "%d%02d%02d%02d%02d%02d",
|
now_time->tm_year+1900, now_time->tm_mon+1,
|
now_time->tm_mday, now_time->tm_hour,
|
now_time->tm_min,now_time->tm_sec);
|
return filename;
|
}
|
|
void CreateLogger(const char *dir, const char *name,
|
const bool show_stdout){
|
|
std::string str_dir(dir);
|
std::string file_name(name);
|
|
|
std::string logfile(str_dir + "/" +
|
file_name + "-" +
|
makeTimeStamp() +
|
".tlog");
|
|
CreateLogger(logfile.c_str(), show_stdout);
|
}
|
|
void CreateLogger(const char *name, const bool show_stdout){
|
|
rotateLog(name, kRotateLogFileSize, kRotateLogFileCount, show_stdout);
|
|
log_run = true;
|
}
|
|
void DestroyLogger(){
|
if (log_run)
|
spdlog::drop_all();
|
}
|
|
void logIt(const char *fmt, ...){
|
|
char temp[512];
|
|
va_list args; //定义一个va_list类型的变量,用来储存单个参数
|
va_start(args,fmt); //使args指向可变参数的第一个参数
|
vsnprintf(temp, 512, fmt, args); //必须用vprintf等带V的
|
va_end(args); //结束可变参数的获取
|
|
if(log_run){
|
std::string lc(temp);
|
lc = "LIB-libcffmpeg.so-> " + lc;
|
spdlog::get(log_name)->error(lc);
|
spdlog::get(log_name)->flush();
|
}else{
|
printf("%s\n", temp);
|
}
|
|
}
|
|
}
|