当前位置: 首页 > wzjs >正文

手机端的网站怎么做的糕点网站策划书

手机端的网站怎么做的,糕点网站策划书,wordpress伪静态 404,整合营销传播名词解释什么是设计模式 IT⾏业 ,为了让 菜鸡们不太拖⼤佬的后腿, 于是⼤佬们针对⼀些经典的常⻅的场景, 给定了⼀些对应的解决⽅案, 这个就是 设计模式 日志认识 计算机中的⽇志是记录系统和软件运⾏中发⽣事件的⽂件,主要作⽤是监控运⾏状态、记录异常信 息&#xff…

什么是设计模式

IT⾏业 ,为了让 菜鸡们不太拖⼤佬的后腿, 于是⼤佬们针对⼀些经典的常⻅的场景, 给定了⼀些对应的解决⽅案, 这个就是  设计模式 

日志认识

计算机中的⽇志是记录系统和软件运⾏中发⽣事件的⽂件,主要作⽤是监控运⾏状态、记录异常信 息,帮助快速定位问题并⽀持程序员进⾏问题修复。它是系统维护、故障排查和安全管理的重要⼯ 具。

⽇志格式以下⼏个指标是必须得有的

  • 时间戳
  • ⽇志等级
  • ⽇志内容

以下几个指标是可选的

  • 文件名行号
  • 进程,线程相关id信息等

⽇志有现成的解决⽅案,如:spdlog、glog、Boost.Log、Log4cxx等等,我们依旧采⽤⾃定义⽇志的方式。

这⾥我们采⽤设计模式-策略模式来进⾏⽇志的设计,

策略模式是一种行为型设计模式,它允许在运行时选择算法或行为。该模式将算法族定义为一组可互换的策略,使得算法可以独立于使用它的客户端变化。

策略模式基于以下设计原则:

  • 封装变化:将易变的算法部分单独封装

  • 面向接口编程:定义策略接口,而不是具体实现

  • 组合优于继承:通过组合策略对象来获得灵活性,而非通过继承

策略模式包含三个主要角色:

  1. Context(上下文)

    • 维护对策略对象的引用

    • 可以定义一个接口让策略访问它的数据

  2. Strategy(策略接口)

    • 定义所有支持的算法的公共接口

    • Context使用这个接口调用具体策略定义的算法

  3. ConcreteStrategy(具体策略)

    • 实现策略接口的具体算法

例子:

