Log.cpp
上传用户:pumpssky
上传日期:2007-12-07
资源大小:110k
文件大小:1k
源码类别:

MacOS编程

开发平台:

C/C++

  1. // Log.cpp: implementation of the CLog class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "gmark.h"
  6. #include "Log.h"
  7. #include "stdio.h"
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CLog::CLog()
  17. {
  18. fp = NULL;
  19. }
  20. CLog::~CLog()
  21. {
  22. }
  23. void CLog::Open(LPCSTR filename)
  24. {
  25. fp = fopen( filename, "wtc");
  26. InitializeCriticalSection( &m_CriticalSection );
  27. }
  28. void CLog::Write(LPCSTR message)
  29. {
  30. EnterCriticalSection(&m_CriticalSection);
  31. SYSTEMTIME systime;
  32. GetLocalTime(&systime);
  33. DWORD dwTickCount = GetTickCount();
  34. if( fp && message )
  35. {
  36. fprintf( fp, "%d-%d-%d %d-%d-%d-%d ",
  37. systime.wYear, systime.wMonth, systime.wDay, 
  38. systime.wHour, systime.wMinute, systime.wSecond, dwTickCount/*systime.wMilliseconds*/ );
  39. fwrite( message, sizeof(char), strlen(message), fp );
  40. fputs("n", fp);
  41. fflush(fp);
  42. }
  43. LeaveCriticalSection(&m_CriticalSection);
  44. }
  45. void CLog::Close()
  46. {
  47. if(fp)
  48. {
  49. fclose(fp);
  50. fp = NULL;
  51. }
  52. DeleteCriticalSection( &m_CriticalSection );
  53. }