程序中如何打印日志?(三) VC++6.0和BCB6.0都不支持变长参数的宏
来源:互联网 发布:网络优化 谢金星 pdf 编辑:程序博客网 时间:2024/06/06 15:38
接着前面的来讲。但是遗憾的是,无论是VC++6.0还是BCB6.0, 都不支持变长参数的宏,所以在这种环境下,无法彻底解决之前的问题(在VS2005中可以)。既然如此,我们只能求其次了,还是利用变成参数的函数来迂回解决吧:
//---------------------------------------------------------------------------#ifndef Unit1H#define Unit1H//---------------------------------------------------------------------------#include <Classes.hpp>#include <Controls.hpp>#include <StdCtrls.hpp>#include <Forms.hpp>#define POS __FUNC__, __LINE__, __FILE__//---------------------------------------------------------------------------class TForm1 : public TForm{__published:// IDE-managed Components TButton *Button1; TButton *Button2; void __fastcall Button2Click(TObject *Sender); void __fastcall Button1Click(TObject *Sender);private:// User declarationspublic:// User declarations __fastcall TForm1(TComponent* Owner);};//---------------------------------------------------------------------------extern PACKAGE TForm1 *Form1;//---------------------------------------------------------------------------#endif
//---------------------------------------------------------------------------#include <vcl.h>#pragma hdrstop#include <wtypes.h>#include <stdio.h>#include <fstream>#include <string>using namespace std;#include "Unit1.h"//---------------------------------------------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;void log(char *function, int line, char *file, char *format, ...){va_list args;va_start(args, format);char buf[1024] = {0};vsprintf(buf, format, args);va_end(args); SYSTEMTIME stCurTime = {0}; GetLocalTime(&stCurTime); char szTime[128] = {0}; sprintf(szTime, "%d-%d-%d %d:%d:%d", stCurTime.wYear, stCurTime.wMonth, stCurTime.wDay, stCurTime.wHour, stCurTime.wMinute, stCurTime.wSecond); char szLocation[2048] = {0}; sprintf(szLocation, "%s ===> Function--->%s, Line: %d, File: %s", buf, function, line, file); char szLog[2048] = {0}; sprintf(szLog, "%s %s ", szTime, szLocation); ofstream outfile("log.txt", ios::app); outfile << szLog << endl;}//---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){ log(POS, "HELLO");}//---------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender){ int a = 1; log(POS, "WORLD%d", a);}//---------------------------------------------------------------------------
最后说一句,如果支持变成参数的宏,那该多好啊, 那时,POS都不用写。VS2005就可以,有__VA_ARGS__啊。
睡觉了。
- 程序中如何打印日志?(三) VC++6.0和BCB6.0都不支持变长参数的宏
- BCB6.0中的__FUNC__(vc++6.0中没有此功能)
- 变长参数宏:gcc 和 vc 环境下
- vsnprintf 打印变长参数
- 程序中如何打印日志?(一)
- 程序中如何打印日志?(二)
- 如何在宏定义中使用变长参数
- 变长参数的三点..., 打印log为什么容易core dump?
- VC的控制台程序,如何获取输入参数和环境变量
- 变长参数的宏定义
- 如何定义变长参数个数的函数
- 如何在程序中写log日志(定义log级别:error,warn, info, debug; 宏定义打印不同级别的日志; 程序中引用宏定义即可)
- VC中打印程序的分析
- Java中变长参数的小例子
- python中变长参数的用法举例?
- 如何在VC++6.0中运行C语言中的变长数组?
- 程序那些事:日志记录的作用和方法----如何在程序中记录日志
- VC中编写打印程序
- Virtual Box下配置Host-Only联网方式详解
- Tabhost的简单使用
- [OpenGL ES 08]Per-Pixel Light及卡通效果
- C语言 - 移动的小方块
- 数据结构之图用邻接矩阵实现赋值有向图
- 程序中如何打印日志?(三) VC++6.0和BCB6.0都不支持变长参数的宏
- Linux 内核堆栈打印函数
- 正则表达式实例学习-TCL2
- dota改高清分辨率mark
- Ubuntu 12.04增加右键命令:在终端中打开
- ffdshow 源代码分析 6: 对解码器的dll的封装(libavcodec)
- python中的参数传递顺序
- 链-反链-Dilworth定理 (转载)
- [回忆录]CNC-DIY系列二:步进电机