[libcamera-devel,3/3] utils: libtuning: Add logging parameters to argparse
diff mbox series

Message ID 20221129053326.2858347-4-paul.elder@ideasonboard.com
State New
Headers show
Series
  • utils: tuning: Add logging infrastructure
Related show

Commit Message

Paul Elder Nov. 29, 2022, 5:33 a.m. UTC
Add command line arguments for log levels and log output file.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
---
 utils/tuning/libtuning/libtuning.py | 32 +++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)

Patch
diff mbox series

diff --git a/utils/tuning/libtuning/libtuning.py b/utils/tuning/libtuning/libtuning.py
index d34d98ab..b2c40ba4 100644
--- a/utils/tuning/libtuning/libtuning.py
+++ b/utils/tuning/libtuning/libtuning.py
@@ -6,6 +6,7 @@ 
 
 import argparse
 from pathlib import Path
+import sys
 
 import libtuning as lt
 from libtuning.logger import Logger
@@ -150,10 +151,24 @@  class Tuner(object):
         # options, so simply return an empty configuration if none is provided.
         parser.add_argument('-c', '--config', type=str, default='',
                             help='Config file (optional)')
-        # \todo Check if we really need this or if stderr is good enough, or if
-        # we want a better logging infrastructure with log levels
-        parser.add_argument('-l', '--log', type=str, default=None,
-                            help='Output log file (optional)')
+        parser.add_argument('-l', '--log', type=str, default=sys.stderr,
+                            help='Output log file (optional). Default is stderr.')
+        parser.add_argument('-ll', '--log_levels', type=str, default='',
+                            help='''Output log levels (optional).
+                                    A comma-separated list of module names and
+                                    their log levels (in integer form, where
+                                    debug=0, info=1, warn=2, error=3, and
+                                    fatal=4), in the format: "{module name}:{log
+                                    level},{module name}:{log_level}...", and so
+                                    on. Any module that is not specified will use the
+                                    default log level, or the default can be
+                                    specified with the log name "*". For the
+                                    purposes of logging, module names are the
+                                    same as class names, or the file name for
+                                    components that are not classes. This
+                                    parameter can also be simply set to a
+                                    single integer to set the log levels for
+                                    all components.''')
         parser.add_argument('-dd', '--debug_dir', type=str, default=None,
                             help='''Debug output directory (optional).
                                     If the directory does not exist, it will be
@@ -205,6 +220,12 @@  class Tuner(object):
             if not debug_dir.is_dir():
                 debug_dir.mkdir()
 
+        # Prepare logging
+        if args.log is not sys.stderr:
+            Logger.log_file = open(args.log, mode='a')
+
+        Logger.set_log_levels(args.log_levels)
+
         # Validate modules and set debug
         for module in self.modules:
             if not module.validate_config(self.config):
@@ -237,4 +258,7 @@  class Tuner(object):
 
         self.generator.write(args.output, self.output, self.output_order)
 
+        if Logger.log_file is not sys.stderr:
+            Logger.log_file.close()
+
         return 0