From 6fef801bcb910710b3c7314d7eac044c6c02aae1 Mon Sep 17 00:00:00 2001 From: Guo XIn <371864209@qq.com> Date: Wed, 17 Jan 2024 10:07:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=A2=AB=E6=B5=8B=E4=BB=B6?= =?UTF-8?q?=EF=BC=9A=E6=85=8E=E9=87=8D=E8=80=83=E8=99=91=E5=86=99=E5=85=A5?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=E4=BF=A1=E6=81=AF=E7=9A=84?= =?UTF-8?q?=E9=9A=90=E7=A7=81=E6=80=A7=E3=80=82=E9=81=BF=E5=85=8D=E6=8A=8A?= =?UTF-8?q?=E6=95=8F=E6=84=9F=E4=BF=A1=E6=81=AF=E5=86=99=E5=85=A5=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cxx/rules/checkers/LogFileWriteChecker.cc | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/LogFileWriteChecker.cc diff --git a/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/LogFileWriteChecker.cc b/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/LogFileWriteChecker.cc new file mode 100644 index 0000000..0626055 --- /dev/null +++ b/sonar-keyware-plugins-cxx/src/test/resources/com/keyware/sonar/cxx/rules/checkers/LogFileWriteChecker.cc @@ -0,0 +1,59 @@ +#include "spdlog/spdlog.h" +#include +#include +#include +#include +#include + +std::string username = "user1"; +std::string orgName = "org1"; +std::string armsName = "arms1"; + +// 创建一个rotating file sink,日志文件大小达到1MB时滚动并创建新的日志文件,最多保存3个历史文件 +auto rotating_logger = spdlog::rotating_logger_mt("my_logger", "logs/mylogfile.log", 1048576, 3); +// 设置日志级别为info及以上 +rotating_logger->set_level(spdlog::level::info); + +int writeLogFile1() { + + // 写入不同级别的日志信息 + rotating_logger->track("This is an track message"); + rotating_logger->debug("This is an debug message"); + rotating_logger->info("This is an info message"); + rotating_logger->warn("This is a warning"); + rotating_logger->error("This is an error"); + + rotating_logger->track("This is an track message" + armsName); // error + rotating_logger->debug("This is an debug message %s, %s", username, orgName); // error + rotating_logger->info("This is an info message" + armsName); // error + rotating_logger->warn("This is a warning" + armsName); // error + rotating_logger->error("This is an error" + armsName); // error + + return 0; +} + +int writeLogFile2() { + log4cpp::PatternLayout* layout = new log4cpp::PatternLayout(); + layout->setConversionPattern("%d{%Y-%m-%d %H:%M:%S} [%p] %c: %m%n"); + + // 创建FileAppender,并关联布局 + log4cpp::FileAppender *fileAppender = new log4cpp::FileAppender("fileAppender", "logs/mylogfile.log"); + fileAppender->setLayout(layout); + + // 获取或创建一个category,并将appender添加给它 + log4cpp::Category& root = log4cpp::Category::getRoot(); + root.addAppender(fileAppender); + root.setPriority(log4cpp::Priority::INFO); // 设置最低记录的日志级别 + + // 写入不同级别的日志信息 + log4cpp::Category& myCat = log4cpp::Category::getInstance(std::string("MyCategory")); + myCat.track("This is an track message" + armsName); // error + myCat.debug("This is an debug message %s, %s", username, orgName); // error + myCat.info("This is an info message" + armsName); // error + myCat.warn("This is a warning" + armsName); // error + myCat.error("This is an error" + armsName); // error + + delete layout; + + return 0; +} \ No newline at end of file