Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update odl.py to handle empty log files #9

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions odl.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def ReadUnixMsTime(unix_time_ms): # Unix millisecond timestamp
"reserved" / Byte[0x64]
)

def is_file_empty(file_path):
'''Check if file is empty by confirming if its size is 0 bytes'''
return os.path.exists(file_path) and os.stat(file_path).st_size == 0

def read_string(data):
'''read string, return tuple (bytes_consumed, string)'''
if (len(data)) >= 4:
Expand Down Expand Up @@ -480,21 +484,24 @@ def main():
paths.extend(glob.glob(os.path.join(odl_folder, pattern)))
for path in paths:
print("Searching ", path)
try:
odl_rows = process_odl(path, map, args.all_data)
if is_file_empty(path):
print("File is empty, file size is 0 bytes")
else:
try:
if odl_rows:
writer.writerows(odl_rows)
print(f'Wrote {len(odl_rows)} rows')
else:
print("No log data was found in this file.")
except Exception as ex:
print("ERROR writing rows:", type(ex), ex)
except OSError as ex:
print(f"Error - File not found! {path}")

odl_rows = process_odl(path, map, args.all_data)
try:
if odl_rows:
writer.writerows(odl_rows)
print(f'Wrote {len(odl_rows)} rows')
else:
print("No log data was found in this file.")
except Exception as ex:
print("ERROR writing rows:", type(ex), ex)
except OSError as ex:
print(f"Error - File not found! {path}")

csv_f.close()
print(f'Finished processing files, output is at {csv_file_path}')

if __name__ == "__main__":
main()
main()