电子商务网站建设可运用的技术贵阳网站设计报价

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

电子商务网站建设可运用的技术,贵阳网站设计报价,湖南省住房和城乡建设网站,社交软件一、借用标准库模板构造自己的模板 通常#xff0c;模板设计是遵循当对象的类型不影响类中函数的行为时就使用模板。这也就是为何标准库提供大部分的模板都是与容器、迭代器、适配器、通用算法等有关#xff0c;因为这些主要是除了对象集合行为#xff0c;如读写、增删、遍历…一、借用标准库模板构造自己的模板 通常模板设计是遵循当对象的类型不影响类中函数的行为时就使用模板。这也就是为何标准库提供大部分的模板都是与容器、迭代器、适配器、通用算法等有关因为这些主要是除了对象集合行为如读写、增删、遍历、排序、比较等大多与对象类型无关的逻辑行为。 在实际项目中本人建议大家在使用模板时采用的最好模板设计方式是走适配器方法就像标准库基于vector、list、deque这些基准容器通过适配封装去实现stack、queue、priority queue这些容器适配器一样。我们根据自己项目需要采用标准库提供的容器及算法创建自己的类模板应用到项目中。 二、基于标准库模板创建自定义类模板案例 2.1 自定义容器适配器 在本文中将设计一个日志消息队列的容器适配器基于双端队列(deque)容器和互斥锁通过适配创建一个线程安全的消息队列类模板。该消息队列类通过二次封装实现数据末端添加数据头部取出数据队列大小有限制的跨线程安全消息缓冲区。 首先创建一个queuedata.h和queuedata.cpp源文件在queuedata.h文件实现消息队列类模板进行声明在queuedata.cpp源文件进行定义在queuedata.h文件末通过#include queuedata.cpp引用定义内容。本文中QueueData类模板虽然说类似容器适配器但是没有提供配套的迭代器iterator大家感兴趣的可以自行实现一下就是将deque容器的迭代器iterator做一下中转即可。 #if _MSC_VER 1000 #pragma once #endif // _MSC_VER 1000#ifndef _QUEUE_DATAH #define _QUEUE_DATAH/**********************************************************************Copyright 2023-02-06, pyfreeFile Name : queuedata.h*File Mark : *Summary : *数据队列类模板,线程安全对std::deque进行适配通过PYMutex锁约束线程安全提供数据增删等功能接口Current Version : 1.00*Author : pyfree*FinishDate :Replace Version :*Author :FinishDate :**********************************************************************/ #include deque #include queue #include stdio.h #include string.h #include string#include Mutex.htemplate class T class QueueData { public:QueueData(std::string desc thread_queue);~QueueData();/// 获取队列大小* return {int } 队列大小/int size();/** 判定队列是否为空* return {bool } 是否为空队列/bool isEmpty();/** 获取队列头元素* param it {T} 头元素* return {bool } 是否成功/bool getFirst(T it);/** 删除元素* return {bool } 是否成功/bool removeFirst();/** 获取队列头元素并从队列终删除* param it {T} 头元素* return {bool } 是否成功/bool pop(T it);/** 从队列头开始逐步获取多个元素并剔除* param its {queueT} 获取到的元素集* param sizel {int} 一次获取多少个* return {bool } 至少获取一个元素以上则成功/bool getList(std::queueT its,unsigned int sizel5);/** 从队列尾部添加元素* param it {T} 被添加元素* return {void } 无返回/void add(T it);/** 从队列头部添加元素* param it {T} 被添加元素* return {void } 无返回/void add_front(T it);/** 清空元素* return {void }*/void clear(); private:void init();QueueData operator(const QueueData) {return this;}; protected:std::string queue_desc; private:/点集转发//协议解析结果缓存std::dequeT datacache_queue; //队列容器PYMutex m_Mutex; //线程锁,或者如果更彻底采用acl库,采用acl::thread_mutex替代//static unsigned int QSize; //队列大小约束,超出是会从队列头剔除旧数据腾出空位在对末添加数据//int queue_overS; //队列溢出次数计数 };#include queuedata.cpp#endif //_QUEUE_DATAH 2.2 非类型模板参数的结构体模板 传入消息队列类的信息是一个非类型模板参数的结构体模板可以根据实际项目需要选择消息缓存区最大字节数。创建LogDef.h源文件实现消息结构体模板并定义日志等级枚举值。该结构体在定义对象时可以采用默认的方法MyLogStruct obj也可以指定大小MyLogStructDATA_SIZE_MAX_LEVLE2 obj或指定任意值作为缓存区域大小MyLogStruct100 obj。 #ifndef _LOG_DEFH #define _LOG_DEFH /**********************************************************************Copyright 2023-02-06, pyfreeFile Name : LogDef.h*File Mark : *Summary : Log线程类使用到的日志等级枚举类型日志信息格式-用于日志缓冲区存放日志的结构体Current Version : 1.00*Author : pyfree*FinishDate :Replace Version :*Author :*FinishDate :*********************************************************************/ #ifdef linux #include string.h #endif typedef enum eLogType {eLog_DEBUG 1,eLog_TRACE 2,eLog_NOTICE 3,eLog_WARNING 4,eLog_ERROR 5 }LogLevel;#define DATA_SIZE_MAX_LEVLE1 1024 //日志缓存信息空间大小(1024字节) #define DATA_SIZE_MAX_LEVLE2 512 //日志缓存信息空间大小(512字节) #define DATA_SIZE_MAX_LEVLE3 256 //日志缓存信息空间大小(256字节)//日志缓存信息结构体,采用结构体默认赋值 templateint SIZE DATA_SIZE_MAX_LEVLE3 struct MyLogStruct {MyLogStruct():type(0){memset(szBuffer, 0, SIZE);};int type;char szBuffer[SIZE]; };#endif //_LOG_DEFH 2.3 日志记录类设计 下来就是日志记录功能部分创一个日志类该类有一个QueueDataMyLogStruct logs_cache对象用来缓存日志。该日志类还是一个单体模式设计并继承一个线程类。线程循环体内日志对象会不断去读取缓存区内的日志信息如果读取到信息就写入到日志文件内。另外为了方式日志记录过大造成磁盘存储空间不足将有一个DiskSpaceMgr类对象来定期巡检磁盘剩余空间在存储空间不足时去删除旧的日志信息。 创建Log.h和Log.cpp源文件Log.h源文件如下 #ifndef _CLOGGERH #define _CLOGGERH /*******************************************************************Copyright 2023-02-06, pyfreeFile Name : Log.h*File Mark : Summary : Log写入独立线程类提供添加日志信息进入缓存接口自身不断从缓存读取日志信息写入文件 会启动一个磁盘空间管理线程对象DiskSpaceMgr在磁盘空间不足时删除旧日志Current Version : 1.00*Author : pyfree*FinishDate :Replace Version :*Author :FinishDate :************************************************************************/ #include myThread.h #include LogDef.h #include queuedata.hclass DiskSpaceMgr;class CLogger : public MyThread { public:void Log(const eLogType type, const char lpszFormat, …);static CLogger* createInstance( void ); private:CLogger();~CLogger();int Run();void WriteLog( const int iMsgType, const char * strMsg);void WriteLogToFile( std::string strPath,std::string strTime,const char * strMsg);std::string createFileName(std::string strTime);std::string strTime();std::string createLogTimeAndTypeDesc(const unsigned int iMsgType, std::string strTime); private:static CLogger* m_pLogInstance;bool running;FILE * fpLog;std::string cur_log_path;//for cacheQueueDataMyLogStruct logs_cache;//DiskSpaceMgr *disk_space_mgr; }; #endif //_CLOGGER_H_2.4 磁盘剩余空间管理类 对象DiskSpaceMgr *disk_space_mgr是由Logger类创建的该磁盘空间管理类是继承了类线程类的在线程循环体内不间断地巡检指定磁盘的剩余空间是否小于限定空间大小小于时将按配置要求去删除旧的日志信息。 创建spaceMgr.h和spaceMgr.cpp源文件spaceMgr.h内容如下 #if _MSC_VER 1000 #pragma once #endif // _MSC_VER 1000#ifndef _SPACE_MGRH #define _SPACE_MGRH /**********************************************************************Copyright 2023-02-06, pyfreeFile Name : spaceMgr.h*File Mark : *Summary : 磁盘空间巡检线程类根据预留空间要求删除app日志文件Current Version : 1.00*Author : pyfree*FinishDate :Replace Version :*Author :FinishDate :**********************************************************************/ #ifdef linux #include string #endif #include vector#include myThread.hclass DiskSpaceMgr : public MyThread { public:/ 构造函数,如传入参数D 2000 log log,则指明当D盘空闲空间不足2000M时,删除当前目录/log下的log(后缀)文件* param disk_ {char} 指定盘符* param fsl_ {int} 预留空间大小,单位MB* param dir_ {string} 指定目录* param ext_ {string} 扩展名* return { } */DiskSpaceMgr(char disk, int fsl,std::string dir_log,std::string extlog);virtual ~DiskSpaceMgr(void);int Run();void add(std::string dir,std::string ext); private:struct DelInfo{DelInfo(std::string dir,std::string ext): dir(dir), ext(ext_){};std::string dir;std::string ext;}; private://用于日志删除std::vectorDelInfo dirs; //存储目录char DiskStr; //日志存储磁盘int freeSizeLimit; //磁盘预留空间要求 };#endif2.5 其他辅助类及函数设计 日志类及磁盘管理类都继承了线程类MyThread是通过myThread.h和myThread.cpp声明定义的myThread.h内容如下 #ifndef _MYTHREAD_H #define _MYTHREAD_H /**********************************************************************Copyright 2023-02-06, pyfreeFile Name : myThread.h*File Mark : *Summary : *支持windows和linux的线程类,提供基本的线程功能(循环、启动、退出)Current Version : 1.00*Author : pyfree*FinishDate :Replace Version :*Author :FinishDate :************************************************************************/ #ifdef WIN32 #include process.h #include iostreamtypedef void HANDLE_THREAD; #else #include pthread.h #include unistd.htypedef pthread_t HANDLE_THREAD; #endifclass MyThread { public://funcMyThread(); // constructed function~MyThread();/*** the entity for thread running* param null {void } 无参数* return {int} 运行返回结果/virtual int Run()0;/** start thread* param null {void } 无参数* return {bool} 是否启动成功/bool start();/** get thread ID* param null {void } 无参数* return {HANDLE_THREAD} 线程句柄*/HANDLE_THREAD getThreadID(); #ifdef linux//get thread statusint getState();//wait for thread endvoid join();//wait for thread end in limit timevoid join(unsigned long millisTime); #endif public://val #ifdef linux//threadStatus-new createstatic const int THREAD_STATUS_NEW 0;//threadStatus-runningstatic const int THREAD_STATUS_RUNNING 1;//threadStatus-endstatic const int THREAD_STATUS_EXIT -1; #endif private://func #ifdef WIN32static void agent(void p); #else//get manner pointer of execution static void run0(void* pVoid);//manner of execution insidevoid* run1(); #endif private://valHANDLE_THREAD hThread; #ifdef linux//thread statusint threadStatus; #endif };#endif /* _MYTHREAD_H */消息队列类在队列内容进行变更时需要通过互斥锁确保跨线程操作安全互斥锁是由Mutex.h和Mutex.cpp源文件声明定义的Mutex.h内容如下 #ifndef PYMUTEX_H #define PYMUTEX_H#ifdef WIN32 //#include windows.h #else #include pthread.h #endiftypedef void *HANDLE;class IMutex { public:virtual ~IMutex() {}virtual void Lock() const 0;virtual bool TryLock() const 0;virtual void Unlock() const 0; };class PYMutex : public IMutex { public:PYMutex();~PYMutex();virtual void Lock() const;virtual bool TryLock() const;virtual void Unlock() const; private: #ifdef _WIN32HANDLE m_mutex; #elsemutable pthread_mutex_t m_mutex; #endif };#endif //PYMUTEX_H 磁盘管理类在进行磁盘信息读取和文件删除时是借用了DiskSpace.h和DiskSpace.cpp源文件声明定义的磁盘空间统计和日志删除相关函数集来实现的DiskSpace.h内容如下 #ifndef DISK_SPACE_H #define DISK_SPACE_H/**********************************************************************Copyright 2023-02-06, pyfreeFile Name : DiskSpace.h*File Mark : *Summary : *磁盘空间统计和日志删除相关函数集*Current Version : 1.00*Author : pyfree*FinishDate :Replace Version :*Author :*FinishDate :*******************************************************************/#include stringnamespace pyfree {/ 根据指定磁盘获取磁盘的总空间和剩余空间* param _DiskStr {char} 磁盘目录,一般win指定{C,D},linux指定{/}* param _totalSize {int} 返回磁盘总空间* param _freeSize {int} 返回磁盘剩余空间* return {int} 预留的返回值,暂指定是常量1/int getDiskFreeSpace(char _DiskStr, int _totalSize, int _freeSize);/** 根据指定目录 扩展名 天数限制等删除目录下的文件,项目用于删除旧日志操作* param dir {string}} 目录* param extname {string} 扩展名,如txt/ini/log等* param dayForLimit {int} 指定天数默认为0时会删除非当天的其他文件* return {void} 无返回/void moveOldFile(std::string _dir, const std::string extname, int dayForLimit_0);/** 获取当天凌晨时刻的偏移时间,单位秒,与1970-01-01 00:00:00起* param deviation {int} 偏移秒数* return {int} 当天凌晨时刻的偏移时间.单位秒*/int getCurDayZeroClockTime(int deviation0); };#endif 三、工程结构及编程测试 3.1 工程目录结构 按上述源文件设计给出组织这些源文件的工程目录结构 log_testbin #测试程序输出build_win #win编译中间文件存储build_linux #Linux编译中间文件存储srcDiskSpace.h #磁盘访问、文件删除等功能实现DiskSpace.cppLog.h #日志类单体模式继承线程类Log.cppLogDef.h #日志信息定义Mutex.h #互斥锁Mutex.cppmyThread.h #线程类myThread.cpppysleep.h #线程睡眠等待统一宏转换设计queuedata.h #消息队列类模板queuedata.cppspaceMgr.h #磁盘空间管理类继承线程类spaceMgr.cpptestmain.cpp #测试程序代码CMakeLists.txt #cmake工程compile.txt #工程编译指令 3.2 cmake工程配置 CMakeLists.txtcmake工程配置文件

