summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozlog/mozlog/scripts/format.py
diff options
context:
space:
mode:
Diffstat (limited to 'testing/mozbase/mozlog/mozlog/scripts/format.py')
-rw-r--r--testing/mozbase/mozlog/mozlog/scripts/format.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/testing/mozbase/mozlog/mozlog/scripts/format.py b/testing/mozbase/mozlog/mozlog/scripts/format.py
new file mode 100644
index 0000000000..27a643068f
--- /dev/null
+++ b/testing/mozbase/mozlog/mozlog/scripts/format.py
@@ -0,0 +1,55 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+import argparse
+import sys
+
+from .. import commandline, handlers, reader
+
+
+def get_parser(add_help=True):
+ parser = argparse.ArgumentParser(
+ "format", description="Format a structured log stream", add_help=add_help
+ )
+ parser.add_argument(
+ "--input",
+ action="store",
+ default=None,
+ help="Filename to read from, defaults to stdin",
+ )
+ parser.add_argument(
+ "--output",
+ action="store",
+ default=None,
+ help="Filename to write to, defaults to stdout",
+ )
+ parser.add_argument(
+ "format", choices=list(commandline.log_formatters.keys()), help="Format to use"
+ )
+ return parser
+
+
+def main(**kwargs):
+ if kwargs["input"] is None:
+ input_file = sys.stdin
+ else:
+ input_file = open(kwargs["input"])
+ if kwargs["output"] is None:
+ output_file = sys.stdout
+ else:
+ output_file = open(kwargs["output"], "w")
+
+ formatter = commandline.log_formatters[kwargs["format"]][0]()
+
+ handler = handlers.StreamHandler(stream=output_file, formatter=formatter)
+
+ for data in reader.read(input_file):
+ handler(data)
+
+
+if __name__ == "__main__":
+ parser = get_parser()
+ args = parser.parse_args()
+ kwargs = vars(args)
+ main(**kwargs)