git clone https://github.com/google/glog.git/ .
or:
svn co https://github.com/google/glog.git/ .
windows: download cmake (www.cmake.org) generate cmake .sln file(glog.sln).
linux: cmake . && make && make install
simple testcase:
#if defined(WIN32)||defined(WINDOWS)
#error("Not supported")
#include <windows.h>
#include <io.h>
#include <direct.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
#include <string.h>
#define GOOGLE_GLOG_DLL_DECL
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include "glog/logging.h"
bool dirExists(const std::string& dirName_in)
{
return access(dirName_in.c_str(), F_OK) == 0;
}
#include <string>
#include <iostream>
void init_glog(const char* pname, const std::string& logDir)
{
google::InitGoogleLogging(pname);
FLAGS_colorlogtostderr = true;
FLAGS_logbufsecs = 0;
FLAGS_max_log_size = 1024 * 1024;
FLAGS_stop_logging_if_full_disk = true;
bool result = dirExists(logDir);
if (!result)
{
::mkdir(logDir.c_str(), 0755);
}
char buffer[256] = {0};
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s/info.log", logDir.c_str());
google::SetLogDestination(google::GLOG_INFO, buffer);
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s/warning.log", logDir.c_str());
google::SetLogDestination(google::GLOG_WARNING, buffer);
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s/error.log", logDir.c_str());
google::SetLogDestination(google::GLOG_ERROR, buffer);
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s/fatal.log", logDir.c_str());
google::SetLogDestination(google::GLOG_FATAL, buffer);
}
int main(int argc, const char** argv)
{
init_glog(argv[0], "./logs");
char str[20] = "hello glog!";
LOG(INFO) << str;
std::string cStr = "hello google!";
LOG(INFO) << cStr;
LOG(INFO) << "info test" << "hello log!";
LOG(WARNING) << "warning test";
LOG(ERROR) << "error test";
std::string strInput;
std::cin >> strInput;
while (strInput != "E" && strInput != "e")
{
LOG(ERROR) << strInput;
std::cin >> strInput;
}
google::ShutdownGoogleLogging();
return 0;
}
windows:
Configuration Properties->C/C++->Additional Include Directories: E:\study\glog\glog_src\trunk\src\windows
Configuration Properties->Linker->Additional Library Directories: E:\study\glog\glog_build\Debug\
Configuration Properties->Linker->Additional Dependences:glog.lib;%(AdditionalDependencies)
make sure Configuration Properties->C/C++->Code Generation->Runtime Library is the same as the library named glog in glog.sln(MTD/MT or MDD/MD)
linux编译:
g++ -g main.cpp -lglog
优化:
按天分日志文件以及不同级别不同文件: https://www.cppfans.org/1566.html