#include <iostream>
#include <memory>// 策略接口
class SortingStrategy {
public:virtual void sort(int* data, int size) const = 0;virtual ~SortingStrategy() = default;
};// 具体策略A:快速排序
class QuickSort : public SortingStrategy {
public:void sort(int* data, int size) const override {std::cout << "Sorting using QuickSort\n";// 实际快速排序实现...}
};// 具体策略B:冒泡排序
class BubbleSort : public SortingStrategy {
public:void sort(int* data, int size) const override {std::cout << "Sorting using BubbleSort\n";// 实际冒泡排序实现...}
};// 上下文类
class Sorter {
private:std::unique_ptr<SortingStrategy> strategy;public:explicit Sorter(std::unique_ptr<SortingStrategy> strategy) : strategy(std::move(strategy)) {}void setStrategy(std::unique_ptr<SortingStrategy> newStrategy) {strategy = std::move(newStrategy);}void executeSort(int* data, int size) {strategy->sort(data, size);}
};int main() {int data[] = {5, 2, 7, 1, 9};Sorter sorter(std::make_unique<QuickSort>());sorter.executeSort(data, 5);  // 使用快速排序sorter.setStrategy(std::make_unique<BubbleSort>());sorter.executeSort(data, 5);  // 改为冒泡排序return 0;
}

我们想要的⽇志格式如下:

[可读性很好的时间] [⽇志等级] [进程pid] [打印对应⽇志的⽂件名][⾏号] - 消息内容,⽀持可
变参数
[2024-08-04 12:27:03] [DEBUG] [202938] [main.cc] [16] - hello world
[2024-08-04 12:27:03] [DEBUG] [202938] [main.cc] [17] - hello world
[2024-08-04 12:27:03] [DEBUG] [202938] [main.cc] [18] - hello world
[2024-08-04 12:27:03] [DEBUG] [202938] [main.cc] [20] - hello world
[2024-08-04 12:27:03] [DEBUG] [202938] [main.cc] [21] - hello world
[2024-08-04 12:27:03] [WARNING] [202938] [main.cc] [23] - hello world

log.hpp

#pragma once
#include <iostream>
#include <string>
#include "Mutex.hpp"
#include <filesystem> //c++17
#include <fstream>    //c++文件流
#include <sstream>    //c++字符串流
#include <memory>
#include <time.h>//基于策略模式的日志
namespace LogModule
{using namespace LockModule;// 获取时间的函数std::string CurrentTime(){time_t time_stamp = ::time(nullptr); // 获取时间戳struct tm curr;//_r代表可以重入,支持多线程localtime_r(&time_stamp, &curr); // 将时间戳转化成可读性较强的时间信息char buffer[1024];// bugsnprintf(buffer, sizeof(buffer), "%4d-%02d-%02d %02d:%02d:%02d",curr.tm_year + 1900,curr.tm_mon + 1,curr.tm_mday,curr.tm_hour,curr.tm_min,curr.tm_sec);return buffer;}// 日志构成两个阶段: 一.构建日志信息  二.刷新落盘screen / file(向哪里刷新)// 二. 刷新落盘// 1. 日志文件的默认路径和名称const std::string dafaultlogpath = "./log/";const std::string dafaultlogname = "log.txt";// 2. 日志等级enum class LogLevel{DEBUG = 1,INFO, // 正常的WARNNING,ERROR,FATAL // 致命的};std::string Level2String(LogLevel level){switch (level){case LogLevel::DEBUG:return "DEBUG";case LogLevel::INFO:return "INFO";case LogLevel::WARNNING:return "WARNNING";case LogLevel::ERROR:return "ERROR";case LogLevel::FATAL:return "FATAL";default:return "NONE";}}// 3.刷新策略class LogStrategy // 基类{public:virtual ~LogStrategy() = default;   //虚析构函数(保证派生类对象能正确析构):确保通过基类指针删除派生类对象时能正确调用派生类的析构函数virtual void SyncLog(const std::string &message) = 0;//纯虚函数使得基类为抽象类 ,其派生类必须重构此函数才能构建对象};// 3.1控制台策略class ConsoleLogStrategy : public LogStrategy{public:ConsoleLogStrategy(){}~ConsoleLogStrategy(){}void SyncLog(const std::string &message){// 屏幕也是临界资源LockGuard lockguard(_mutex);std::cout << message << std::endl;}private:Mutex _mutex;};// 3.2文件级策略class FileLogStrategy : public LogStrategy{public:FileLogStrategy(const std::string &logpath = dafaultlogpath, const std::string &logname = dafaultlogname): _logpath(logpath),_logname(logname){LockGuard lockguard(_mutex);// 确认_logpath存在if (std::filesystem::exists(_logpath)){return;}try{std::filesystem::create_directories(_logpath);}catch (std::filesystem::filesystem_error &e){std::cerr << e.what() << "\n";}}~FileLogStrategy(){}void SyncLog(const std::string &message){LockGuard lockguard(_mutex);// c++文件操作std::string log = _logpath + _logname;// 创建一个ofstream文件输出流对象,以追加模式打开日志文件std::ofstream out(log, std::ios::app); // 日志是追加写入if (!out.is_open()){return;}out << message << "\n";out.close();}private:std::string _logpath;std::string _logname;Mutex _mutex; // 保证资源安全};// 一. 构建日志信息// 日志类 ,构建日志字符串(内部类实现) ,根据策略进行刷新class Logger{public:Logger(){// 默认使用控制台刷新_strategy = std::make_shared<ConsoleLogStrategy>();}void EnableConsoleLog(){_strategy = std::make_shared<ConsoleLogStrategy>();}void EnableFileLog(){_strategy = std::make_shared<FileLogStrategy>();}~Logger(){}// 定义了内部类   一个logmessage就包含了一条完整的日志信息//  一条完整的日志信息: [2024-08-04 12:27:03] [DEBUG] [202938] [main.cc] [16] + 日志的可变部分(<< "hello world" << 3.14 << a << b;)class LogMessage{public:LogMessage(LogLevel level, const std::string &filename, int line, Logger &logger): _currtime(CurrentTime()), _level(level), _pid(getpid()), _src_name(filename), _line(line), _logger(logger){// 用stringstream进行流式拼接std::stringstream ssbuffer;ssbuffer << "[" << _currtime << "] "<< "[" << Level2String(_level) << "] " // 我们想要字符串式的日志等级<< "[" << _pid << "] "<< "[" << _src_name << "] "<< "[" << _line << "] - ";_loginfo = ssbuffer.str();}// LOG(DEBUG) << "hello " << 3.14 << a << b;想要实现需要重载<<template <typename T>LogMessage &operator<<(const T &info) // 返回使用引用 (要保证后面的信息都拼接到同一个LogMessage){std::stringstream ss;ss << info;_loginfo += ss.str();return *this; // 返回自己}~LogMessage(){// 析构时,执行Logger所对应的根据指定策略进行刷新一条方法if (_logger._strategy)//设置策略了就刷新{_logger._strategy->SyncLog(_loginfo);}}private:std::string _currtime; // 时间LogLevel _level;       // 日志等级pid_t _pid;            // 进程pidstd::string _src_name; // 原文件名称int _line;             // 行号Logger &_logger;       // 负责根据不同的策略进行刷新std::string _loginfo;  // 一条完整的日志信息};// 仿函数重载() ,返回一个完整的日志信息// 故意没有写引用 ,就是要拷贝,返回临时的LogMessage??? 临时的LogMessage 自动析构时 自动刷新日志  LogMessage operator()(LogLevel level, const std::string &filename, int line){return LogMessage(level, filename, line, *this);}private:std::shared_ptr<LogStrategy> _strategy; // 日志的刷新方案// LogStrategy是纯虚类 ,不能定义对象,能定义指针};// 使用Logger logger;#define LOG(level) logger(level, __FILE__, __LINE__) //__是预处理符    logger()运算符重载
#define ENABLE_CONSOLE_LOG() logger.EnableConsoleLog()
#define ENABLE_FILE_LOG() logger.EnableFileLog()
}

Main.cc

#include"log.hpp"using namespace LogModule;int main()
{//用C++版的流,实现可变参数//日志输出格式//LOG(DEBUG) << "hello " << 3.14 << a << b;//会被替换成下面格式//logger(level ,__FILE__ ,__LINE__)<< "hello " << 3.14 << a << b;    //()执行完构建一个临时的LogMessage,临时的LogMessage会执行<<//LogMessage<< "hello " << 3.14 << a << b;        //LogMessage重载了<<ENABLE_FILE_LOG();LOG(LogLevel::INFO)<<"hello"<<666;return 0;
}


文章转载自:

http://BaGAVnvE.wtcyz.cn
http://vJ94SfEA.wtcyz.cn
http://qAodFVeX.wtcyz.cn
http://A0aEyMSf.wtcyz.cn
http://tzhE8dqZ.wtcyz.cn
http://9lGs1Rqe.wtcyz.cn
http://G8HfbFWg.wtcyz.cn
http://apLFiwTb.wtcyz.cn
http://4YkXFJO3.wtcyz.cn
http://97jUHm2T.wtcyz.cn
http://lDT8BvEQ.wtcyz.cn
http://5R0YIFOS.wtcyz.cn
http://aGOyJIDN.wtcyz.cn
http://GqdapuUg.wtcyz.cn
http://FHgGOugw.wtcyz.cn
http://KiSHWvCp.wtcyz.cn
http://yStoY96W.wtcyz.cn
http://e2jdXVOU.wtcyz.cn
http://cQA4o1ML.wtcyz.cn
http://Yjnwtr1d.wtcyz.cn
http://jRj9dZvn.wtcyz.cn
http://aDILtVKv.wtcyz.cn
http://VLGRbbx8.wtcyz.cn
http://ogGlRZCO.wtcyz.cn
http://2pFMddm9.wtcyz.cn
http://cXlvi455.wtcyz.cn
http://oGcKU2AK.wtcyz.cn
http://3kdL4kb8.wtcyz.cn
http://vFDIWuoj.wtcyz.cn
http://22tQXNWE.wtcyz.cn
http://www.dtcms.com/wzjs/620097.html

相关文章:

