base11. Logger日志类和Impl日志实现类

muduo库通过这几个宏的日志输出,既可以输出到标准输出,又可以输出到宏
__FILE__是这一行代码所在的文件名,__LINE__是这一行代码所在的行号

#define LOG_TRACE if (muduo::Logger::logLevel() <= muduo::Logger::TRACE) \
  //构造匿名对象,在调用完Logger.stream()之后,会自动调用析构用函数
  // // 匿名对象只存在于该行代码,离开这行代码后立即调用析构函数
  muduo::Logger(__FILE__, __LINE__, muduo::Logger::TRACE, __func__).stream()
#define LOG_DEBUG if (muduo::Logger::logLevel() <= muduo::Logger::DEBUG) \
  muduo::Logger(__FILE__, __LINE__, muduo::Logger::DEBUG, __func__).stream()
#define LOG_INFO if (muduo::Logger::logLevel() <= muduo::Logger::INFO) \
  muduo::Logger(__FILE__, __LINE__).stream()
#define LOG_WARN muduo::Logger(__FILE__, __LINE__, muduo::Logger::WARN).stream()
#define LOG_ERROR muduo::Logger(__FILE__, __LINE__, muduo::Logger::ERROR).stream()
#define LOG_FATAL muduo::Logger(__FILE__, __LINE__, muduo::Logger::FATAL).stream()
#define LOG_SYSERR muduo::Logger(__FILE__, __LINE__, false).stream()
#define LOG_SYSFATAL muduo::Logger(__FILE__, __LINE__, true).stream()

Logger类图在这里插入图片描述
数据成员:

Impl impl_:一个Impl类型的impl_对象,impl_包含日志的实现以及格式化
enum LogLevel:枚举类LogLevel

typedef

typedef void (OutputFunc)(const char msg, int len)
typedef void (*FlushFunc)()

成员函数:

Logger(SourceFile file, int line):构造函数
Logger(SourceFile file, int line, LogLevel level):构造函数
Logger(SourceFile file, int line, LogLevel level, const char* func):构造函数
Logger(SourceFile file, int line, bool toAbort):构造函数
~Logger():析构函数,调用impl_.finish(),是输出的最后一步
LogStream& stream():返回LogStream类型的stream_对象
static LogLevel logLevel():
static void setLogLevel(LogLevel level):设置日志等级
static void setOutput(OutputFunc):用来更改默认输出设备
static void setFlush(FlushFunc):用来更改默认输出设备的冲洗

Impl类图
Impl类包含日志的实现以及格式化
在这里插入图片描述
数据成员:

Timestamp time_:一个Timestamp类型的时间戳time_
LogStream stream_:一个类型为LogStream的stream_对象,用来输出信息到缓冲区FixedBuffer
LogLevel level_:一个LogLevel枚举类型的日志等级level_
int line_:一个line_对象
SourceFile basename_:日志文件的basename_

typedef

typedef Logger::LogLevel LogLevel

成员函数:

Impl(LogLevel level, int old_errno, const SourceFile& file, int line):构造函数
void formatTime():格式化函数
void finish():输出的最后一步

Logging.h

#ifndef MUDUO_BASE_LOGGING_H
#define MUDUO_BASE_LOGGING_H

#include <muduo/base/LogStream.h>
#include <muduo/base/Timestamp.h>

namespace muduo
{
   
//最外层日志类Logger类,负责日志的级别
class Logger
{
   
 public:
  enum LogLevel
  {
   
    TRACE,
    DEBUG,
    INFO,
    WARN,
    ERROR,
    FATAL,
    NUM_LOG_LEVELS,
  };

  // compile time calculation of basename of source file
  class SourceFile
  {
   
   public:
    template<int N>
    inline SourceFile(const char (&arr)[N])
      : data_(arr),
        size_(N-1)
    {
   
      const char* slash = strrchr(data_, '/'); // builtin function
      if (slash)
      {
   
        data_ = slash + 1;
        size_ -= static_cast<int>(data_ - arr);
      }
    }

    explicit SourceFile(const char* filename)
      : data_(filename)
    {
   
      const char* slash = strrchr(filename, '/');
      if (slash)
      {
   
        data_ = slash + 1;
      }
      size_ = static_cast<int
附件是VS2010的工程,C++日志,谷歌的东西,很好用,也很强大哦! glog简介 Google glog是一个基于程序级记录日志信息的c++库,编程使用方式与c++的stream操作似,例: LOG(INFO) << "Found " << num_cookies << " cookies"; “LOG”宏为日志输出关键字,“INFO”为严重性程度。 主要支持功能: 1, 参数设置,以命令行参数的方式设置标志参数来控制日志记录行为; 2, 严重性分级,根据日志严重性分级记录日志; 3, 可有条件地记录日志信息; 4, 条件中止程序。丰富的条件判定宏,可预设程序终止条件; 5, 异常信号处理。程序异常情况,可自定义异常处理过程; 6, 支持debug功能。可只用于debug模式; 7, 自定义日志信息; 8, 线程安全日志记录方式; 9, 系统级日志记录; 10, google perror风格日志信息; 11, 精简日志字符串信息。 开源代码托管 开源代码地址:https://github.com/google/glog 其实官方开源代码已经有大量demo可以参考了,也提供了VS可以直接打开编译的项目。 如何使用 1:把glog文件夹拷贝到源代码目录 2:在工程设置中添加附加包含目录(glog\include;)和附加库目录(glog\lib;),在附件依赖项中添加对应lib文件,一一对应关系如下: MDd libglog32MDd.lib MD libglog32MD.lib MTd libglog32MTd.lib MT libglog32MT.lib 建议使用MDd和MD方式,带上对应的dll(在glog\bin目录,需要时拷贝到bin文件输出目录)可以避免使用MTd,MT引起的内存泄露是值得的。 #include #include using namespace std; //包含glog头文件,建议放在stdafx.h中 //#define GOOGLE_GLOG_DLL_DECL // 使用静态库的时候用这个,不过我测试静态库有内存泄露,所以不推荐使用静态库 #define GLOG_NO_ABBREVIATED_SEVERITIES #include "glog/logging.h" //获取当前程序的运行目录 string GetAppPathA() { char szExePath[MAX_PATH] = {0}; GetModuleFileNameA(NULL,szExePath,MAX_PATH); char *pstr = strrchr(szExePath,'\\'); memset(pstr+1,0,1); string strAppPath(szExePath); return strAppPath; } void main() { //glog初始化 google::InitGoogleLogging("重签程序"); string strLogPath = GetAppPathA().append("LogInfo\\"); CreateDirectoryA(strLogPath.c_str(),NULL); google::SetLogDestination(google::GLOG_INFO,strLogPath.c_str()); //功能测试 LOG(INFO)<<"log start...";//普通日志 LOG(WARNING)<<"Warning log";//警告日志 LOG(ERROR)<<"Error log";//错误日志 int i = 4; LOG_IF(INFO,i == 4)<<"Log if Test"; //以上就是我常用的几个日志函数了,当然还有很多更加强大的日志相关函数,大家如有有兴趣,可以参照官方给的示例使用, //开源代码地址:https://github.com/google/glog MessageBoxA(NULL,"Test Over",":)",MB_ICONINFORMATION); } 测试程序中,我使用的动态链接库方式。(Debug模式中代码生成为MDd,Release为MD)。lib是截止现在2015-11-04-21:35是最新的。采用VS2010编译,MTd,MT,MDd,MD方式编译在测试项目中都有提供。 博文地址: http://blog.csdn.net/sunflover454/article/details/49643625
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值