程序中如何打印日志?(二)

来源:互联网 发布:招银网络科技 知乎 编辑:程序博客网 时间:2024/06/11 16:22

       前面博文中的打印日志方法太复杂,能不能简单一点呢?我能!

//---------------------------------------------------------------------------#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 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 <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 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 <Classes.hpp>#include <Controls.hpp>#include <StdCtrls.hpp>#include <Forms.hpp>#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 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 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);的时候就会出错,那怎么办呢?相信一切都是有出路的,我们下次见。

 

原创粉丝点击