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

972 篇文章 329 订阅
33 篇文章 2 订阅

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

//---------------------------------------------------------------------------

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

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值