学习Gtkmm系列之七

调试的时候总是使用std:cout比较麻烦,干脆写个log头文件,方便自己调试程序。注意一下,LOG_FUNCTION_SCOPE可以打印出函数运行时间,你只需要在函数入口处使用该宏就可以了。

/*
 * log.h
 *
 * Copyright (C) 2012 - Lei.Cao
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * $Id$
 */
#ifndef UTILS_LOG_H
#define UTILS_LOG_H

#include <cassert>
#include <cstdlib>
#include <glibmm/timer.h>
#include <glibmm/ustring.h>

#ifndef HERE
#define HERE __ASSERT_FUNCTION << ":" <<__FILE__<< ":" << __LINE__ << ": "
#endif

#ifndef PRETY_FUNCTION_NAME_
#define PRETTY_FUNCTION_NAME_ __ASSERT_FUNCTION                                                         
#endif

#ifndef LOG_STREAM
#define LOG_STREAM  std::cout
#define LOG_ENDLINE std::endl
#endif

#ifdef DEBUG
#define LOG(msg)  LOG_STREAM <<"  |I|" << HERE << msg << LOG_ENDLINE
#define LOGD(msg) LOG_STREAM <<"  |D|" << HERE << msg << LOG_ENDLINE
#define LOGE(msg) LOG_STREAM <<"  |E|" << HERE << msg << LOG_ENDLINE
class ScopeLogger {
 public:
  ScopeLogger(const char*scope_name)
  : name(scope_name) {
    LOG_STREAM <<"|{|" << name << ":" << LOG_ENDLINE;
    timer.start();
  }
  virtual ~ScopeLogger(){
    timer.stop();
    LOG_STREAM <<"|}|" << name << " elapsed:"<< timer.elapsed ()*1000 << "(ms)" << LOG_ENDLINE;
  }
 private:
  Glib::Timer timer;
  Glib::ustring name;
};

#define LOG_FUNCTION_SCOPE ScopeLogger scope_logger(PRETTY_FUNCTION_NAME_)
#else
#define LOG(msg)
#define LOGD(msg)
#define LOGE(msg)
#define LOG_FUNCTION_SCOPE
#endif /* #ifdef DEBUG */


#endif /* #ifndef UTILS_LOG_H */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值