C++的日志宏

来源:互联网 发布:文件夹加密软件绿色版 编辑:程序博客网 时间:2024/06/06 18:41

C/C++的宏是很强大的,比如我们可以实现一个日志宏来做调试,以前在VS2008下写控制台的程序时很喜欢用printf或者cout输出调试信息,但是要是写的程序不是控制台的程序呢?我们就可以用日志宏了,我们利用日志宏将调试信息输出到文件,程序运行完后就可以打开文件查看调试信息了大笑。下面是我的一个例子



//common.h

#ifndef COMMON_H_#define COMMON_H_#define DEBUG(Message) fileLog<<__FILE__<<":"<<__LINE__<<":"<<Message<<std::endl;#include <fstream>extern std::ofstream fileLog;#endif

//common.cpp

#include "stdafx.h"#include "common.h"std::ofstream fileLog;

//test.cpp

#include "stdafx.h"#include "common.h"void f1();void close();void f2();int _tmain(int argc, _TCHAR* argv[]){fileLog.open("logs.log");DEBUG("open");f1();f2();close();return 0;}void f1(){DEBUG("Enter....f1");DEBUG("Exit....f1");}void f2(){DEBUG("Enter....f2");DEBUG("Enter....f2");}void close(){fileLog.close();


0 0
原创粉丝点击