log_file.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:27k
源码类别:

打印编程

开发平台:

Visual C++

  1. #include "log_file.h"
  2. static TCHAR logFileName[ MAX_PATH + 1 ] =  _T("");
  3. static unsigned int show_level = 0;
  4. int __cdecl create_log(LPCTSTR ID, unsigned int level )
  5. {
  6. HANDLE hFile = NULL;
  7. DWORD writedBytes =  (level>3)?3:level;
  8. GetSystemDirectory(logFileName, sizeof(logFileName)/sizeof(TCHAR)-1);
  9. _stprintf_s(logFileName + 3, sizeof(logFileName)/sizeof(TCHAR)-4,  _T("%s.log"), ID );
  10. hFile = CreateFile(logFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  11. if( INVALID_HANDLE_VALUE == hFile )
  12. hFile = CreateFile(logFileName,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  13. else
  14. {
  15. CloseHandle(hFile);
  16. hFile = INVALID_HANDLE_VALUE;
  17. }
  18. if( INVALID_HANDLE_VALUE != hFile )
  19. {
  20. _stprintf_s(logFileName, sizeof(logFileName)/sizeof(TCHAR)-1,  _T("%drn"),writedBytes);
  21. WriteFile(hFile, logFileName, (DWORD)_tcslen(logFileName),&writedBytes,NULL);
  22. CloseHandle(hFile);
  23. }
  24. // 钺眢