使用__FILE__得到当前源文件的文件名&使用__LINE__得到当前所在行数
来源:互联网 发布:广场舞 知乎 编辑:程序博客网 时间:2024/06/02 07:28
想用C++自己实现一个轻量级的日志文件类,记录运行时的错误信息、时间信息、错误发生的源代码文件名、错误发生所在行数。
实现后两个功能时遇到了些困难,后来在msdn上查到标准C++提供了一组宏,可以完美的解决这些问题。下面摘自MSDN:
-----------------------------------------------------------------------------------------------------------------------------------------------------
__FILE__ The name of the current source file. __FILE__ expands to a string surrounded by double quotation marks. To ensure that the full path to the file is displayed, use /FC (Full Path of Source Code File in Diagnostics).
You can create your own wide string version of __FILE__ as follows:
#include <stdio.h>
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
wchar_t *pwsz = __WFILE__;
-----------------------------------------------------------------------------------------------------------------------------------------------------
__LINE__ The line number in the current source file. The line number is a decimal integer constant. It can be altered with a#line directive.
-----------------------------------------------------------------------------------------------------------------------------------------------------
__FILE__得到的是单字符的字符串,当在Unicode中使用时,如果直接用__FILE__,会出现乱码,因此需要用上面的宏处理下得到__WFILE__,这个宏将返回双字节
__LINE__返回一个数
e.g.
在Unicode中
首先
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
然后
CString str;
#ifdef _UNICODE
str.Format(_T("%s line %d"), __WFILE__, __LINE__ );
#else
str.Format(_T("%s line %d"), __FILE__, __LINE__ );
#endif
MessageBox(str);
- 使用__FILE__得到当前源文件的文件名&使用__LINE__得到当前所在行数
- 使用__FILE__得到当前文件位置
- C语言程序获取或者打印当前语句所在的行号、当前源文件的文件名、程序编译的日期等信息(__LINE__、__FUNCTION__、__FILE__、__DATE__、__TIME__)
- 切换标签状态,得到当前事件所在的行数
- 得到当前日期所在的年+周
- 使用脚本得到当前的时间函数
- ios 得到当前使用wifi的名称
- 输当前程序执行点所在的文件名和行数
- 关键字: C/C++,预定义宏,__FILE__, __LINE__, __DATE__,__TIME__,__func__,__FUNCTION__ __FILE__:当前源代码文件名的字符串文
- __FILE__和__LINE__的使用
- 如何得到当前所在的sql的数据库名称
- JAVA怎样得到当前程序所在文件夹的路径
- ASP得到当前文件所在目录
- 得到当前的输入法
- 使用Sys(0)得到当前工作的主机名和用户名
- java Swing得到当前系统所允许使用的字体.
- 如何使用javascript得到TreeView当前选中的节点【收集】
- ViewPager 如何得到当前的Fragment (使用FragmentPagerAdapter)
- 获取CPU制造商----读注册表
- UNetbootin 制作U盘启动盘
- 切换jdk版本
- RBAC权限体系设计UML
- DES加密与解密算法
- 使用__FILE__得到当前源文件的文件名&使用__LINE__得到当前所在行数
- NBA: Kobe Bryant, Michael Jordan and the Greatest Pick-Up Game Ever
- [译]OOSE第7章:Analysis 分析 7.3 The analysis model 分析模型 & 7.4 Summary
- ASP.net 4.0嵌入Ckeditor 3.5 报错A potentially dangerous Request.Form value was detected from the client 的解决办法
- webDAV服务的开启以及客户端的上传、下载、删除、新建文件夾、列表的代码(C#)
- 为什么要选择UniDAC
- jQuery 插件模板
- 批量执行linux文件夹下的各个shell文件
- WINCE6.0 远程工具,不能使用ActiveSync连接的问题