Write a daemon for Windows (1 ).

Source: Internet
Author: User

Write a daemon for Windows (1 ).
Write a daemon on Windows (1 ).

Recently, due to work requirements, we need to write a daemon process, which should be started when the daemon is suspended. This function is relatively simple to say, but I wrote a lot of code that is looking back and getting worse a while ago, so I just want to write it easily this time. After writing it, you can find it. So we will summarize the results.

I. General requirements

1. function-start the daemon again after it is suspended by the daemon.

2. You can configure the process to be dazed.

Ii. Overall consideration

1. In order to avoid repeated wheel creation, and some wheels may not be made by themselves, go to the boost Library

2. To gain high permissions and enable automatic start upon startup, write it as a Windows Service

3. The configuration file is stored in xml and parsed using the rapidxml library.

4. In order to make it more universal, I added a function: to execute cyclic tasks. Cyclic tasks are divided into two types: one is a time point task, which is to execute the task every few minutes, and the other is a time interval task, which is to execute the task every few minutes.

Iii. First Topic: Log Module

In other words, grain and grass are not active, and soldiers and horses are the first. The Log Module is the same as the grain and grass, so the Log Module is the first to be written.

However, some people may ask if the logging module has many wheels. Isn't it difficult to duplicate the wheels? Of course there is a reason for this.

1. The Log Module I have seen provides only a few string Log interfaces. In practice, we often use GetLastError to obtain error codes on Windows. I used to write

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

I am in trouble when I write more. Can I simplify it?

2. Sometimes I want to write binary data into a log file, whether readable or not. However, there is no log interface for directly recording byte data.

3. anyone familiar with Windows Programming knows that APIs can be divided into A/W because the character (string) parameters are wide and narrow, and some logstores do not consider this at all, only interfaces of the char version are provided. If I want to input the wchar_t string, I have to re-compile it myself. This is too much trouble to write.

Based on the above points, I decided to create a comfortable wheel on my own.

The following are the Declaration of the log class:

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);};

Pay attention to its interface and ignore other parts. Here, the overload method is used to avoid the caller's distinction between char and wchar_t.

Iv. Conclusion

At, I decided to say that this time we had so many satursday, 1.1.

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.