转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600649.html

需求:Log()函数,能够自动根据时间记录日志信息,要求不定参数类型和参数个数。

第一步,得到日志时间:

  get_data() 和get_time()分别得到当前日期和时间。

 #include <ctime>
 static std::string get_data()
 {
     time_t t = time();
     struct tm *now = localtime(&t);
     std::stringstream ss;
     ss<<now->tm_year+<<'_'
         <<now->tm_mon+<<"_"
         <<now->tm_mday;
     return ss.str();
 }

 static std::string get_time()
 {
     time_t t = time();
     struct tm* now = localtime(&t);
     std::stringstream ss;
     ss<<get_data()<<' ';
     ss<<now->tm_hour<<':'
         <<now->tm_min<<':'
         <<now->tm_sec;
     return ss.str();
 }

2.to_string()

  C++中,我们使用可变参数模板实现不定参数。

  首先实现to_string()参数将Log()中的参数全部传递至streamstring。to_string()通过递归的方式实现。

#include <string>
#include <sstream>

template <typename T>
static int to_string(std::stringstream& ss,const T &t)
{
    ss<<t;
    ;
}

template <typename T,typename...Args>
static int to_string(std::stringstream& ss,const T &t,const Args&...rest)
{
    ss<<t;
    return to_string(ss,rest...);
}

3.Log()

最后Log()调用上面三个函数,get_data()得到当天的时间找到对应的日志文件,to_string()将日志内容和get_time()时间结合后输入到日志文件中。

template <typename T,typename...Args>
static int Log(const T &t,const Args&...rest)
{
    std::stringstream ss;
    to_string(ss,t,rest...);
    std::string path = LOG_PATH;
    path +=get_data();        //日志名字为当天时间
    std::fstream log_file;
    log_file.open(path,std::ios::out|std::ios::app);
    log_file<<"["<<get_time()<<"]"<<ss.str()<<std::endl;
    log_file.close();
    std::cerr<<ss.str()<<std::endl;
}

C++实现Log()日志函数的更多相关文章

  1. LR中日志设置和日志函数

    LR中日志参数的设置与使用 1.Run-Time Setting日志参数的设置 在loadrunner的vuser菜单下的Run-Time Setting的General的LOG选项中可以对在执行脚本 ...

  2. winfrom存储txt日志函数

    参考微信支付SDK的代码,抽取出来的winform存储记事本日志函数: #region 存储日志 public string path = Application.StartupPath + &quo ...

  3. log4j.properties配置与将异常输出到Log日志文件实例

    将异常输出到 log日志文件 实际项目中的使用: <dependencies> <dependency> <groupId>org.slf4j</groupI ...

  4. C/C++log日志库比较

    事实上,在C的世界里面没有特别好的日志函数库(就像Java里面的的log4j,或者C++的log4cxx).C程序员都喜欢用自己的轮子.printf就是个挺好的轮子,但没办法通过配置改变日志的格式或者 ...

  5. 使用mapreduce来分析网站的log日志

    近日,有人和我说分析log日志. 之前,就写过,但是忘了总结了,找了半天也没有找到,看了以后要将东西整理了. 无奈,在网上收拾,看到这个人写的,索性,就搬过来,待我找到我写的,在一块补充一下! 所有网 ...

  6. Python + logging 输出到屏幕,将log日志写入文件

    日志 日志是跟踪软件运行时所发生的事件的一种方法.软件开发者在代码中调用日志函数,表明发生了特定的事件.事件由描述性消息描述,该描述性消息可以可选地包含可变数据(即,对于事件的每次出现都潜在地不同的数 ...

  7. Log 日志工具类 保存到文件 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. php日志函数error_log

    php内置打印log日志的函数,这个对php程序调试非常高效 1.配置 编辑php.ini文件 log_errors = On 设置log日志存储路径 error_log = /wwwroot/php ...

  9. (转)DB2 db2diag.log 日志分析

    DB2 db2diag.log 日志分析 原文:http://blog.csdn.net/lyjiau/article/details/52129997 db2diag.log是用来记录DB2数据库运 ...

随机推荐

  1. BZOJ 1015 [JSOI2008]星球大战starwar

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 3551  Solved: 1581[Submit ...

  2. mklink命令转移win7系统盘文件夹users和programdata(附xp的方法)

    mklink命令转移win7系统盘文件夹users和programdata(附xp的方法)                   使用mklink命令转移文件夹清理臃肿的c盘        (一) 我的 ...

  3. word页面不对齐,如何解决?

    http://blog.163.com/haibianfeng_yr/blog/static/34572620201157105439516/

  4. HDOJ(HDU) 1985 Conversions(汇率转换)

    Problem Description Conversion between the metric and English measurement systems is relatively simp ...

  5. Java 程序员必须掌握的 Linux 命令

    作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令.因为很多服务器上都是Linux系统.所以,要和服 ...

  6. L - 还是畅通工程 - hdu 1233

    Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达 ...

  7. SQL - 复制数据库中的一行

    insert into MyTable(field1, field2, id_backup) select field1, field2, uniqueId from MyTable where un ...

  8. Android ExpandableListView BaseExpandableListAdapter (类似QQ分组列表)

    分组列表视图(ExpandableListView) 和ListView不同的是它是一个两级的滚动列表视图,每一个组可以展开,显示一些子项,类似于QQ列表,这些项目来至于ExpandableListA ...

  9. [AngularJS] ngPluralize

    ngPluralize is a directive that displays messages according to en-US localization rules. <script& ...

  10. 查看linux版本号的几种方法

    (1)lsb_release 命令查看,FSG(Free Standards Group)组织开发的LSB (Linux Standard Base)标准的一个命令,用来查看linux兼容性的发行版信 ...