Posted to tcl by thomas at Thu Nov 03 21:45:43 GMT 2011view raw

  1. set fileToRead d:/tmp/log.txt
  2.  
  3. set pollIntervalInMs 1000
  4.  
  5.  
  6.  
  7. # Get and store the current size of the file to read :
  8.  
  9. set waitForFile 1
  10.  
  11. while { $waitForFile } {
  12.  
  13. if { [catch {
  14.  
  15. set lastKnownFileSize [file size $fileToRead]
  16.  
  17. } errorMsg] } {
  18.  
  19. puts "Warning : Could not get the initial size of $fileToRead. Retry in $pollIntervalInMs ms..."
  20.  
  21. after $pollIntervalInMs
  22.  
  23. } else {
  24.  
  25. set waitForFile 0
  26.  
  27. }
  28.  
  29. }
  30.  
  31.  
  32.  
  33. # Store the initial file pointer position (end of file) :
  34.  
  35. set currentPos $lastKnownFileSize ; # Can be forced to 0 is a full log file parse operation is required @ startup (but this could take a long time)
  36.  
  37.  
  38.  
  39. # Loop until a condition is set :
  40.  
  41. set continueFlag 1 ; # Set to 0 by a depressed button ?
  42.  
  43. while { $continueFlag } {
  44.  
  45.  
  46.  
  47. # Wait between each poll :
  48.  
  49. after $pollIntervalInMs
  50.  
  51.  
  52.  
  53. # Get the new file size :
  54.  
  55. if { [catch {
  56.  
  57. set newFileSize [file size $fileToRead]
  58.  
  59. } errorMsg ] } {
  60.  
  61. puts "Warning : Could not get the size of $fileToRead !"
  62.  
  63. continue
  64.  
  65. }
  66.  
  67.  
  68.  
  69. # Has file been modified ? :
  70.  
  71. if { $newFileSize != $lastKnownFileSize } {
  72.  
  73.  
  74.  
  75. # Store file size for next change detection :
  76.  
  77. set lastKnownFileSize $newFileSize
  78.  
  79.  
  80.  
  81. # Try to open file :
  82.  
  83. if { [catch {
  84.  
  85. set fileHandler [open $fileToRead r]
  86.  
  87. } errorMsg ] } {
  88.  
  89. puts "Warning : Could not open $fileToRead for reading after change detection ($errorMsg) !"
  90.  
  91. continue
  92.  
  93. }
  94.  
  95.  
  96.  
  97. # Deal with the 'truncated' file case :
  98.  
  99. if { $newFileSize < $currentPos } {
  100.  
  101. puts "Warning : File truncated, moving to the end of file."
  102.  
  103. set currentPos $newFileSize
  104.  
  105. }
  106.  
  107.  
  108.  
  109. # Move to the last "known" position :
  110.  
  111. if { [catch {
  112.  
  113. seek $fileHandler $currentPos start
  114.  
  115. } errorMsg] } {
  116.  
  117. puts "Warning : Could not seek file $fileToRead to byte $currentPos ($errorMsg) !"
  118.  
  119. continue
  120.  
  121. }
  122.  
  123.  
  124.  
  125. # Read until the end of file :
  126.  
  127. if { [catch {
  128.  
  129. while { [gets $fileHandler line] >= 0 } {
  130.  
  131. puts -($currentPos)->$line
  132.  
  133. }
  134.  
  135. } errorMsg] } {
  136.  
  137. puts "Warning : Could not read data from $fileToRead ($errorMsg) !"
  138.  
  139. }
  140.  
  141.  
  142.  
  143. # Store current position and close file :
  144.  
  145. if { [catch {
  146.  
  147. set currentPos [tell $fileHandler]
  148.  
  149. close $fileHandler
  150.  
  151. } errorMsg] } {
  152.  
  153. puts "Warning : Could not properly close $fileToRead ($errorMsg)"
  154.  
  155. }
  156.  
  157. }
  158.  
  159. }