Added Logging and Error Handling

This commit is contained in:
fuysaal 2025-03-24 14:49:26 -04:00
parent 23dda1022d
commit 67518e7895

76
sqlmapapi.py Executable file → Normal file
View File

@ -6,15 +6,24 @@ See the file 'LICENSE' for copying permission
""" """
import sys import sys
import logging
import os
import warnings
# Error management and logging setup
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s', # Log format
level=logging.DEBUG, # Set log level to DEBUG
handlers=[logging.StreamHandler()] # Print logs to console
)
# Logger object
logger = logging.getLogger(__name__)
sys.dont_write_bytecode = True sys.dont_write_bytecode = True
__import__("lib.utils.versioncheck") # this has to be the first non-standard import __import__("lib.utils.versioncheck") # this has to be the first non-standard import
import logging
import os
import warnings
warnings.filterwarnings(action="ignore", category=UserWarning) warnings.filterwarnings(action="ignore", category=UserWarning)
warnings.filterwarnings(action="ignore", category=DeprecationWarning) warnings.filterwarnings(action="ignore", category=DeprecationWarning)
@ -83,36 +92,43 @@ def main():
""" """
REST-JSON API main function REST-JSON API main function
""" """
try:
dirtyPatches()
resolveCrossReferences()
dirtyPatches() # Set default logging level to debug
resolveCrossReferences() logger.setLevel(logging.DEBUG)
# Set default logging level to debug # Initialize paths
logger.setLevel(logging.DEBUG) setPaths(modulePath())
# Initialize paths # Parse command line options
setPaths(modulePath()) apiparser = ArgumentParser()
apiparser.add_argument("-s", "--server", help="Run as a REST-JSON API server", action="store_true")
# Parse command line options apiparser.add_argument("-c", "--client", help="Run as a REST-JSON API client", action="store_true")
apiparser = ArgumentParser() apiparser.add_argument("-H", "--host", help="Host of the REST-JSON API server (default \"%s\")" % RESTAPI_DEFAULT_ADDRESS, default=RESTAPI_DEFAULT_ADDRESS)
apiparser.add_argument("-s", "--server", help="Run as a REST-JSON API server", action="store_true") apiparser.add_argument("-p", "--port", help="Port of the REST-JSON API server (default %d)" % RESTAPI_DEFAULT_PORT, default=RESTAPI_DEFAULT_PORT, type=int)
apiparser.add_argument("-c", "--client", help="Run as a REST-JSON API client", action="store_true") apiparser.add_argument("--adapter", help="Server (bottle) adapter to use (default \"%s\")" % RESTAPI_DEFAULT_ADAPTER, default=RESTAPI_DEFAULT_ADAPTER)
apiparser.add_argument("-H", "--host", help="Host of the REST-JSON API server (default \"%s\")" % RESTAPI_DEFAULT_ADDRESS, default=RESTAPI_DEFAULT_ADDRESS) apiparser.add_argument("--database", help="Set IPC database filepath (optional)")
apiparser.add_argument("-p", "--port", help="Port of the REST-JSON API server (default %d)" % RESTAPI_DEFAULT_PORT, default=RESTAPI_DEFAULT_PORT, type=int) apiparser.add_argument("--username", help="Basic authentication username (optional)")
apiparser.add_argument("--adapter", help="Server (bottle) adapter to use (default \"%s\")" % RESTAPI_DEFAULT_ADAPTER, default=RESTAPI_DEFAULT_ADAPTER) apiparser.add_argument("--password", help="Basic authentication password (optional)")
apiparser.add_argument("--database", help="Set IPC database filepath (optional)") (args, _) = apiparser.parse_known_args() if hasattr(apiparser, "parse_known_args") else apiparser.parse_args()
apiparser.add_argument("--username", help="Basic authentication username (optional)")
apiparser.add_argument("--password", help="Basic authentication password (optional)")
(args, _) = apiparser.parse_known_args() if hasattr(apiparser, "parse_known_args") else apiparser.parse_args()
# Start the client or the server # Start the client or the server
if args.server: if args.server:
server(args.host, args.port, adapter=args.adapter, username=args.username, password=args.password, database=args.database) server(args.host, args.port, adapter=args.adapter, username=args.username, password=args.password, database=args.database)
elif args.client: elif args.client:
client(args.host, args.port, username=args.username, password=args.password) client(args.host, args.port, username=args.username, password=args.password)
else: else:
apiparser.print_help() apiparser.print_help()
except Exception as e:
logger.critical("An error occurred while executing the main function: %s", e, exc_info=True)
raise # Stop the program after the error
if __name__ == "__main__": if __name__ == "__main__":
main() try:
main()
except Exception as e:
logger.critical("An unexpected error occurred while running the program: %s", e, exc_info=True)