CMake 最低版本号要求

cmake_minimum_required (VERSION 2.8)

项目信息

project (log_test) # if(WIN32)message(STATUS windows compiling…)add_definitions(-D_PLATFORM_ISWINDOWS)set(CMAKE_CXX_FLAGS_RELEASE \({CMAKE_CXX_FLAGS_RELEASE} /MT)set(CMAKE_CXX_FLAGS_DEBUG \){CMAKE_CXX_FLAGS_DEBUG} /MTd)set(WIN_OS true) else(WIN32)message(STATUS linux compiling…)add_definitions( -D_PLATFORM_ISLINUX)add_definitions(-Wno-invalid-source-encoding)# add_definitions(-O2)set(UNIX_OS true)set(_DEBUG true)endif(WIN32)# set(EXECUTABLE_OUTPUT_PATH \({PROJECT_SOURCE_DIR}/bin)# 指定源文件的目录,并将名称保存到变量 SET(source_h\){PROJECT_SOURCE_DIR}/src/pysleep.h\({PROJECT_SOURCE_DIR}/src/Mutex.h\){PROJECT_SOURCE_DIR}/src/myThread.h\({PROJECT_SOURCE_DIR}/src/queuedata.h\){PROJECT_SOURCE_DIR}/src/LogDef.h\({PROJECT_SOURCE_DIR}/src/Log.h\){PROJECT_SOURCE_DIR}/src/DiskSpace.h\({PROJECT_SOURCE_DIR}/src/spaceMgr.h)SET(source_cpp#\){PROJECT_SOURCE_DIR}/src/Mutex.cpp\({PROJECT_SOURCE_DIR}/src/myThread.cpp\){PROJECT_SOURCE_DIR}/src/Log.cpp\({PROJECT_SOURCE_DIR}/src/DiskSpace.cpp\){PROJECT_SOURCE_DIR}/src/spaceMgr.cpp\({PROJECT_SOURCE_DIR}/test/main.cpp)#头文件目录 include_directories(\){PROJECT_SOURCE_DIR}/src)if (\({UNIX_OS})add_definitions(-W-fPIC-Wall# -Wall -g-Werror-Wshadow-Wformat-Wpointer-arith-D_REENTRANT-D_USE_FAST_MACRO-Wno-long-long-Wuninitialized-D_POSIX_PTHREAD_SEMANTICS-DACL_PREPARE_COMPILE-Wno-unused-parameter-fexceptions)set(CMAKE_C_FLAGS_DEBUG \){CMAKE_C_FLAGS_DEBUG} -O0)link_directories()

