c++打印封裝每次打印前面加上時(shí)間戳問題
c++打印封裝每次打印前面加上時(shí)間戳
封裝之后我們打印不必每次都加上時(shí)間
#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++獲取、打印當(dāng)前時(shí)間:time、localtime
先來總結(jié)下
1、函數(shù)1為基本的獲取time_t格式時(shí)間函數(shù);
2、函數(shù)3、4為轉(zhuǎn)換為tm
格式時(shí)間函數(shù);
3、函數(shù)2、5、6為輸出可讀格式時(shí)間函數(shù)。
4、其中函數(shù)2、5不符合使用習(xí)慣,因此不長使用,常用函數(shù)6定制化輸出。
以下函數(shù)全部在#include <ctime>
中。
1、time_t time(time_t *seconds):
函數(shù)描述:返回基于當(dāng)前系統(tǒng)的自紀(jì)元起經(jīng)過的時(shí)間,以秒為單位。
參數(shù)/返回值: seconds,存儲獲取的時(shí)間。
使用:
time_t now = time(nullptr);
2、char *ctime(const time_t *timer):
函數(shù)描述:返回一個(gè)表示時(shí)間的字符串。
格式:
Www Mmm dd hh:mm:ss yyyy(Mon Apr 05 15:23:17 2021)
其中,Www表示星期,Mmm表示月份,dd表示天數(shù),hh:mm:ss表示時(shí)間,yyyy表示年份。
參數(shù):time_t
類型的指針。
返回值: c字符串,包含可讀格式的日期時(shí)間信息。
使用:
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
結(jié)構(gòu)。
參數(shù):time_t
類型的指針。
返回值: 返回指向tm
結(jié)構(gòu)的指針,本地時(shí)間。
使用:
tm* curr_tm = localtime(&now);
4、struct tm *gmtime(const time_t *timer):
函數(shù)描述:使用timer
的值來填充tm結(jié)構(gòu)。
參數(shù):time_t
類型的指針。
返回值: 返回指向tm
結(jié)構(gòu)的指針,GMT時(shí)間。
使用:
tm* curr_tm = gmtime(&now);
5、char *asctime(const struct tm *timeptr):
函數(shù)描述:將tm
結(jié)構(gòu)體表示的時(shí)間返回為可讀的字符串類型。
參數(shù):tm
結(jié)構(gòu)體類型的指針。
返回值: c字符串,包含可讀格式的日期時(shí)間信息。
使用:
char* curr_time2 = asctime(curr_tm);
注:函數(shù)2 = 函數(shù)3/4 + 函數(shù)5; // 函數(shù)2實(shí)現(xiàn)的功能與3/4+5實(shí)現(xiàn)的一致。
6、size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr):
函數(shù)描述:根據(jù) format
中定義的格式化規(guī)則,格式化結(jié)構(gòu) timeptr
表示的時(shí)間,并把它存儲在 str
中。
參數(shù):
- str:這是指向目標(biāo)數(shù)組的指針,用來復(fù)制產(chǎn)生的 C 字符串。
- maxsize:這是被復(fù)制到 str 的最大字符數(shù)。
- format:指定的 C 格式字符串。
使用:
time_t now = time(nullptr); tm* curr_tm = localtime(&now); // 返回的結(jié)構(gòu)體存儲位置未知,不知何時(shí)釋放,因此推薦使用安全版本。 char time[80] = {0}; strftime(time, 80, "%Y-%m-%d %H:%M:%S", curr_tm);
最后
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- C/C++實(shí)現(xiàn)重置文件時(shí)間戳
- 關(guān)于C++使用std::chrono獲取當(dāng)前秒級/毫秒級/微秒級/納秒級時(shí)間戳問題
- C++?std::chrono庫使用示例(實(shí)現(xiàn)C++?獲取日期,時(shí)間戳,計(jì)時(shí)等功能)
- C++ 中時(shí)間與時(shí)間戳的轉(zhuǎn)換實(shí)例詳解
- C++時(shí)間戳轉(zhuǎn)化操作實(shí)例分析【涉及GMT與CST時(shí)區(qū)轉(zhuǎn)化】
- C++時(shí)間戳轉(zhuǎn)換成日期時(shí)間的步驟和示例代碼
- c++之time_t和struct tm及時(shí)間戳的正確使用方式
相關(guān)文章
C語言浮點(diǎn)型數(shù)據(jù)在內(nèi)存中的存儲方式詳解
任何數(shù)據(jù)在內(nèi)存中都是以二進(jìn)制的形式存儲的,例如一個(gè)short型數(shù)據(jù)1156,其二進(jìn)制表示形式為00000100 10000100,下面這篇文章主要給大家介紹了關(guān)于C語言浮點(diǎn)型數(shù)據(jù)在內(nèi)存中的存儲方式,需要的朋友可以參考下2023-03-03C語言數(shù)據(jù)結(jié)構(gòu)之圖的遍歷實(shí)例詳解
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之圖的遍歷實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07C語言學(xué)生信息管理系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了C語言學(xué)生信息管理系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01解析C++中多層派生時(shí)的構(gòu)造函數(shù)及一些特殊形式
這篇文章主要介紹了解析C++中多層派生時(shí)的構(gòu)造函數(shù)及一些特殊形式,特殊形式主要針對基類和子對象類型的構(gòu)造函數(shù)內(nèi)容,需要的朋友可以參考下2015-09-09C++實(shí)現(xiàn)LeetCode(53.最大子數(shù)組)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(53.最大子數(shù)組),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)開發(fā)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08