LogFile.h头文件
1
// LogFile.h: interface for the CLogFile class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
5
#if !defined(AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8_F1078E1F6A6B__INCLUDED_)
6
#define AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8_F1078E1F6A6B__INCLUDED_
7
8
#if _MSC_VER > 1000
9
#pragma once
10
#endif // _MSC_VER > 1000
11
12
class CLogFile
13
{
14
public:
15
CLogFile();
16
virtual ~CLogFile();
17
static CString GetFileName();
18
static CString GetFilePath();
19
static BOOL WriteLog(CString LogText);
20
};
21
22
#endif // !defined(AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8_F1078E1F6A6B__INCLUDED_)
23
LogFile.cpp文件
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

1
// LogFile.cpp: implementation of the CLogFile class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
5
#include "stdafx.h"
6
#include "KJ83NUpper.h"
7
#include "LogFile.h"
8
9
#ifdef _DEBUG
10
#undef THIS_FILE
11
static char THIS_FILE[]=__FILE__;
12
#define new DEBUG_NEW
13
#endif
14
15
//////////////////////////////////////////////////////////////////////
16
// Construction/Destruction
17
//////////////////////////////////////////////////////////////////////
18
19
CLogFile::CLogFile()
20
{
21
22
}
23
24
CLogFile::~CLogFile()
25
{
26
27
}
28
//获取文件名称
29
CString CLogFile::GetFileName()
30
{
31
CString m_sFileName;
32
33
m_sFileName = CTime::GetCurrentTime().Format("%Y-%m-%d") + ".log";
34
35
return m_sFileName;
36
}
37
//获取应用程序所在路径
38
CString CLogFile::GetFilePath()
39
{
40
CString m_FilePath;
41
42
GetModuleFileName(NULL,m_FilePath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
43
44
m_FilePath.ReleaseBuffer();
45
46
int m_iPosIndex;
47
48
m_iPosIndex = m_FilePath.ReverseFind('\\');
49
50
m_FilePath = m_FilePath.Left(m_iPosIndex) + "\\Log";
51
52
return m_FilePath;
53
}
54
55
BOOL CLogFile::WriteLog(CString LogText)
56
{
57
try
58
{
59
CFile m_File;
60
CStdioFile m_SFile;
61
CFileFind m_FileFind;
62
CString m_sErrorMessage;
63
CString m_sFileName = GetFileName();
64
CString m_sFilePath = GetFilePath();
65
CString m_sCurrentTime = CTime::GetCurrentTime().Format("%Y-%m-%d %X");
66
67
m_sErrorMessage = "*******************" + m_sCurrentTime + "*******************" + "\r";
68
m_sErrorMessage += LogText + "\r";
69
m_sErrorMessage += "*******************" + m_sCurrentTime + "*******************" + "\r";
70
71
if(!m_FileFind.FindFile(m_sFilePath))
72
{
73
CreateDirectory(m_sFilePath,NULL);
74
}
75
76
if(!m_SFile.Open(m_sFilePath + "\\" +m_sFileName,CFile::modeReadWrite))
77
{
78
m_SFile.Open(m_sFilePath + "\\" + m_sFileName,CFile::modeCreate | CFile::modeReadWrite | CFile::typeText);
79
}
80
81
m_SFile.SeekToEnd();
82
83
char* m_szMessage;
84
85
m_szMessage=(LPTSTR)(LPCTSTR)m_sErrorMessage;
86
87
m_SFile.Write(m_szMessage,lstrlen(m_szMessage));
88
89
m_SFile.Close();
90
}
91
catch(CFileException fileException)
92
{
93
return false;
94
}
95
96
return true;
97
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

调用方法说明:
首先在要调用的页面加入引用
#include "LogFile.h"
然后,写入下列代码可以正常调用
CLogFile::WriteLog("IO错误,文件打开失败!");
点此下载源码