C++11 is easy to implement thread-safe multi-parameter log syntax

Source: Internet
Author: User

Just like

MyLogger ("who is? ") " " I ' m ZRH " " I ' m in thread: " , std::this_thread::get_id ()); MyLogger (1"+"2u " = " " 3 ");

Multiple thread output shapes such as:

2016-04-16 10:54:54 who is? I ' M ZRH i ' m in thread:6736
2016-04-16 10:54:54 1 + 2 = 3
2016-04-16 10:54:54 who is? I ' M ZRH i ' m in thread:13528
2016-04-16 10:54:54 1 + 2 = 3
2016-04-16 10:54:54 who is? I ' M ZRH i ' m in thread:3036
2016-04-16 10:54:54 1 + 2 = 3

Use the

-Parentheses operator overloading

-C++11 Chrono, Iomanip gets and formats the output timestamp;

-C++11 Lock_guard to ensure that a log record is thread-safe;

-c++11 variadic template, so that you can do arbitrary parameter number and parameter type (of course, to ensure that the parameters can be printed, such as the << operator implementation);

-c++11 list initialization, braced-init-list, list initialization in the form of sequential print log, which is much better than recursive implementation;

C++11 is easy to implement thread-safe multi-parameter log syntax

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.