proper OS compatibility

This commit is contained in:
TheFlipside88 2022-10-02 13:42:12 +02:00
parent 51b86c730d
commit c28f52e2b1
2 changed files with 28 additions and 8 deletions

View File

@ -1,4 +1,5 @@
import os import os
import socket
from datetime import date from datetime import date
from datetime import datetime from datetime import datetime
@ -11,7 +12,7 @@ if os.name == 'nt':
# For OS which are not MS Windows # For OS which are not MS Windows
else: else:
logfile = os.path.join(os.path.join(os.environ['HOME']), logfile = os.path.join(os.path.join(os.environ['HOME']),
r'Documents\login.log') r'Documents/login.log')
# Create the file if it does not exist yet # Create the file if it does not exist yet
if not os.path.exists(logfile): if not os.path.exists(logfile):
@ -29,6 +30,11 @@ current_time = now.strftime("%H:%M:%S")
# Determine the client hostname # Determine the client hostname
client_hostname = os.getenv("CLIENTNAME") client_hostname = os.getenv("CLIENTNAME")
# Determine the client hostname via a different method
# in case the first one failed
if client_hostname is None:
client_hostname = socket.gethostbyaddr(socket.gethostname())[0]
# Class for prepending the information to the log file # Class for prepending the information to the log file
class Prepender(object): class Prepender(object):

View File

@ -1,10 +1,18 @@
import os import os
import socket
from datetime import date from datetime import date
from datetime import datetime from datetime import datetime
# Set the location of the file to write to, in the user Documents directory
logfile = os.path.join(os.path.join(os.environ['USERPROFILE']), # Determine if the OS is MS Windows
r'Documents\logoff.log') if os.name == 'nt':
# Set the location of the file to write to, in the user Documents directory
logfile = os.path.join(os.path.join(os.environ['USERPROFILE']),
r'Documents\logoff.log')
# For OS which are not MS Windows
else:
logfile = os.path.join(os.path.join(os.environ['HOME']),
r'Documents/logoff.log')
# Create the file if it does not exist yet # Create the file if it does not exist yet
if not os.path.exists(logfile): if not os.path.exists(logfile):
@ -16,10 +24,16 @@ today = date.today()
now = datetime.now() now = datetime.now()
current_day = today.strftime("%d.%m.%Y") current_day = today.strftime("%d.%m.%Y")
# print("Today's date:", current_day)
current_time = now.strftime("%H:%M:%S") current_time = now.strftime("%H:%M:%S")
# print("Current Time =", current_time)
# Determine the client hostname
client_hostname = os.getenv("CLIENTNAME")
# Determine the client hostname via a different method
# in case the first one failed
if client_hostname is None:
client_hostname = socket.gethostbyaddr(socket.gethostname())[0]
# Class for prepending the date and time to the log file # Class for prepending the date and time to the log file
@ -55,10 +69,10 @@ class Prepender(object):
self.__open_file.close() self.__open_file.close()
# Prepend the determined date and time to the top of the log file # Prepend the determined information to the top of the log file
with Prepender(logfile) as f: with Prepender(logfile) as f:
# Must write individual lines in reverse order # Must write individual lines in reverse order
f.write_line(current_day+' - '+current_time) f.write_line(current_day+' - '+current_time+' - '+client_hostname)
# Or, use write_lines instead - that maintains order. # Or, use write_lines instead - that maintains order.
# with Prepender(logfile) as f: # with Prepender(logfile) as f: