linux:
#pragma once
#include <chrono>
static std::time_t getTimeStamp()
{
std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
auto tmp=std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
std::time_t timestamp = tmp.count();
return timestamp;
}
static std::tm* gettm(uint64_t timestamp)
{
uint64_t milli = timestamp;
milli += (uint64_t)8*60*60*1000;//add to beijing time zone.
auto mTime = std::chrono::milliseconds(milli);
auto tp=std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds>(mTime);
auto tt = std::chrono::system_clock::to_time_t(tp);
std::tm* now = std::gmtime(&tt);
return now;
}
static std::string getTimeStr()
{
time_t timep;
timep = getTimeStamp();
struct tm *info;
info = gettm(timep);
char tmp[27] = {0};
sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d.%06ld", info->tm_year+1900, info->tm_mon+1, info->tm_mday, info->tm_hour, info->tm_min, info->tm_sec, timep%1000000);
return tmp;
}
#define DEBUG_FLAG
#ifdef DEBUG_FLAG
#define MY_LOGD(format, ...) {printf("%s: D/%s[%s]: ", getTimeStr().c_str(), basename(__FILE__), __func__);printf(format, ##__VA_ARGS__);printf("\n");}
#else
#define MY_LOGD(format, ...)
#endif
#define MY_LOGE(format, arg...) {printf("%s: E/%s[%s]: ", getTimeStr().c_str(), basename(__FILE__), __func__);if(strlen(format)>0){printf(format, ##arg);}printf("\n");}
#define FUNCTION_NAME MY_LOGD(" ");
#define FUNCTION_IN MY_LOGD("+")
#define FUNCTION_OUT MY_LOGD("-")
windows:
#pragma once
#include <chrono>
#ifdef WIN32
#include <ctime>
#include <filesystem>
#endif
static std::time_t getTimeStamp()
{
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
std::time_t timestamp = tmp.count();
return timestamp;
}
static std::tm* gettm(uint64_t timestamp)
{
uint64_t milli = timestamp;
milli += (uint64_t)8 * 60 * 60 * 1000;//add to beijing time zone.
auto mTime = std::chrono::milliseconds(milli);
auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(mTime);
auto tt = std::chrono::system_clock::to_time_t(tp);
std::tm* now = std::gmtime(&tt);
return now;
}
static std::string getTimeStr()
{
time_t timep;
timep = getTimeStamp();
struct tm *info;
info = gettm(timep);
char tmp[27] = { 0 };
sprintf_s(tmp, "%04d-%02d-%02d %02d:%02d:%02d.%06ld", info->tm_year + 1900, info->tm_mon + 1, info->tm_mday, info->tm_hour, info->tm_min, info->tm_sec, (long)timep % 1000000);
return tmp;
}
static void string_replace(std::string &strBig, const std::string &strsrc, const std::string &strdst)
{
std::string::size_type pos = 0;
std::string::size_type srclen = strsrc.size();
std::string::size_type dstlen = strdst.size();
while ((pos = strBig.find(strsrc, pos)) != std::string::npos)
{
strBig.replace(pos, srclen, strdst);
pos += dstlen;
}
}
static std::string GetPathOrURLShortName(std::string strFullName)
{
if (strFullName.empty())
{
return "";
}
string_replace(strFullName, "/", "\\");
std::string::size_type iPos = strFullName.find_last_of('\\') + 1;
return strFullName.substr(iPos, strFullName.length() - iPos);
}
#define DEBUG_FLAG
#ifdef DEBUG_FLAG
#define MY_LOGD(format, ...) {printf("%s: D/%s[%s]: ", getTimeStr().c_str(), GetPathOrURLShortName(__FILE__).c_str(), __func__);printf(format, ##__VA_ARGS__);printf("\n");}
#else
#define MY_LOGD(format, ...)
#endif
#define MY_LOGE(format, ...) {printf("%s: E/%s[%s]: ", getTimeStr().c_str(), GetPathOrURLShortName(__FILE__).c_str(), __func__);if(strlen(format)>0){printf(format, ##__VA_ARGS__);}printf("\n");}
#define FUNCTION_NAME MY_LOGD(" ");
#define FUNCTION_IN MY_LOGD("+")
#define FUNCTION_OUT MY_LOGD("-")
save file:
#include <ctime>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdarg.h>
#include <direct.h>
#include <string>
#include <io.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <time.h>
#include <fcntl.h>
/******************************
* 初始化日志
******************************/
static FILE *logFile;
void initLogFile(const char* dir="e:/") {
time_t seconds = time(NULL); //获取时间
struct tm *p;
p = localtime(&seconds);//获取本地时间
char filePath[100] = {0};
sprintf_s(filePath,"%s/log%04d-%02d-%02d_%02d%02d%02d.log",dir,1900+p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
logFile = fopen(filePath, "w");
if ( logFile == NULL ) {
printf("can not open log file: %s/n", filePath);
}
}
/******************************
* 打印日志
******************************/
void log(const char *fmt ...) {
if(logFile == NULL) {
initLogFile();
}
//获取本地时间
time_t seconds = time(NULL);
struct tm *p;
p = localtime(&seconds);
// 毫秒
struct timeb tb;
ftime(&tb);
char prefix[100] = {0};
sprintf_s(prefix, "[%04d-%02d-%02d %02d:%02d:%02d.%03d] >> ",1900+p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec, tb.millitm);
fprintf(logFile, "%s", prefix);
va_list vp;
va_start(vp, fmt);
vfprintf(logFile, fmt, vp);
//fprintf(logFile, "\n");
va_end(vp);
fflush(logFile);
}
/******************************
* 打印日志(无前缀)
******************************/
void logNoPrefix(const char *fmt ...) {
if(logFile == NULL) {
initLogFile();
}
va_list vp;
va_start(vp, fmt);
vfprintf(logFile, fmt, vp);
va_end(vp);
fflush(logFile);
}