Google-glog open source library analysis (4): glog macro skills

Source: Internet
Author: User

In addition to the core structure, Google-glog also provides unified and concise interfaces for use through macro techniques.
At the same time, the namespace should be used to minimize name conflicts and provide a brief logstore.

Macro Assistant
    • Provides a unified and concise log output interface through macros
      • Simple use of log output interfaces such as log (Info) and log (error)
    • Rich log output extensions through macros
      • Provides helper macros such as if and check to simplify code.
Programming Skills
    • Namespace usage
      • Because it is used as a library, namespace classes are used in glog to avoid name conflicts.
      • The glog library interface space is provided through the Google namespace, and the internal implementation is further encapsulated into the nested namespace
    • Macro skills: shunting
      • The debug macro can be used to locate the log output to the logmessage or the nullstream with nothing to output.
    • Macro tips: Hide
      • The core of log output is implemented through logmessage and other classes, and rich interfaces such as conditional logs and check logs are provided through macros.
    • Macro tips: clean
      • When using macros, the # UNDEF operation will be performed after use to ensure that the macro of the library will not interfere with the Program of using the library.
    • Macro skills: assertions
      • In mutex. H, # define mutexlock (x)... is defined to ensure that the synchronization lock will not encounter errors such as mutexlock * Lock = new mutexlock (& mu ).
    • Macro skills: skills
      • 1. Use # error, there is... in the # If condition to directly output the macro of the error message. The error prompt is displayed during compilation.
      • 2. Use do {...} while (0) in macro definition to ensure macro content as a whole and avoid macro expansion problems:
        • For example, when # define call () A (); B () is called if (1 <0) Call (), it is extended to if (1 <0) (); B () and B () will always be called
Summary

The above is just a summary of my reference to online materials and reading Source Code. It has not been applied in actual projects.
Reading the source code is also a pleasure.

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.