pytail.py 1.34 KB
  1
  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
#!/usr/bin/env python3

import re, os, time, sys
from time import time, sleep
from os.path import join, getsize, getmtime

if len(sys.argv) == 1:
print("Argument Missing !")
print("Usage:\n {0:s} /path/to/log/file".format(sys.argv[0]))
sys.exit(1)

startTime = time()
mTimeStart = 0
fileName = sys.argv[1]

if not os.path.exists(fileName) or not os.path.isfile(fileName):
print("This file '{0:s}' does not exist !".format(fileName))
sys.exit(2)

try:
with open(fileName, 'r') as f:
f.read(1)
except Exception as e:
print("This file '{0:s}' cannot be opened for reading !".format(fileName))
sys.exit(3)

folderLogs = os.path.dirname(fileName)

copyLines = []

try:

while True:
with open(fileName, 'r') as f:
lines = f.readlines()

show = False

if len(copyLines) == 0:
showLines = lines[-10:]
show = True

elif len(copyLines) != len(lines):
showLines = lines[len(copyLines) - len(lines):]
show = True

if show:
copyLines = lines.copy()
for line in showLines:
print(line, end='')

sleep(1)

except KeyboardInterrupt as e:
print("Program stopped by user !")
sys.exit(0)

except Exception as e:
print("Unknown error during execution !")
print(e)
sys.exit(4)