c++打印封裝每次打印前面加上時間戳問題
c++打印封裝每次打印前面加上時間戳
封裝之后我們打印不必每次都加上時間
#include <iostream>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <mutex>
#include <pthread.h>
#include <thread>
#include <unistd.h>
std::mutex g_mtx;
void printfTime()
{
char buf[32] = {0};
struct timeval tv;
struct tm tm;
size_t len = 28;
memset(&tv, 0, sizeof(tv));
memset(&tm, 0, sizeof(tm));
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &tm);
strftime(buf, len, "%Y-%m-%d %H:%M:%S", &tm);
len = strlen(buf);
sprintf(buf + len, ".%-6.3d", (int)(tv.tv_usec/1000));
printf("%s",buf);
}
void printXX()
{
std::cout<<std::endl;
}
template <typename T,typename... types>
void printXX(const T& firstArg,const types&... arges)
{
std::cout<<firstArg;
printXX(arges...);
}
template <typename T,typename... types>
void mvPrintf(const T& firstArg,const types&... arges)
{
std::lock_guard<std::mutex> lock(g_mtx);
printfTime();
printXX(firstArg,arges...);
}
int main()
{
int i = 0;
std::string a="ddddd";
mvPrintf("i = ", i, " a = ",a);
i = 1000;
mvPrintf("i = ", i, " a = ",a);
mvPrintf("ssssssssssssssssssssssssssssssss ");
return 0;
}輸出:

c++獲取、打印當前時間:time、localtime
先來總結下
1、函數(shù)1為基本的獲取time_t格式時間函數(shù);
2、函數(shù)3、4為轉換為tm格式時間函數(shù);
3、函數(shù)2、5、6為輸出可讀格式時間函數(shù)。
4、其中函數(shù)2、5不符合使用習慣,因此不長使用,常用函數(shù)6定制化輸出。
以下函數(shù)全部在#include <ctime>中。
1、time_t time(time_t *seconds):
函數(shù)描述:返回基于當前系統(tǒng)的自紀元起經(jīng)過的時間,以秒為單位。
參數(shù)/返回值: seconds,存儲獲取的時間。
使用:
time_t now = time(nullptr);
2、char *ctime(const time_t *timer):
函數(shù)描述:返回一個表示時間的字符串。
格式:
Www Mmm dd hh:mm:ss yyyy(Mon Apr 05 15:23:17 2021)
其中,Www表示星期,Mmm表示月份,dd表示天數(shù),hh:mm:ss表示時間,yyyy表示年份。
參數(shù):time_t類型的指針。
返回值: c字符串,包含可讀格式的日期時間信息。
使用:
char* curr_time = ctime(&now); cout << curr_time <<endl; // Mon Apr 05 15:23:17 2021
3、struct tm *localtime(const time_t *timer):
函數(shù)描述:使用timer的值來填充tm結構。
參數(shù):time_t類型的指針。
返回值: 返回指向tm結構的指針,本地時間。

使用:
tm* curr_tm = localtime(&now);
4、struct tm *gmtime(const time_t *timer):
函數(shù)描述:使用timer的值來填充tm結構。
參數(shù):time_t類型的指針。
返回值: 返回指向tm結構的指針,GMT時間。
使用:
tm* curr_tm = gmtime(&now);
5、char *asctime(const struct tm *timeptr):
函數(shù)描述:將tm結構體表示的時間返回為可讀的字符串類型。
參數(shù):tm結構體類型的指針。
返回值: c字符串,包含可讀格式的日期時間信息。
使用:
char* curr_time2 = asctime(curr_tm);
注:函數(shù)2 = 函數(shù)3/4 + 函數(shù)5; // 函數(shù)2實現(xiàn)的功能與3/4+5實現(xiàn)的一致。
6、size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr):
函數(shù)描述:根據(jù) format 中定義的格式化規(guī)則,格式化結構 timeptr 表示的時間,并把它存儲在 str 中。
參數(shù):
- str:這是指向目標數(shù)組的指針,用來復制產生的 C 字符串。
- maxsize:這是被復制到 str 的最大字符數(shù)。
- format:指定的 C 格式字符串。
使用:
time_t now = time(nullptr);
tm* curr_tm = localtime(&now); // 返回的結構體存儲位置未知,不知何時釋放,因此推薦使用安全版本。
char time[80] = {0};
strftime(time, 80, "%Y-%m-%d %H:%M:%S", curr_tm);
最后
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
C++實現(xiàn)LeetCode(53.最大子數(shù)組)
這篇文章主要介紹了C++實現(xiàn)LeetCode(53.最大子數(shù)組),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07
C語言實現(xiàn)學生信息管理系統(tǒng)開發(fā)
這篇文章主要為大家詳細介紹了C語言實現(xiàn)學生信息管理系統(tǒng)開發(fā),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08

