项目需求:
需求分析:
log_test.h
- #ifndef LOG_TEST_H_
- #define LOG_TEST_H_
- #include
- #include
-
- namespace my_log_test {
- //log switch
- #define MY_LOG_DEBUG 1
-
- //log write to output console
- #define MY_LOG_OUTPUT_CONSOLE 1
-
- //log buffer size
- #define MY_LOG_BUFFER_SIZE 512
-
- constexpr const char* g_testDirPath { "./testDir/" };
-
- typedef enum {
- LOG_FATAL = 0,
- LOG_ERROR = 1,
- LOG_WARN = 2,
- LOG_INFO = 3,
- LOG_UNKNOW
- } MY_LOG_LEVEL;
-
- class MyLogTest {
- public:
- MyLogTest(std::string fileName, std::string moduleName = "");
- ~MyLogTest();
-
- static void LogPrint(MY_LOG_LEVEL level, const char* pcFunc, const int& line, const char* fmt, ...);
- private:
- std::ofstream writeOf;
- bool fsOpenFlag {false};
- }; //end of class MyLogTest
-
- //日志打印函数,可变参数。可通过宏开关,整个代码不编译
- #ifdef MY_LOG_DEBUG
- #ifndef MY_LOGERR
- #define MY_LOGERR(fmt, args...) MyLogTest::LogPrint(LOG_ERROR, __FUNCTION__, __LINE__, fmt, ## args)
- #endif
- #else
- #define MY_LOGERR(fmt, args...) ((void)0)
- #endif
-
- //日志打印函数,可变参数。可通过宏开关,整个代码不编译
- #ifdef MY_LOG_DEBUG
- #ifndef MY_LOGWAR
- #define MY_LOGWAR(fmt, args...) MyLogTest::LogPrint(LOG_WARN, __FUNCTION__, __LINE__, fmt, ## args)
- #endif
- #else
- #define MY_LOGWAR(fmt, args...) ((void)0)
- #endif
-
-
- //类使用宏,可通过宏开关,整个代码不编译
- #ifdef MY_LOG_DEBUG
- #ifndef DefineMyLogTest
- #define DefineMyLogTest(fileName, moduleName) \
- MyLogTest myLogTest(fileName, moduleName)
- #endif
- #else
- #define DefineMyLogTest(fileName, moduleName) ((void)0)
- #endif
-
- }//end of namespace my_log_test
-
- #endif //LOG_TEST_H_
log_test.cpp
- #include
- #include
-
- //获取系统时间,可变参数
- #include
- #include
- #include
- #include "log_test.h"
-
- namespace my_log_test {
- void * BaseMyLogTest { nullptr };
-
- //获取当前系统时间
- void gettime(std::string *date_time)
- {
- time_t rawtime;
- struct tm *ptminfo;
- char buf[32];
-
- time(&rawtime);
- ptminfo = localtime(&rawtime);
- strftime(buf, 32, "%Y-%m-%d %H:%M:%S", ptminfo);
- *date_time = buf;
- //std::cout<<"date-time: " <
- }
-
- MyLogTest::MyLogTest(std::string fileName, std::string moduleName)
- {
- if( fileName.empty()) {
- std::string file {__FILE__};
- if(file.find(".cpp")) {
- fileName = file.substr(0, file.size()-5);
- }
- }
-
- if(!moduleName.empty()) {
- fileName = moduleName + "-" + fileName;
- }
-
- BaseMyLogTest = (void*)this;
-
- fileName = g_testDirPath + fileName;
- writeOf.open(fileName, std::ios::out);
- if(writeOf.fail()) {
- std::cout<<__FUNCTION__<<" open file failed: "<< fileName <
- fsOpenFlag = false;
- }else {
- fsOpenFlag = true;
- }
-
- if(fsOpenFlag) {
- std::string date_time {""};
- gettime(&date_time);
- std::cout<<__FUNCTION__<<" "<
-
- writeOf <<"My log test Begin date-time: "<
- }
- }
-
- MyLogTest::~MyLogTest()
- {
- if(fsOpenFlag) {
- writeOf.close();
- }
- }
-
- void MyLogTest::LogPrint(MY_LOG_LEVEL level, const char*pcFunc, const int& line, const char* fmt, ...)
- {
- char buffer[MY_LOG_BUFFER_SIZE];
- int n = sprintf(buffer, "[%s]-[%d]", pcFunc, line);
-
- va_list vap;
- va_start(vap, fmt);
- vsnprintf(buffer + n, MY_LOG_BUFFER_SIZE-n, fmt, vap);
- va_end(vap);
-
- std::string logLevelStr {""};
- switch(level) {
- case LOG_ERROR:
- logLevelStr = "Error";
- break;
- case LOG_WARN:
- logLevelStr = "Warn";
- break;
- default:
- logLevelStr = "Error";
- break;
- }
- std::string dt {""};
- gettime(&dt);
-
- #ifdef MY_LOG_OUTPUT_CONSOLE
- std::cout<
- " [" <
"]: " < -
- #else
- MyLogTest *myLogTest = (MyLogTest*)BaseMyLogTest ;
- myLogTest->writeOf <
- " [" <
"]: " < - #endif
- }
- }//end of namespace my_log_test
-
main.cpp
- #include
- #include "log_test.h"
-
- using namespace my_log_test;
-
- int main(int argc, char* argv[])
- {
- std::cout<< __FUNCTION__ <<" Begin of the program!"<< std::endl;
-
- DefineMyLogTest("test_file", "test_module");
- std::string strErr {"string error!"};
- std::string strWar {"string waring!"};
-
- MY_LOGERR("my log test error: %s", strErr.c_str());
- MY_LOGWAR("my log test warn: %s", strWar.c_str();
-
- std::cout<< __FUNCTION__ <<" End of the program!"<< std::endl;
- return 0;
- }
代码测试
当前代码在 ubuntu 系统上运行,通过下面命令进行编译。
g++ -std=c++11 log_test.cpp main.cpp -o test
在当前目录创建 ”testDir“ 文件夹,执行 ./test
即会在终端打印部分日志,并在文件 ./testDir/test_module_test_file 中写入日志,及时间戳。
-
相关阅读:
vue3.x中父组件添加自定义参数后,如何获取子组件$emit传递过来的参数
React使用forward和useImperativeHandle实现父组件调用子组件
ubuntu 22.04配置静态ip
面试10000次依然会问的【线程池】,你还不会?
云原生2.0时代,如何让应用拥抱云原生?
Android 开发学习(二)
爬取任意百度贴吧评论(可直接Copy)
基于 idea 将 springboot 应用部署到 docker环境
【C++深入浅出】日期类的实现
载阿霉素PLGA-PLL-PEG纳米粒|马钱子碱mPEG-PLGA纳米粒|PLGA-PEG包裹液态PFP载紫杉醇纳米粒
-
原文地址:https://blog.csdn.net/SHK242673/article/details/126343969