灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3159回复:0

[C++技术]C++builder 写入日志功能,调试用!

楼主#
更多 发布于:2012-09-06 11:33

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;
}  

喜欢0 评分0
游客

返回顶部