88using System . IO ;
99using System . Runtime . InteropServices ;
1010using System . Threading ;
11+ using Microsoft . Extensions . Logging ;
1112using Microsoft . Win32 . SafeHandles ;
1213
1314namespace Tsavorite . core
@@ -40,6 +41,7 @@ public unsafe class LocalStorageDevice : StorageDeviceBase
4041 private static uint sectorSize = 0 ;
4142 private bool _disposed ;
4243 readonly bool readOnly ;
44+ readonly ILogger logger ;
4345
4446 /// <summary>
4547 /// Number of pending reads on device
@@ -65,14 +67,17 @@ public override string ToString()
6567 /// <param name="capacity">The maximum number of bytes this storage device can accommodate, or CAPACITY_UNSPECIFIED if there is no such limit </param>
6668 /// <param name="recoverDevice">Whether to recover device metadata from existing files</param>
6769 /// <param name="useIoCompletionPort">Whether we use IO completion port with polling</param>
70+ /// <param name="readOnly">Indicate if this is a readonly device</param>
71+ /// <param name="logger">ILogger instance</param>
6872 public LocalStorageDevice ( string filename ,
6973 bool preallocateFile = false ,
7074 bool deleteOnClose = false ,
7175 bool disableFileBuffering = true ,
7276 long capacity = Devices . CAPACITY_UNSPECIFIED ,
7377 bool recoverDevice = false ,
7478 bool useIoCompletionPort = false ,
75- bool readOnly = false )
79+ bool readOnly = false ,
80+ ILogger logger = null )
7681 : this ( filename , preallocateFile , deleteOnClose , disableFileBuffering , capacity , recoverDevice , null , useIoCompletionPort , readOnly : readOnly )
7782 {
7883 }
@@ -99,6 +104,8 @@ void _callback(uint errorCode, uint numBytes, NativeOverlapped* pOVERLAP)
99104 /// <param name="recoverDevice">Whether to recover device metadata from existing files</param>
100105 /// <param name="initialLogFileHandles">Optional set of preloaded safe file handles, which can speed up hydration of preexisting log file handles</param>
101106 /// <param name="useIoCompletionPort">Whether we use IO completion port with polling</param>
107+ /// <param name="readOnly">Indicate if this is a readonly device</param>
108+ /// <param name="logger">ILogger instance</param>
102109 protected internal LocalStorageDevice ( string filename ,
103110 bool preallocateFile = false ,
104111 bool deleteOnClose = false ,
@@ -107,7 +114,8 @@ protected internal LocalStorageDevice(string filename,
107114 bool recoverDevice = false ,
108115 IEnumerable < KeyValuePair < int , SafeFileHandle > > initialLogFileHandles = null ,
109116 bool useIoCompletionPort = true ,
110- bool readOnly = false )
117+ bool readOnly = false ,
118+ ILogger logger = null )
111119 : base ( filename , GetSectorSize ( filename ) , capacity )
112120 {
113121 if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
@@ -146,6 +154,7 @@ protected internal LocalStorageDevice(string filename,
146154 this . deleteOnClose = deleteOnClose ;
147155 this . disableFileBuffering = disableFileBuffering ;
148156 this . readOnly = readOnly ;
157+ this . logger = logger ;
149158 results = new ConcurrentQueue < SimpleAsyncResult > ( ) ;
150159
151160 logHandles = initialLogFileHandles != null
@@ -255,12 +264,14 @@ public override void ReadAsync(int segmentId, ulong sourceAddress,
255264 }
256265 catch ( IOException e )
257266 {
267+ logger ? . LogCritical ( e , $ "{ nameof ( ReadAsync ) } ") ;
258268 Interlocked . Decrement ( ref numPending ) ;
259269 callback ( ( uint ) ( e . HResult & 0x0000FFFF ) , 0 , context ) ;
260270 results . Enqueue ( result ) ;
261271 }
262- catch
272+ catch ( Exception e )
263273 {
274+ logger ? . LogCritical ( e , $ "{ nameof ( ReadAsync ) } ") ;
264275 Interlocked . Decrement ( ref numPending ) ;
265276 callback ( uint . MaxValue , 0 , context ) ;
266277 results . Enqueue ( result ) ;
@@ -322,12 +333,14 @@ public override unsafe void WriteAsync(IntPtr sourceAddress,
322333 }
323334 catch ( IOException e )
324335 {
336+ logger ? . LogCritical ( e , $ "{ nameof ( WriteAsync ) } ") ;
325337 Interlocked . Decrement ( ref numPending ) ;
326338 callback ( ( uint ) ( e . HResult & 0x0000FFFF ) , 0 , context ) ;
327339 results . Enqueue ( result ) ;
328340 }
329- catch
341+ catch ( Exception e )
330342 {
343+ logger ? . LogCritical ( e , $ "{ nameof ( WriteAsync ) } ") ;
331344 Interlocked . Decrement ( ref numPending ) ;
332345 callback ( uint . MaxValue , 0 , context ) ;
333346 results . Enqueue ( result ) ;
0 commit comments