标签:
断点调试和打印日志各有优点,下面我们来简要说说如何在程序中打印日志,在BCB6.0中实现。
-
- #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;
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
-
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- 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[1024] = {0};
- sprintf(szLocation, "Function--->%s, Line: %d, File: %s", __FUNC__, __LINE__, __FILE__);
-
- char buf[2048] = {0};
- sprintf(buf, "%s %s", szTime, szLocation);
- ofstream outfile("log.txt", ios::app);
- outfile << buf << endl;
- }
结果,log.txt中为:
2013-11-9 10:42:57 Function--->TForm1::Button1Click, Line: 32, File: C:\Documents and Settings\Administrator\hL?\bcbTest\Unit1.cpp
2013-11-9 10:42:59 Function--->TForm1::Button1Click, Line: 32, File: C:\Documents and Settings\Administrator\hL?\bcbTest\Unit1.cpp
2013-11-9 10:43:0 Function--->TForm1::Button1Click, Line: 32, File: C:\Documents and Settings\Administrator\hL?\bcbTest\Unit1.cpp
实际上,上述只是打印了基本的信息,后续博文中会介绍如何打印其它需要打印的信息。
前面博文中的打印日志方法太复杂,能不能简单一点呢?我能!
-
- #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:
- TButton *Button1;
- TButton *Button2;
- void __fastcall Button2Click(TObject *Sender);
- void __fastcall Button1Click(TObject *Sender);
- private:
- public:
- __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.本文为转载文章,仅分享学习。
程序中打日志
标签:
原文地址:http://www.cnblogs.com/knight-fz/p/5816539.html