首页 技术 正文
技术 2022年11月17日
0 收藏 793 点赞 2,674 浏览 1970 个字

写一个Windows上的守护进程(1)开篇

最近由于工作需要,要写一个守护进程,主要就是要在被守护进程挂了的时候再把它启起来。说起来这个功能是比较简单的,但是我前一阵子写了好多现在回头看起来比较糟糕的代码,所以就想这次写的顺眼一点。写完后发现,诶,还可以哟。于是就总结总结。

一.大致需求

1. 功能——当被守护进程挂掉后再次启动它

2. 可配置需要守护的进程

二.通盘考虑

1. 为了避免重复造轮子,况且有的轮子可能自己也造不出来,上boost库

2. 为了能够获得较高的权限和能够开机自动启动,将其编写为一个Windows服务

3. 配置文件使用xml存储,使用rapidxml库解析

4. 为了使其更具有通用性,我又加了个功能:执行周期任务。周期任务又分了两种,一种是时间点任务,就是每到几点几分执行任务,另一种是时间间隔任务,就是每隔多少分钟执行任务

三.第一个话题:日志模块

俗话讲,粮草未动,兵马先行。窃以为日志模块就像粮草一样,所以最先开始写日志模块。

然而有人会问日志模块这种轮子多得很,不是说好不重复造轮子的吗?这当然是有原因的。

1. 我所见过的日志模块无非就提供几种字符串Log接口,实践而知,在Windows上,我们经常会用到GetLastError去获取错误码,以前我总是写

ErrorLog("xxx fail, error code: %lu", GetLastError());

写得多了,就觉得麻烦了,能不能简化一下?

2. 有的时候我就想往日志文件里头记一段二进制数据,不管可不可读。但是没有直接记录字节数据的日志接口

3. 熟悉Windows编程的同学都知道,API有A/W之分,那是因为字符(串)参数有宽窄之分,有一些日志库根本就没有考虑这个,只提供char版本的接口,我要是想传入wchar_t的字符串,还得自己再转一下,这写多了也麻烦啊

基于以上几点,我决定自行造轮子,要造一个舒舒服服的轮子。

以下是日志类的声明:

class CLoggerImpl : public Singleton<CLoggerImpl>
{
friend class Singleton<CLoggerImpl>;private:
CLoggerImpl();public:
~CLoggerImpl();public:
//if dir is empty, use exe dir
//when log file increases over max_size, we will create new log file
//if max_size is 0, use 10 * 1024 * 1024 (10MB)
bool init(const std::string& dir, const unsigned long max_size);
bool init(const std::wstring& dir, const unsigned long max_size); bool log_string(const LOG_LEVEL level, const char* file, const int line, const std::string& s);
bool log_string(const LOG_LEVEL level, const char* file, const int line, const std::wstring& ws); bool log_bytes(const LOG_LEVEL level, const char* file, const int line, const void *buf, const unsigned long len, const std::string& prefix);
bool log_bytes(const LOG_LEVEL level, const char* file, const int line, const void *buf, const unsigned long len, const std::wstring& wprefix); bool log_last_error(const LOG_LEVEL level, const char* file, const int line, CLastErrorFormat& e, const std::string& prefix);
bool log_last_error(const LOG_LEVEL level, const char* file, const int line, CLastErrorFormat& e, const std::wstring& wprefix);
};

请先关注其接口,忽略其他部分。这里用了重载的方式免去调用者对char和wchar_t的区分。

四.结束语

我决定一点一点的说,这次就这么多写一个Windows上的守护进程(1)开篇

源码:https://github.com/mkdym/DaemonSvc.git && https://git.oschina.net/mkdym/DaemonSvc.git

2015年10月24日星期六

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849