75 lines
1.6 KiB
C++
75 lines
1.6 KiB
C++
|
|
#include "mylog.h"
|
|||
|
|
|
|||
|
|
namespace ai_matrix
|
|||
|
|
{
|
|||
|
|
MyLog::~MyLog()
|
|||
|
|
{
|
|||
|
|
if (of_.is_open())
|
|||
|
|
{
|
|||
|
|
of_.close(); //关闭文件
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::string MyLog::get_name() const
|
|||
|
|
{
|
|||
|
|
return name_;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void MyLog::open_file(std::string filename)
|
|||
|
|
{
|
|||
|
|
if (of_.is_open())
|
|||
|
|
{
|
|||
|
|
of_.close(); //关闭文件
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
of_.open(filename.c_str(), std::ios_base::out | std::ios_base::app);
|
|||
|
|
|
|||
|
|
assert(of_.is_open() && "file create failed, please check the file's name and path.");
|
|||
|
|
|
|||
|
|
name_ = filename;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void MyLog::close()
|
|||
|
|
{
|
|||
|
|
if (of_.is_open())
|
|||
|
|
{
|
|||
|
|
of_.close(); //关闭文件
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool MyLog::is_open()
|
|||
|
|
{
|
|||
|
|
return of_.is_open();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void MyLog::write(std::string value)
|
|||
|
|
{
|
|||
|
|
if (of_.is_open() == false)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
long int usValue = 0;
|
|||
|
|
struct timeval time = {0, 0};
|
|||
|
|
gettimeofday(&time, nullptr);
|
|||
|
|
time_t timep = time.tv_sec;
|
|||
|
|
struct tm *ptm = gmtime(&timep); //返回tm结构的格林尼治时间(GMT)
|
|||
|
|
char timeString[32] = {0};
|
|||
|
|
//%F 年-月-日 %X 标准的时间串
|
|||
|
|
strftime(timeString, 32, "[%F %X:", ptm);
|
|||
|
|
usValue = time.tv_usec;
|
|||
|
|
|
|||
|
|
//时间
|
|||
|
|
of_.fill('0');
|
|||
|
|
of_ << timeString << std::setw(3) << usValue / 1000 << "]";
|
|||
|
|
|
|||
|
|
//位置
|
|||
|
|
std::string file = __FILE__;
|
|||
|
|
std::string fileName = file.substr(file.rfind('/') + 1);
|
|||
|
|
of_ << "[" << fileName << " " << __FUNCTION__ << ":" << __LINE__ << "] ";
|
|||
|
|
|
|||
|
|
//内容
|
|||
|
|
of_ << value << std::endl;
|
|||
|
|
}
|
|||
|
|
}
|