一个API方式存取日志文件的模块
2010-10-18 15:42
489 查看
'**************************************
' 模块名称: AppendToLog
' 功能描述:一个很不错的日志文件写入模块,不同于
' open/print/close写文件方法,这个模块使用API
' 存取文件,这样保证文件能正确的存取,及时被
' 存取的文件正被其他用户打开。这个模块是最安全
' 有效的文件写入方法,用于日志文件的创建,当然
' 也可以用于其他文件存取。
' : 枕善居收藏整理
'**************************************
'API 声明
Const GENERIC_WRITE = &H40000000
Const FILE_SHARE_READ = &H1
Const Create_NEW = 1
Const OPEN_EXISTING = 3
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_BEGIN = 0
Const INVALID_HANDLE_VALUE = -1
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'**************************************
' 模块名称: AppendToLog
' 功能描述:一个很不错的日志文件写入模块,不同于
' open/print/close写文件方法,这个模块使用API
' 存取文件,这样保证文件能正确的存取,及时被
' 存取的文件正被其他用户打开。这个模块是最安全
' 有效的文件写入方法,用于日志文件的创建,当然
' 也可以用于其他文件存取。
' : 枕善居收藏整理
'
' 输入参数:lpFileName As String - 要写入的日志文件名称
' 返 回 值:True 成功, False 失败
'**************************************
Private Function AppendToLog(ByVal lpFileName As String, ByVal sMessage As String) As Boolean
'appends a string to a text file. it's u
' p to the coder to add a CR/LF at the end
'
'of the string if (s)he so desires.
'assume failure
AppendToLog = False
'exit if the string cannot be written to
' disk
If Len(sMessage) < 1 Then Exit Function
'get the size of the file (if it exists)
'
Dim fLen As Long
fLen = 0
If (Len(Dir(lpFileName))) Then
fLen = FileLen(lpFileName)
End If
'open the log file, create as necessary
Dim hLogFile As Long
hLogFile = CreateFile(lpFileName, GENERIC_WRITE, FILE_SHARE_READ, ByVal 0&, _
IIf(Len(Dir(lpFileName)), OPEN_EXISTING, Create_NEW), _
FILE_ATTRIBUTE_NORMAL, 0&)
'ensure the log file was opened properly
'
If (hLogFile = INVALID_HANDLE_VALUE) Then Exit Function
'move file pointer to end of file if fil
' e was not created
If (fLen <> 0) Then
If (SetFilePointer(hLogFile, fLen, ByVal 0&, FILE_BEGIN) = &HFFFFFFFF) Then
'exit sub if the pointer did not set cor
' rectly
CloseHandle (hLogFile)
Exit Function
End If
End If
'convert the source string to a byte arr
' ay for use with WriteFile
Dim lTemp As Long
ReDim TempArray(0 To Len(sMessage) - 1) As Byte
For lTemp = 1 To Len(sMessage)
TempArray(lTemp - 1) = Asc(Mid$(sMessage, lTemp, 1))
Next
'write the string to the log file
If (WriteFile(hLogFile, TempArray(0), Len(sMessage), lTemp, ByVal 0&) <> 0) Then
'the data was written correctly
AppendToLog = True
End If
'flush buffers and close the file
FlushFileBuffers (hLogFile)
CloseHandle (hLogFile)
End Function
' 模块名称: AppendToLog
' 功能描述:一个很不错的日志文件写入模块,不同于
' open/print/close写文件方法,这个模块使用API
' 存取文件,这样保证文件能正确的存取,及时被
' 存取的文件正被其他用户打开。这个模块是最安全
' 有效的文件写入方法,用于日志文件的创建,当然
' 也可以用于其他文件存取。
' : 枕善居收藏整理
'**************************************
'API 声明
Const GENERIC_WRITE = &H40000000
Const FILE_SHARE_READ = &H1
Const Create_NEW = 1
Const OPEN_EXISTING = 3
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_BEGIN = 0
Const INVALID_HANDLE_VALUE = -1
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'**************************************
' 模块名称: AppendToLog
' 功能描述:一个很不错的日志文件写入模块,不同于
' open/print/close写文件方法,这个模块使用API
' 存取文件,这样保证文件能正确的存取,及时被
' 存取的文件正被其他用户打开。这个模块是最安全
' 有效的文件写入方法,用于日志文件的创建,当然
' 也可以用于其他文件存取。
' : 枕善居收藏整理
'
' 输入参数:lpFileName As String - 要写入的日志文件名称
' 返 回 值:True 成功, False 失败
'**************************************
Private Function AppendToLog(ByVal lpFileName As String, ByVal sMessage As String) As Boolean
'appends a string to a text file. it's u
' p to the coder to add a CR/LF at the end
'
'of the string if (s)he so desires.
'assume failure
AppendToLog = False
'exit if the string cannot be written to
' disk
If Len(sMessage) < 1 Then Exit Function
'get the size of the file (if it exists)
'
Dim fLen As Long
fLen = 0
If (Len(Dir(lpFileName))) Then
fLen = FileLen(lpFileName)
End If
'open the log file, create as necessary
Dim hLogFile As Long
hLogFile = CreateFile(lpFileName, GENERIC_WRITE, FILE_SHARE_READ, ByVal 0&, _
IIf(Len(Dir(lpFileName)), OPEN_EXISTING, Create_NEW), _
FILE_ATTRIBUTE_NORMAL, 0&)
'ensure the log file was opened properly
'
If (hLogFile = INVALID_HANDLE_VALUE) Then Exit Function
'move file pointer to end of file if fil
' e was not created
If (fLen <> 0) Then
If (SetFilePointer(hLogFile, fLen, ByVal 0&, FILE_BEGIN) = &HFFFFFFFF) Then
'exit sub if the pointer did not set cor
' rectly
CloseHandle (hLogFile)
Exit Function
End If
End If
'convert the source string to a byte arr
' ay for use with WriteFile
Dim lTemp As Long
ReDim TempArray(0 To Len(sMessage) - 1) As Byte
For lTemp = 1 To Len(sMessage)
TempArray(lTemp - 1) = Asc(Mid$(sMessage, lTemp, 1))
Next
'write the string to the log file
If (WriteFile(hLogFile, TempArray(0), Len(sMessage), lTemp, ByVal 0&) <> 0) Then
'the data was written correctly
AppendToLog = True
End If
'flush buffers and close the file
FlushFileBuffers (hLogFile)
CloseHandle (hLogFile)
End Function
相关文章推荐
- python logging 模块之TimedRotatingFileHandler 实现每天一个日志文件
- HDFS的API调用,创建Maven工程,创建一个非Maven工程,HDFS客户端操作数据代码示例,文件方式操作和流式操作
- 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- FlumeNG1.3.1在Source Exec tail 文件方面的一个缺陷以及改进方式
- Linux下重要日志文件及查看方式
- Apache不记录指定文件类型日志---模块
- 利用HTML5+的Uploader模块API实现基于MUI的文件上传
- 多个*.c文件编译生成一个模块驱动*.ko的makefile文件编写
- C++第六周任务5:解决用一个项目多个文件的方式实现,其中两个类的声明放在一个.h文件中,每个类的成员函数分别放一个文件,main()函数用一个文件。体会这样安排的优点。
- 下载一个指定文件,下载成功后修改权限,自动重试60次,带详细日志记录。
- 求一个论坛的在线人数,假设有一个论坛,其注册ID有两亿个,每个ID从登陆到退出会向一个日志文件中记下登陆时间和退出时间,要求写一个算法统计一天中论坛的用户在线分布,取样粒度为秒。
- RDIFramework.NET — 基于.NET的快速信息化系统开发框架- 5.4平台日志、异常管理、生成自动升级配置文件模块
- 使用read(),write(),seekg(),seekp()实现二进制方式文件随机存取
- log4j中配置日志文件几种方式
- java锁住一个文件(独占方式访问)
- SPORE,一个很好的实现JS命名空间的管理,模块的定义,与自动加载依赖模块的脚本文件
- 03_dbcp数据源依赖jar包,DBCP中API介绍,不同过dbcp方式使用dbcp数据库连接池,通过配置文件使用dbcp数据库连接池
- CHM帮助文件怎么启动时自动跳转?(API方式)
- 一个奇特的脚本输出日志方式-----记录时间方式
- 拥有归档日志 如何恢复一个丢失的数据文件