C++builder 写入日志功能,调试用!
String ExePath;//路径ExePath=ExtractFilePath(Application->ExeName);//当前目录
ExePath=ExtractFilePath(ExcludeTrailingPathDelimiter(GetCurrentDir()));//EXE程序的上一级目录地址
WriteLog();//使用
.h
void __fastcall WriteLog(String AMsg);
.cpp
//-----------------在可执行程序目录下写入日志文件-------------------------------
void __fastcall WriteLog(String AMsg) {
static String LogFileName = ExePath+"log\\WebUpdata.log";
static String LogFile = ExePath+"log";
if (!DirectoryExists(LogFile))
{
CreateDir(LogFile);//文件夹不存在则创建
}
TFileStream *Log;
try {
if (!FileExists(LogFileName)) {
Log = new TFileStream(LogFileName, fmCreate);
}
else {
Log = new TFileStream(LogFileName, fmOpenWrite);
}
}
catch (...) {
return;
}
Log->Seek(0, soFromEnd);
AMsg = AMsg+"---->"+Now()+"\r\n";
Log->Write(AMsg.c_str(), AMsg.Length());
delete Log;
}
摘自 破空的专栏
补充:软件开发 , C++ ,