您的位置:首页 > 其它

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

2015-10-24 19:20 337 查看

写一个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的区分。

四.结束语

我决定一点一点的说,这次就这么多



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

2015年10月24日星期六
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: