add plain text output capability
[logparse.git] / logparse / interface.py
index f9a76b2b4c1c7a40dbb63361380543c5a4526388..a9bd2e1125d9e3baa2def24f9600faf6b3b6d797 100644 (file)
@@ -46,6 +46,7 @@ def main():
     argparser.add_argument('-l', '--logs', help='services to analyse', required=False)
     argparser.add_argument('-nl', '--ignore-logs', help='skip these services (takes precedence over -l)', required=False)
     argparser.add_argument('-es', '--embed-styles', help='make CSS rules inline rather than linking the file', required=False, default=False, action='store_true')
+    argparser.add_argument('-nh', '--plain', help='write/send plain text rather than HTML', required = False, default=False, action='store_true')
 
     # Load config
     if argparser.parse_args().config:
@@ -74,11 +75,15 @@ def main():
     logger.info("Beginning log analysis at {0} {1}".format(start.strftime(formatting.DATEFMT), start.strftime(formatting.TIMEFMT)))
     logger.debug("This is {0} version {1}, running on Python {2}".format(logparse.__name__, logparse.__version__, sys.version.replace('\n', '')))
      
-    # Write HTML header
+    # Write header
 
-    global output_html
-    output_html = formatting.header(prefs['header'])
-    output_html += formatting.opentag('div', id='main')
+    global output
+    if argparser.parse_args().plain:
+        output = formatting.PlaintextOutput(linewidth=prefs['linewidth'])
+    else:
+        output = formatting.HtmlOutput()
+
+    output.append_header(prefs['header'])
 
     # Find parsers
     
@@ -117,32 +122,27 @@ def main():
 
     logger.debug(str(parser_providers))
     for parser in parser_providers:
-        output_html += parser.parse_log()
+        output.append_section(parser.parse_log())
 
     # Write HTML footer
-
-    output_html += formatting.closetag('div') + formatting.closetag('body') + formatting.closetag('html')
+    output.append_footer()
 
     if argparser.parse_args().printout:
-        print(output_html)
+        print(output)
     if argparser.parse_args().destination or prefs['output']:
         if argparser.parse_args().destination:
             dest_path = argparser.parse_args().destination
         else:
             dest_path = prefs['output']
         logger.debug("Outputting to {0}".format(dest_path))
-        if argparser.parse_args().embed_styles or prefs['embed-styles']:
-            output_html = mail.mailprep(output_html, prefs['css'])
-        if not os.path.isfile(dest_path) and not (argparser.parse_args().overwrite or config['overwrite']):
-            with open(dest_path, 'w') as f:
-                f.write(output_html)
-                logger.info("Written output to {}".format(dest_path))
+        if (argparser.parse_args().embed_styles or prefs['embed-styles']) and not (argparser.parse_args.plain or prefs['plain']):
+            output.embed_css(prefs['css'])
+        if (not os.path.isfile(dest_path)) and not (argparser.parse_args().overwrite or config['overwrite']):
+            output.write(dest_path)
         else:
             logger.warning("Destination file already exists")
             if input("Would you like to overwrite {0}? (y/n) [n] ".format(dest_path)) == 'y':
-                with open(dest_path, 'w') as f:
-                    f.write(output_html)
-                logger.debug("Written output to {}".format(dest_path))
+                output.write(dest_path)
             else:
                 logger.warning("No output written")
 
@@ -151,7 +151,8 @@ def main():
             to = argparser.parse_args().to
         else:
             to = prefs['mail']['to']
-        mail.sendmail(mailbin=prefs['mail']['mailbin'], body=mail.mailprep(output_html, prefs['css']), recipient=to, subject=formatting.fsubject(config.prefs['mail']['subject']))
+        if argparser.parse_args().plain or prefs['plain']:
+            mail.sendmail(mailbin=prefs['mail']['mailbin'], body=(output.embed_css(prefs['css']) if isinstance(output, formatting.HtmlOutput) else output.content), recipient=to, subject=formatting.fsubject(config.prefs['mail']['subject']))
     
     # Print end message
     finish = datetime.now()