前面博文中的打印日志方法太复杂,能不能简单一点呢?我能!
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include
#include
#include
#include
using namespace std;
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
void logFunc(char *function, int line, char *file, char *msg)
{
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", msg, function, line, file);
char buf[2500] = {0};
sprintf(buf, "%s %s", szTime, szLocation);
ofstream outfile("log.txt", ios::app);
outfile << buf << endl;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
logFunc(__FUNC__, __LINE__, __FILE__, "HELLO");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
logFunc(__FUNC__, __LINE__, __FILE__, "WORLD");
}
//---------------------------------------------------------------------------
上述程序逻辑和结果正确,但有个问题,两个函数中都需要写__FUNC__, __FILE__, __LINE__这些东西,你想啊,在整个工程中有多少函数啊,所以,要改:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include
#include
#include
#include
using namespace std;
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
void logFunc(char *msg)
{
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", msg, __FUNC__, __LINE__, __FILE__);
char buf[2500] = {0};
sprintf(buf, "%s %s", szTime, szLocation);
ofstream outfile("log.txt", ios::app);
outfile << buf << endl;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
logFunc("HELLO");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
logFunc("WORLD");
}
//---------------------------------------------------------------------------
上述程序逻辑上有问题,打印的不是所想要的信息,而是:
2013-11-9 11:9:44 HELLO ===> Function--->logFunc, Line: 26, File: C:\Documents and Settings\Administrator\hLآ\bcbTest\Unit1.cpp
2013-11-9 11:9:44 WORLD ===> Function--->logFunc, Line: 26, File: C:\Documents and Settings\Administrator\hLآ\bcbTest\Unit1.cpp
那怎么办呢?人类的智慧是无穷的,你要知道。考虑利用宏,如下:
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include
#define log(msg) logFunc(__FUNC__, __LINE__, __FILE__, msg)
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include
#include
#include
#include
using namespace std;
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
void logFunc(char *function, int line, char *file, char *msg)
{
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", msg, function, line, file);
char buf[2500] = {0};
sprintf(buf, "%s %s", szTime, szLocation);
ofstream outfile("log.txt", ios::app);
outfile << buf << endl;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
log("HELLO");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
log("WORLD");
}
//---------------------------------------------------------------------------
这样就对了。但是,还存在一个问题,log宏不支持变参,当你用log("num is %d", num);的时候就会出错,那怎么办呢?相信一切都是有出路的,我们下次见。