程序中如何打印日志?(二)
来源:互联网 发布:招银网络科技 知乎 编辑:程序博客网 时间: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);的时候就会出错,那怎么办呢?相信一切都是有出路的,我们下次见。
- 程序中如何打印日志?(二)
- 程序中如何打印日志?(一)
- 如何在程序中写log日志(定义log级别:error,warn, info, debug; 宏定义打印不同级别的日志; 程序中引用宏定义即可)
- 程序打印日志
- 程序中如何打印日志?(三) VC++6.0和BCB6.0都不支持变长参数的宏
- python如何打印日志
- 如何打印错误日志
- 如何打印日志
- 闲谈程序中如何打印log
- 闲谈程序中如何打印log
- Linux中调试程序使用打印日志纠错的技巧
- Linux中调试程序使用打印日志纠错
- 关于程序中日志打印和异常抛出
- 在日志中如何打印文件、行号、方法名
- 如何在Objective-C中打印日志记录
- 如何将异常打印到日志文件中
- JNI中打印日志
- java中打印日志
- Java中的线程(三)-Java的锁机制
- struts2线程安全
- Android Framework------之Input子系统
- matlab conv2 和filter2的区别
- ucd-snmp
- 程序中如何打印日志?(二)
- poj1734 Musical Theme
- Ubuntu 12.10 中增加Eclipse图标到启动器
- linux profile执行及顺序
- SQL高级教程--笔记
- 协程在游戏服务器开发中的应用
- DB210.1版本如何导入Excel、txt格式的数据
- 黑马程序员_____开发前奏
- android input system(frameworks) analysis -- InputManager