指定生成目标

add_executable(log_test \({source_h} \){source_cpp}) #link target_link_libraries(log_test -lpthread -pthread -lz -lrt -ldl )endif(\({UNIX_OS})if (\){WIN_OS})set(CMAKE_CXX_FLAGS \({CMAKE_CXX_FLAGS} /wd4819)add_definitions(-D_CRT_SECURE_NO_WARNINGS-D_WINSOCK_DEPRECATED_NO_WARNINGS-DNO_WARN_MBCS_MFC_DEPRECATION-DWIN32_LEAN_AND_MEAN )link_directories()if (CMAKE_BUILD_TYPE STREQUAL Debug)set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG \){PROJECT_SOURCE_DIR}/bin)

指定生成目标

add_executable(log_testd \({source_h} \){source_cpp})else(CMAKE_BUILD_TYPE)set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/bin)

指定生成目标

add_executable(log_test \({source_h} \){source_cpp})endif (CMAKE_BUILD_TYPE)endif(${WIN_OS}) compile.txt编译指令指示文件 win: cd log_test mkdir build_win cd build_win cmake -G Visual Studio 10 2010 Win64 -DCMAKE_BUILD_TYPERelease .. msbuild log_test.sln /p:ConfigurationRelease /p:Platformx64cmake -G Visual Studio 10 2010 Win64 -DCMAKE_BUILD_TYPEDebug .. msbuild log_test.sln /p:ConfigurationDebug /p:Platformx64Linux: cd log_test mkdir build_linux cd build_linux cmake .. make 3.3 日志类调用测试案例 测试工程文件main.cpp #include stdio.h #include stdlib.h#include pysleep.h #include Log.hnamespace GlobalVar {extern char disk_DescStr; //磁盘名extern unsigned int disk_space_limit; //剩余空间限制为*MBextern std::string logdir; //后期记录日志目录重配置文件读取extern std::string logname; //可设置为服务名extern std::string log_ext_flag; //日志扩展名 }; //log_test.exe D 2000 log log //log_test.exe D void arg_analysis(int argc, char *argv[]);int main(int argc, char *argv[]) {arg_analysis(argc,argv);int i 0;while(i100){CLogger::createInstance()-Log(LogLevel(rand()%5),log test for [%d],i);pysleep(10);}//while(1){pysleep(10);}return 0; }void arg_analysis(int argc, char *argv[]) {for(int index0; indexargc; index){switch(index){case 0:{#ifdef WIN32std::string appname std::string(argv[0]);GlobalVar::logname appname.substr(0,appname.length()-4);#elseGlobalVar::logname std::string(argv[0]);#endif}break;case 1:{GlobalVar::disk_DescStr argv[1][0];}break;case 2:{GlobalVar::disk_space_limit static_castunsigned int(atoi(argv[2]));}break;case 3:{GlobalVar::logdir std::string(argv[3]);}break;case 4:{GlobalVar::log_ext_flag std::string(argv[4]);}break;default:break;}} }3.4 编译及测试 win: linux: 四、补充源码 部分源码前面已经给出头文件下面给出其他源码文件 pysleep.h #ifndef _PY_SLEEEPH #define _PY_SLEEEPH#ifdef WIN32 #include windows.h #define pysleep(x) Sleep(x) #else #define pysleep(x) usleep(1000x) #endif#endif //_PY_SLEEEPH DiskSpace.cpp #include DiskSpace.h#include time.h #include list #include vector#ifdef WIN32 #include io.h #include direct.h #include Windows.h #else #include sys/statfs.h #include dirent.h /#include sys/vfs.h or sys/statfs.h */ #include sys/vfs.h #include sys/types.h #include sys/stat.h #include unistd.h #include errno.h #include string.h #include stdio.h #endif#include Log.hint pyfree::getDiskFreeSpace(char _DiskStr, int _totalSize, int _freeSize) { #ifdef WIN32BOOL fResult;unsigned _int64 i64FreeBytesToCaller;unsigned _int64 i64TotalBytes;unsigned _int64 i64FreeBytes;char dir[4] { _DiskStr, :, \ };fResult GetDiskFreeSpaceEx(dir,(PULARGE_INTEGER)i64FreeBytesToCaller,(PULARGE_INTEGER)i64TotalBytes,(PULARGE_INTEGER)i64FreeBytes);//GetDiskFreeSpaceEx function,get the disk space status,return BOOL type if (fResult)//jude the disk is in work status by the return value {_totalSize static_castint(static_castfloat(i64TotalBytes) / 1024 / 1024);_freeSize static_castint(static_castfloat(i64FreeBytesToCaller) / 1024 / 1024);//std::cout totalspace: _totalSize MB std::endl;//disk total size //std::cout freespace: _freeSize MB std::endl;//disk free space sze } #endif // WIN32 #ifdef linux//printf(DiskFlag(%c)\n, _DiskStr);struct statfs diskInfo;statfs(/, diskInfo);unsigned long long totalBlocks diskInfo.f_bsize;unsigned long long totalSize totalBlocks * diskInfo.f_blocks;_totalSize static_castint(totalSize 20);//printf(TOTAL_SIZE %d MB\n, _totalSize);unsigned long long freeDisk diskInfo.f_bfree*totalBlocks;_freeSize static_castint(freeDisk 20);//printf(DISK_FREE %d MB\n, _freeSize); #endifreturn 1; }//delete old file which is older taday for the file modify time void pyfree::moveOldFile(std::string dir, const std::string extname, int dayForLimit) {try{ #ifdef WIN32_finddata_t fileInfo;intptr_t hFile;std::string filter _dir;if (filter[filter.size() - 1] ! // || filter[filter.size() - 1] ! \) {filter.push_back(\);}filter *.;filter extname;//time_t filetime time(NULL);time_t filetime (timet)getCurDayZeroClockTime(dayForLimit86400);//gethFile _findfirst(filter.c_str(), fileInfo);if (hFile -1) {return;}std::string delOldFile ;//find the oldest file by modify time do {if (extname.empty())//no ext name, that is dir{if ((fileInfo.attrib _A_SUBDIR)) {if (0 strcmp(fileInfo.name, .) || 0 strcmp(fileInfo.name, ..)){continue;}}}//if(fileInfo.time_write_time)//modify timeif (fileInfo.time_createfiletime)//create time{filetime fileInfo.time_create;delOldFile _dir // (std::string(fileInfo.name));}} while (_findnext(hFile, fileInfo) 0);_findclose(hFile); #endif#ifdef linux//printf(moveOldFile(.%s) 1 from(%s) \n,extname.c_str(),_dir.c_str());std::string curdir _dir;if (curdir[curdir.size() - 1] ! /) {curdir.push_back(/);}DIR *dfd;if ((dfd opendir(curdir.c_str())) NULL){CLogger::createInstance()-Log(eLog_WARNING,open %s error with msg is: %s\n, curdir.c_str(), strerror(errno));return;}struct dirent *dp;//time_t filetime time(NULL);time_t filetime (timet)getCurDayZeroClockTime(dayForLimit * 86400);std::string delOldFile ;while ((dp readdir(dfd)) ! NULL){if (extname.empty())//no ext name, that is dir{if (dp-d_type DT_DIR) {if (0 strcmp(dp-d_name, .) || 0 strcmp(dp-d_name, ..)){continue;}}}else {if (NULL strstr(dp-d_name, extname.c_str())){continue;}}std::string _path _dir /;_path dp-d_name;struct stat el;stat(_path.c_str(), el);if (el.st_mtimefiletime) {filetime el.st_mtime;delOldFile _path;}}if (NULL ! dp) {delete dp;dp NULL;}if (NULL ! dfd) {closedir(dfd);dfd NULL;} #endifif (!delOldFile.empty()){//printf(get old file: %s \n, delOldFile.c_str());int ret remove(delOldFile.c_str());if (0 ! ret) {CLogger::createInstance()-Log(eLog_WARNING,cant remove %s \n, delOldFile.c_str());}else{CLogger::createInstance()-Log(eLog_NOTICE,success remove %s \n, delOldFile.c_str());}}}catch(…){CLogger::createInstance()-Log(eLog_ERROR,moveOldFile(*.%s) from(%s) exception error\n,extname.c_str(),_dir.cstr());} }int pyfree::getCurDayZeroClockTime(int deviation) {int ZeroClockTime 0;time_t curtime time(NULL);struct tm _tt; #ifdef WIN32localtime_s(_tt, curtime); #elselocaltime_r(curtime,_tt); #endif//当日凌晨时刻_tt.tm_hour 0;_tt.tm_min 0;_tt.tmsec 0;ZeroClockTime static_castint(mktime(tt));ZeroClockTime - deviation;//偏移时间return ZeroClockTime_; }Log.cpp #include Log.h #include time.h #include sys/timeb.h#include stdio.h #include stdarg.h #include string#ifdef linux #include string.h #include unistd.h #include stdlib.h#ifndef sprintf_s #define sprintf_s sprintf #endif#else //WIN32#ifndef vsnprintf #define vsnprintf vsnprintf_s #endif #endif#include pysleep.h #include spaceMgr.hnamespace GlobalVar {char disk_DescStr D; //磁盘名unsigned int disk_space_limit 2000; //剩余空间限制为MB,少于该值去删除日志释放空间std::string logdir log; //后期记录日志目录重配置文件读取std::string logname pyfree; //可设置为服务名std::string log_ext_flag log;//日志扩展名 };CLogger CLogger::m_pLogInstance NULL; CLogger::CLogger() : running(true), fpLog(NULL), cur_log_path(log) {char buf[256] {0};sprintf_s(buf,mkdir %s,GlobalVar::logdir.c_str());system(buf);this-start();disk_space_mgr new DiskSpaceMgr(GlobalVar::disk_DescStr,GlobalVar::disk_space_limit,GlobalVar::logdir,GlobalVar::log_ext_flag);disk_space_mgr-start(); };CLogger::~CLogger() {delete disk_space_mgr;disk_space_mgr NULL;running false;if (NULL ! fpLog){fclose(fpLog);fpLog NULL;} };CLogger* CLogger::createInstance( void ) {if (m_pLogInstance NULL){m_pLogInstance new CLogger();return m_pLogInstance;}elsereturn m_pLogInstance; };int CLogger::Run() {MyLogStruct log_item;while (running) {if(logs_cache.pop(log_item)){WriteLog(log_item.type, log_item.szBuffer);#ifdef _DEBUG#ifndef WIN32printf(Log::[%d]–%s\n, getpid(), log_item.szBuffer);#elseprintf(Log::–%s\n, log_item.szBuffer);#endif //WIN32#endif //_DEBUG}pysleep(1);}return 0; };void CLogger::Log(const eLogType type, const char* lpszFormat, …) {va_list args;MyLogStruct log_item;log_item.type static_castint(type);va_start(args, lpszFormat);vsnprintf(log_item.szBuffer, sizeof(log_item.szBuffer), lpszFormat, args);va_end(args); logs_cache.add(logitem); }void CLogger::WriteLog( const int iMsgType, const char * strMsg) {try {//system time to stringstd::string strTime strTime();//file namestd::string strPath createFileName(strTime.substr(0,10)); // log time and type descstd::string strTimeAndTypeDesc createLogTimeAndTypeDesc(iMsgType, strTime);WriteLogToFile(strPath,strTimeAndTypeDesc,strMsg);}catch (…) {printf(write log[%d]{%s}error\n, iMsgType, strMsg);} }void CLogger::WriteLogToFile( std::string strPath,std::string strTime_,const char * strMsg) {if(cur_log_path!strPath||NULL fpLog){if (NULL ! fpLog){fclose(fpLog);fpLog NULL;} //open#ifdef WIN32fopen_s(fpLog, strPath.c_str(), a);#elsefpLog fopen(strPath.c_str(), a);#endif}if (NULL ! fpLog){fseek(fpLog, 0, SEEKEND);fwrite(strTime.cstr(), strTime.length(), 1, fpLog);fwrite(strMsg, strlen(strMsg), 1, fpLog);fwrite(\n, 1, 1, fpLog);} }std::string CLogger::createFileName(std::string strTime) {//file namestd::string strPath GlobalVar::logdir; #ifdef WIN32strPath \; #elsestrPath /; #endifstrPath strTime;strPath _;strPath GlobalVar::logname;strPath .log;return strPath; }std::string CLogger::strTime() {time_t tt;struct timeb tm0;struct tm tm1;char buf[64];ftime(tm0);tt tm0.time; #ifdef WIN32localtime_s(tm1, tt); #elselocaltime_r(tt, tm1); #endifsprintf_s(buf, %04d-%02d-%02d %02d:%02d:%02d.%03d , tm1.tm_year 1900, tm1.tm_mon 1, tm1.tm_mday, tm1.tm_hour, tm1.tm_min, tm1.tmsec, tm0.millitm);std::string strTime buf;buf[10] \0;return strTime_; } //日志等级枚举值的字符串描述 std::string eLogTypeDesc[6] {[UNKNOWN] ,[eLog_DEBUG] ,[eLog_TRACE] ,[eLog_NOTICE] ,[eLog_WARNING] ,[eLogERROR] };std::string CLogger::createLogTimeAndTypeDesc(const unsigned int iMsgType, std::string strTime) {std::string strTimeAndTypeDesc strTime_;unsigned int msg_type iMsgType;if(msg_type6) msg_type 0; //非枚举值外的均为未知strTimeAndTypeDesc eLogTypeDesc[msg_type];return strTimeAndTypeDesc; };/以下是LogFunction/ /* *在queuedata.cpp中应用,该函数主要用来解决QueueData类嵌套调用CLogger的异常问题 *“嵌套名指定中使用了不完全的类型” */ void reportLogCacheOver(const char *queue_desc,int size) {//每溢出10次报告一次CLogger::createInstance()-Log(eLog_WARNING,add item to queue %s, but the size of QueueData is up to limmit size: %d.\n, queue_desc, size); }Mutex.cpp #include Mutex.h#ifdef WIN32 #include windows.h #endif //#include iostream #include stdio.hPYMutex::PYMutex() { #ifdef _WIN32m_mutex ::CreateMutex(NULL, FALSE, NULL); #elsepthread_mutex_init(m_mutex, NULL); #endif }PYMutex::~PYMutex() { #ifdef _WIN32::CloseHandle(m_mutex); #elsepthread_mutex_destroy(m_mutex); #endif }void PYMutex::Lock() const { #ifdef _WIN32//DWORD d WaitForSingleObject(m_mutex, INFINITE);WaitForSingleObject(m_mutex, INFINITE);/// \todo check d for result #elsepthread_mutex_lock(m_mutex); #endif }bool PYMutex::TryLock() const { #ifdef _WIN32DWORD dwWaitResult WaitForSingleObject(m_mutex, 0); if (dwWaitResult ! WAIT_OBJECT_0 dwWaitResult ! WAIT_TIMEOUT) {printf(thread WARNING: bad result from try-locking mutex\n);}return (dwWaitResult WAIT_OBJECT_0) ? true : false; #elsereturn (0pthread_mutex_trylock(m_mutex))?true:false; #endif };void PYMutex::Unlock() const { #ifdef _WIN32::ReleaseMutex(m_mutex); #elsepthread_mutex_unlock(m_mutex); #endif }myThread.cpp #include myThread.h#ifdef WIN32 #include windows.h #else #include stdio.h #endif MyThread::MyThread() {#ifdef linuxhThread 0;threadStatus THREAD_STATUS_NEW;#endif }MyThread::~MyThread() {#ifdef WIN32WaitForSingleObject(hThread, INFINITE);#elsejoin(10);#endif }int MyThread::Run() {#ifdef WIN32printf(Base Thread\n);#elsewhile(true){printf(thread is running!\n);sleep(100);}#endifreturn 0; }bool MyThread::start() {#ifdef WIN32return NULL!(hThread (HANDLE_THREAD)_beginthread(agent, 0, (void *)this));#elsereturn pthread_create(hThread, NULL, run0, this) 0;#endif }HANDLE_THREAD MyThread::getThreadID() {return hThread; }#ifdef WIN32 void MyThread::agent(void *p) {MyThread *agt (MyThread )p;agt-Run(); }#else void MyThread::run0(void* pVoid) {MyThread* p (MyThread) pVoid;p-run1();return p; }void MyThread::run1() {threadStatus THREAD_STATUS_RUNNING;hThread pthread_self();Run();threadStatus THREAD_STATUS_EXIT;hThread 0;pthread_exit(NULL); }int MyThread::getState() {return threadStatus; }void MyThread::join() {if (hThread 0){pthread_join(hThread, NULL);} }void MyThread::join(unsigned long millisTime) {if (hThread 0){return;}if (millisTime 0){join();}else{unsigned long k 0;while (threadStatus ! THREAD_STATUS_EXIT k millisTime){usleep(100);k;}} } #endifqueuedata.cpp #include queuedata.h#include LogDef.h #include Log.hextern void reportLogCacheOver(const char*,int);template class T unsigned int QueueDataT::QSize 100;template class T QueueDataT::QueueData(std::string desc): queue_desc(desc) {init(); };template class T void QueueDataT::init() {queue_overS 0; };template class T QueueDataT::~QueueData() {}// template class T int QueueDataT::size() {int ret 0;m_Mutex.Lock();ret static_castint(datacache_queue.size());m_Mutex.Unlock();return ret; }template class T bool QueueDataT::isEmpty() {bool ret false;m_Mutex.Lock();ret datacache_queue.empty();m_Mutex.Unlock();return ret; }template class T bool QueueDataT::getFirst(T it) {bool ret false;m_Mutex.Lock();if (!datacache_queue.empty()) {it datacache_queue.front();ret true;}m_Mutex.Unlock();return ret; }template class T bool QueueDataT::removeFirst() {bool ret false;m_Mutex.Lock();if (!datacache_queue.empty()) {datacache_queue.pop_front();ret true;}m_Mutex.Unlock();return ret; }template class T bool QueueDataT::pop(T it) {bool ret false;m_Mutex.Lock();if (!datacache_queue.empty()) {it datacache_queue.front();datacache_queue.pop_front();ret true;}m_Mutex.Unlock();return ret; };template class T bool QueueDataT::getList(std::queueT its,unsigned int sizel) {m_Mutex.Lock();while (!datacache_queue.empty()){its.push(datacache_queue.front());datacache_queue.pop_front();if (its.size() sizel){break;}}m_Mutex.Unlock();return !its.empty(); };template class T void QueueDataT::add(T it) {m_Mutex.Lock();if (datacache_queue.size() QSize) {queue_overS;datacache_queue.pop_front();}datacache_queue.push_back(it);m_Mutex.Unlock();if (queue_overS 10) {//每溢出10次报告一次reportLogCacheOver(queue_desc.c_str(),QSize);queue_overS 0;} }template class T void QueueDataT::add_front(T it) {m_Mutex.Lock();if (datacache_queue.size() QSize) {queue_overS;datacache_queue.pop_front();}datacache_queue.push_front(it);m_Mutex.Unlock();if (queue_overS 10) {reportLogCacheOver(queue_desc.c_str(),QSize);queue_overS 0;} }template class T void QueueDataT::clear() {m_Mutex.Lock();datacache_queue.clear();m_Mutex.Unlock();queueoverS 0; }spaceMgr.cpp #include spaceMgr.h#include pysleep.h #include DiskSpace.hDiskSpaceMgr::DiskSpaceMgr(char disk, int fsl, std::string dir/log/, std::string ext/log/): DiskStr(disk),freeSizeLimit(fsl) {DelInfo del(dir,ext);dirs.pushback(del); }DiskSpaceMgr::~DiskSpaceMgr(void) {}int DiskSpaceMgr::Run() {int i_totalSize 0;int i_freeSize 0;while (true){if (pyfree::getDiskFreeSpace(DiskStr, i_totalSize, i_freeSize) 0){if (freeSizeLimit ifreeSize){for(unsigned int i0; idirs.size(); i){std::string ext dirs.at(i).ext;pyfree::moveOldFile(dirs.at(i).dir, ext);}}}pysleep(10);}return 0; }void DiskSpaceMgr::add(std::string dir,std::string ext) {DelInfo del(dir,ext);dirs.pushback(del); }