Files
qt-migration-legatus/fix_logging_macros.py
T

72 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""
Fix logging macro calls in Qt6 migrated code.
Adds m_logger parameter to DEBUG_LOG(), WARNING_LOG(), ERROR_LOG(), FATAL_LOG(), INFO_LOG(), TRACE_LOG()
"""
import re
import os
import sys
# Logging macros that need logger parameter
LOGGING_MACROS = [
'DEBUG_LOG',
'WARNING_LOG',
'ERROR_LOG',
'FATAL_LOG',
'INFO_LOG',
'TRACE_LOG'
]
def fix_logging_in_file(filepath):
"""Fix logging macro calls in a single file."""
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
original = content
fixes = 0
for macro in LOGGING_MACROS:
# Match macro calls without parameter: MACRO()
# But not already with parameter: MACRO(m_logger) or MACRO(logger)
pattern = rf'\b{macro}\s*\(\s*\)'
replacement = f'{macro}(m_logger)'
matches = re.findall(pattern, content)
if matches:
content = re.sub(pattern, replacement, content)
fixes += len(matches)
if fixes > 0:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
return fixes
def main():
if len(sys.argv) < 2:
print("Usage: fix_logging_macros.py <directory>")
sys.exit(1)
base_dir = sys.argv[1]
total_fixes = 0
files_fixed = 0
for root, dirs, files in os.walk(base_dir):
# Skip build directories and Qt5 sources
if 'build' in root or 'qt5-before' in root:
continue
for file in files:
if file.endswith(('.cpp', '.h', '.hpp', '.cc', '.cxx')):
filepath = os.path.join(root, file)
fixes = fix_logging_in_file(filepath)
if fixes > 0:
files_fixed += 1
total_fixes += fixes
print(f"Fixed {fixes} logging calls in {filepath}")
print(f"\nTotal: {total_fixes} logging macro calls fixed in {files_fixed} files")
if __name__ == '__main__':
main()