1.日志基类
.h文件
01 | //--------------------------------------------------------------------------- |
02 | #ifndef UnitLogWriterH |
03 | #define UnitLogWriterH |
04 | #include <vcl.h> |
05 | #include <time.h> |
06 | #include <assert.h> |
07 | //--------------------------------------------------------------------------- |
08 | class LogFile |
09 | { |
10 | protected : |
11 | CRITICAL_SECTION _csLock; |
12 | char * _szFileName; |
13 | HANDLE _hFile; |
14 | bool OpenFile(); //打开文件, 指针到文件尾 |
15 | DWORD Write( LPCVOID lpBuffer, DWORD dwLength); |
16 | virtual void WriteLog( LPCVOID lpBuffer, DWORD dwLength); //写日志, 可以扩展修改 |
17 | void Lock() { ::EnterCriticalSection(&_csLock); } |
18 | void Unlock() { ::LeaveCriticalSection(&_csLock); } |
19 | public : |
20 | LogFile( const char *szFileName = "Log.log" ); //设定日志文件名 |
21 | virtual ~LogFile(); |
22 | const char * GetFileName() |
23 | { |
24 | return _szFileName; |
25 | } |
26 | void SetFileName( const char *szName); //修改文件名, 同时关闭上一个日志文件 |
27 | bool IsOpen() |
28 | { |
29 | return _hFile != INVALID_HANDLE_VALUE; |
30 | } |
31 | void Close(); |
32 | void Log( LPCVOID lpBuffer, DWORD dwLength); //追加日志内容 |
33 | void Log( const char *szText) |
34 | { |
35 | Log(szText, strlen (szText)); |
36 | } |
37 | private : //屏蔽函数 |
38 | LogFile( const LogFile&); |
39 | LogFile&operator = ( const LogFile&); |
40 | }; |
41 | #endif |
基类cpp文件
001 | //--------------------------------------------------------------------------- |
002 | #pragma hdrstop |
003 | #include "UnitLogWriter.h" |
004 | //--------------------------------------------------------------------------- |
005 | #pragma package(smart_init) |
006 | LogFile::LogFile( const char *szFileName) |
007 | { |
008 | _szFileName = NULL; |
009 | _hFile = INVALID_HANDLE_VALUE; |
010 | ::InitializeCriticalSection(&_csLock); |
011 | SetFileName(szFileName); |
012 | } |
013 | //------------------------------------------------------------------------- |
014 | LogFile::~LogFile() |
015 | { |
016 | ::DeleteCriticalSection(&_csLock); |
017 | Close(); |
018 | if (_szFileName) |
019 | delete []_szFileName; |
020 | } |
021 | //------------------------------------------------------------------------- |
022 | bool LogFile::OpenFile() |
023 | { |
024 | if (IsOpen()) |
025 | return true ; |
026 | if (!_szFileName) |
027 | return false ; |
028 | _hFile = CreateFile( |
029 | _szFileName, |
030 | GENERIC_WRITE, |
031 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
032 | NULL, |
033 | OPEN_EXISTING, |
034 | FILE_ATTRIBUTE_NORMAL, |
035 | NULL); |
036 | if (!IsOpen() && GetLastError() == 2) //打开不成功, 且因为文件不存在, 创建文件 |
037 | _hFile = CreateFile( |
038 | _szFileName, |
039 | GENERIC_WRITE, |
040 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
041 | NULL, |
042 | OPEN_ALWAYS, |
043 | FILE_ATTRIBUTE_NORMAL, |
044 | NULL); |
045 | if (IsOpen()) |
046 | SetFilePointer(_hFile, 0, NULL, FILE_END); |
047 | return IsOpen(); |
048 | } |
049 | //------------------------------------------------------------------------- |
050 | DWORD LogFile::Write( LPCVOID lpBuffer, DWORD dwLength) |
051 | { |
052 | DWORD dwWriteLength = 0; |
053 | if (IsOpen()) |
054 | WriteFile(_hFile, lpBuffer, dwLength, &dwWriteLength, NULL); |
055 | return dwWriteLength; |
056 | } |
057 | //------------------------------------------------------------------------- |
058 | void LogFile::WriteLog( LPCVOID lpBuffer, DWORD dwLength) |
059 | { |
060 | time_t now; |
061 | char temp[21]; |
062 | DWORD dwWriteLength; |
063 | if (IsOpen()) |
064 | { |
065 | time (&now); |
066 | strftime (temp, 20, "%Y-%m-%d %H:%M:%S" , localtime (&now)); |
067 | WriteFile(_hFile, "\xd\xa#-----------------------------" , 32, &dwWriteLength, NULL); |
068 | WriteFile(_hFile, temp, 19, &dwWriteLength, NULL); |
069 | WriteFile(_hFile, "-----------------------------#\xd\xa" , 32, &dwWriteLength, NULL); |
070 | WriteFile(_hFile, lpBuffer, dwLength, &dwWriteLength, NULL); |
071 | WriteFile(_hFile, "\xd\xa" , 2, &dwWriteLength, NULL); |
072 | FlushFileBuffers(_hFile); |
073 | } |
074 | } |
075 | //------------------------------------------------------------------------- |
076 | //------------------------------------------------------------------------- |
077 | void LogFile::SetFileName( const char *szName) |
078 | { |
079 | assert (szName); |
080 | if (_szFileName) |
081 | delete []_szFileName; |
082 | Close(); |
083 | _szFileName = new char [ strlen (szName) + 1]; |
084 | assert (_szFileName); |
085 | strcpy (_szFileName, szName); |
086 | } |
087 | //------------------------------------------------------------------------- |
088 | void LogFile::Close() |
089 | { |
090 | if (IsOpen()) |
091 | { |
092 | CloseHandle(_hFile); |
093 | _hFile = INVALID_HANDLE_VALUE; |
094 | } |
095 | } |
096 | //------------------------------------------------------------------------- |
097 | void LogFile::Log( LPCVOID lpBuffer, DWORD dwLength) |
098 | { |
099 | assert (lpBuffer); |
100 | __try |
101 | { |
102 | Lock(); |
103 | if (!OpenFile()) |
104 | return ; |
105 | WriteLog(lpBuffer, dwLength); |
106 | } |
107 | __finally |
108 | { |
109 | Unlock(); |
110 | } |
111 | } |
1 | |
2.日志派生类
.h文件
01 | //--------------------------------------------------------------------------- |
02 | #ifndef LogFileExH |
03 | #define LogFileExH |
04 | #include <assert.h> |
05 | #include "UnitLogWriter.h" |
06 | //--------------------------------------------------------------------------- |
07 | class LogFileEx : public LogFile |
08 | { |
09 | protected : |
10 | char *_szPath; |
11 | char _szLastDate[9]; |
12 | int _iType; |
13 | void SetPath( const char *szPath); |
14 | public : |
15 | enum LOG_TYPE{YEAR = 0, MONTH = 1, DAY = 2}; |
16 | LogFileEx( const char *szPath = "." , LOG_TYPE iType = MONTH); |
17 | ~LogFileEx(); |
18 | const char * GetPath(); |
19 | void Log( LPCVOID lpBuffer, DWORD dwLength); |
20 | void Log( const char *szText); |
21 | void Log( const AnsiString&szText); |
22 | private : //屏蔽函数 |
23 | LogFileEx( const LogFileEx&); |
24 | LogFileEx&operator = ( const LogFileEx&); |
25 | }; |
26 | #endif |
1 | |
1 | cpp文件 |
1 | |
01 | //--------------------------------------------------------------------------- |
02 | #pragma hdrstop |
03 | #include "LogFileEx.h" |
04 | //--------------------------------------------------------------------------- |
05 | #pragma package(smart_init) |
06 | //------------------------------------------------------------------------- |
07 | void LogFileEx::SetPath( const char *szPath) |
08 | { |
09 | assert (szPath); |
10 | WIN32_FIND_DATA wfd; |
11 | char temp[MAX_PATH + 1] = {0}; |
12 | if (FindFirstFile(szPath, &wfd) == INVALID_HANDLE_VALUE && CreateDirectory(szPath, NULL) == 0) |
13 | { |
14 | strcat ( strcpy (temp, szPath), " Create Fail. Exit Now! Error ID :" ); |
15 | ltoa(GetLastError(), temp + strlen (temp), 10); |
16 | MessageBox(NULL, temp, "Class LogFileEx" , MB_OK); |
17 | exit (1); |
18 | } |
19 | else |
20 | { |
21 | GetFullPathName(szPath, MAX_PATH, temp, NULL); |
22 | _szPath = new char [ strlen (temp) + 1]; |
23 | assert (_szPath); |
24 | strcpy (_szPath, temp); |
25 | } |
26 | } |
27 | //------------------------------------------------------------------------- |
28 | LogFileEx::LogFileEx( const char *szPath , LOG_TYPE iType) |
29 | { |
30 | _szPath = NULL; |
31 | SetPath(szPath); |
32 | _iType = iType; |
33 | memset (_szLastDate, 0, 9); |
34 | } |
35 | //------------------------------------------------------------------------- |
36 | LogFileEx::~LogFileEx() |
37 | { |
38 | if (_szPath) |
39 | delete []_szPath; |
40 | } |
41 | //------------------------------------------------------------------------- |
42 | const char * LogFileEx::GetPath() |
43 | { |
44 | return _szPath; |
45 | } |
46 | //------------------------------------------------------------------------- |
47 | void LogFileEx::Log( LPCVOID lpBuffer, DWORD dwLength) |
48 | { |
49 | assert (lpBuffer); |
50 | char temp[10]; |
51 | static const char format[3][10] = { "%Y" , "%Y-%m" , "%Y%m%d" }; |
52 | __try |
53 | { |
54 | Lock(); |
55 | time_t now = time (NULL); |
56 | strftime (temp, 9, format[_iType], localtime (&now)); |
57 | if ( strcmp (_szLastDate, temp) != 0) //更换文件名 |
58 | { |
59 | strcat ( strcpy (_szFileName, _szPath), "\\" ); |
60 | strcat ( strcat (_szFileName, temp), ".log" ); |
61 | strcpy (_szLastDate, temp); |
62 | Close(); |
63 | } |
64 | if (!OpenFile()) |
65 | return ; |
66 | WriteLog(lpBuffer, dwLength); |
67 | } |
68 | __finally |
69 | { |
70 | Unlock(); |
71 | } |
72 | } |
73 | //------------------------------------------------------------------------- |
74 | void LogFileEx::Log( const char *szText) |
75 | { |
76 | Log(szText, strlen (szText)); |
77 | } |
78 | //------------------------------------------------------------------------- |
79 | void LogFileEx::Log( const AnsiString&szText) |
80 | { |
81 | Log(szText.c_str(),szText.Length()); |
82 | } |
3.随便测试的代码
01 | //--------------------------------------------------------------------------- |
02 | #include <vcl.h> |
03 | #include <conio.h> |
04 | #include "LogFileEx.h" |
05 | #pragma hdrstop |
06 | //--------------------------------------------------------------------------- |
07 | #pragma argsused |
08 | int main( int argc, char * argv[]) |
09 | { |
10 | LogFileEx log ; |
11 | log .Log( "哈哈" ); |
12 | AnsiString temp= "adsfsadfsadfsaf" ; |
13 | log .Log(temp); |
14 | log .Log(temp); |
15 | getch(); |
16 | return 0; |
17 | } |
18 | //--------------------------------------------------------------------------- |
1 | |