It took two hours to get it done, which is really efficient. A bunch of blogs, all of them not cool.

Preparations: VS2017, cmake, glog

The cmake installation file can be found on the official website. The mSI file downloaded by individuals is installed directly. By default, the cmake installation file is not added to the environment variables, but is changed to add to the environment variables for all users. After the local installation is complete, run CMD to check: cmake –version.

Glog is downloaded from Github.

Open CMD in the glog folder and run the following commands

    cmake .
    cmake --build .
Copy the code

After compilation, it will automatically generate the local VS version of the corresponding lib library: glog\Debug\glogd.lib, win32 Debug library is generated by default.

Copy the glog/SRC/Windows/glog folder to the new project and add the macro definition:

Copy the code

Then it can be used normally.

#include "pch.h"
#include <iostream>
#include "glog/logging.h"
int main(int argc, char* argv[])
    system("mkdir Log && cd Log && mkdir info warning error fatal");
    google::SetLogDestination(google::GLOG_WARNING, "Log/warning/");
    google::SetLogDestination(google::GLOG_ERROR, "Log/error/");
    google::SetLogDestination(google::GLOG_FATAL, "Log/fatal/");
    LOG(INFO) << "info 005";
    LOG(WARNING) << "warning 005";
    LOG(ERROR) << "error 005";
    //LOG(FATAL) << "fatal 005";
    return 0;
Copy the code

If you want to generate x64 debug or release libraries, you can modify the cmake configuration file in glog to generate lib libraries for other platforms. , can also be generated using the cmake GUI tool.

Note: Optional Toolset to use enter: host=x64. But I didn’t write it, I also generated the row x64 bit); Click Configure, then click Gernerate, will generate SLN solution, open SLN compile the project, and then copy the generated 64 bit lib and lib/ DLL to use.

If you check the arrow, debug and release will both generate lib+ DLL. If not checked, only lib is generated.

When you use logs of a lower level, the logs of a higher level are recorded. To change logs, seeA blogThen re-cmake to generate a new lib library for use.

In addition, glog personally did not find the formatting output Settings, refer to another blog post, I directly added two functions to use project logging.h.

Add the following code to logging.h:

template<typename T> std::string pack_string(const T &value) { std::stringstream ss; ss << value; return ss.str(); } template<typename T, typename... Args> std::string pack_string(const T &value, Args... args) { std::stringstream ss; ss << value << pack_string(args...) ; return ss.str(); } #define ADDTAG LOG(INFO) << pack_string("Enter ", __FUNCTION__); #define ADDLEAVE LOG(INFO) << pack_string("Leave ", __FUNCTION__);Copy the code

Settings: engineering projects at the same time — – > properties – > c/c + + – > voice – > conform to the model modified into no.