  • 网站文章不收录的原因建筑工程网架
  • 邢台做网站费用甘肃省建设厅注册中心网站
  • 企业网站建设课程设计crm管理
  • 我要建个网站做灯箱片的设计网站
  • 做网站去哪里网站后台管理系统展望
  • 网站建设报价明细单怎么咨询网络服务商
  • ae做动画教程网站每天免费体验6小时的云电脑
  • 网站开发团队人员配置山东住房城乡建设厅网站
  • 负责网站建设和网络推广的wordpress dux主题5.0
  • h5免费制作平台火蚁邀请函南京网站seo服务
  • 网站怎么做右上角消息提醒做网站的赚钱吗
  • 广东的网站建设企业网站建设的策略
  • 嘉兴学网站建设酷乐家居在线设计
  • 常州武进区建设局网站中文博客网站模板
  • 网站免费部署瑞安规划建设局网站
  • 山东企业建站系统信息黄骅港旅游攻略
  • 增加网站流量设计接单渠道
  • 怎么用微信做网站电商网站建设浩森宇特
  • 提供虚拟主机服务的网站网站建设的文档
  • 安徽省建设工程资料上传网站重庆做的好的房产网站好
  • 运城网站建设公司有多少网站loading动画
  • 怎么做企业网站推广需要多少钱seo站内优化
  • 河南网站建设找工作网络营销外包价格
  • 郑州做网站建设公司哪家好佛山网页设计怎么做
  • 网站建设有哪些分工分网上做分销代销哪个网站好
  • 什么是企业网站pv河北建设工程信息网一体化平台
  • 优秀高端网站建设报价html所有代码大全
  • 济南网站建设及推广在线文档 wordpress
  • 百度智能建站系统手机桌面布局设计软件
  • 厦门公司建站网络规划与设计报告总结