Write a daemon on Windows (1) starts with

Source: Internet
Author: User

Write a daemon on Windows (1) starts with

Recently, due to the need for work, to write a daemon, the main thing is to be in the process of hanging up the daemon to start again. This function is relatively simple to say, but I wrote a lot of things that look bad now, so I want to write a little more pleasing to the eye. After writing to find, eh, also can yo. So we summarize.

A General requirements

1. Function--Start it again when the daemon is dead

2. Configurable processes that need to be guarded

Two Overall consideration

1. In order to avoid repeating the wheel, and some wheels may not be able to build themselves, on the boost library

2. To be able to get higher privileges and be able to boot automatically, write it as a Windows service

3. configuration files using XML storage, using Rapidxml library parsing

4. To make it more versatile, I added another feature: Perform cycle tasks. Cycle tasks are divided into two, one is a point-in-time task, that is, every few minutes to perform the task, the other is a time interval task, that is, every few minutes to perform the task

Three First topic: Log module

The proverb says, the fodder is not moved, the soldiers first. I think the log module is like a grain of fodder, so I started to write the log module first.

However, some people will ask the log module such a lot of wheels, not a good deal not to repeat the wheel-making? There's a reason for that, of course.

1. The log module I have seen is nothing more than providing several string log interfaces, in practice knowing that, on windows, we often use GetLastError to get error codes, which I used to write

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

Write too much, feel the trouble, can you simplify it?

2. Sometimes I want to write a binary data into a log file, whether it is unreadable or not. But there is no log interface that directly logs byte data

3. Familiar with the Windows programming students know that the API has A/W, that is because the character (string) parameters have a narrow point, there are some log library is not considered this, only provide a char version of the interface, I would like to pass the wchar_t string, I have to turn again, It's a lot of trouble to write.

Based on the above, I decided to build my own wheels and build a comfortable wheel.

The following is a declaration of the log class:

classCloggerimpl: PublicSingleton<cloggerimpl>{friendclassSingleton<cloggerimpl>;Private: Cloggerimpl (); Public:    ~Cloggerimpl (); Public:    //if dir is empty, use EXE dir//When log file increases over max_size, we'll create new log file//if Max_size is 0, use * 1024x768 * 1024x768 (10MB)    BOOLInitConstSTD::string& dir,ConstUnsignedLongmax_size); BOOLInitConststd::wstring& dir,ConstUnsignedLongmax_size); BOOLLog_string (ConstLog_level level,Const Char* File,Const intLineConstSTD::string&s); BOOLLog_string (ConstLog_level level,Const Char* File,Const intLineConststd::wstring&ws); BOOLLog_bytes (ConstLog_level level,Const Char* File,Const intLineConst void*buf,ConstUnsignedLongLenConstSTD::string&prefix); BOOLLog_bytes (ConstLog_level level,Const Char* File,Const intLineConst void*buf,ConstUnsignedLongLenConststd::wstring&Wprefix); BOOLLog_last_error (ConstLog_level level,Const Char* File,Const intLine, clasterrorformat& E,ConstSTD::string&prefix); BOOLLog_last_error (ConstLog_level level,Const Char* File,Const intLine, clasterrorformat& E,Conststd::wstring&wprefix);};

Please pay attention to its interface first, ignoring the other parts. This is an overloaded way to eliminate the caller's distinction between char and wchar_t.

Four Conclusion

I decided to say at 1.1, this time it's so much.

Source: Https://github.com/mkdym/DaemonSvc.git && https://git.oschina.net/mkdym/DaemonSvc.git.

Saturday, October 24, 2015

Write a daemon on Windows (1) starts with

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.