From a6c649cfc8a591d0157b7bc89f34c6ef51a9ca27 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sat, 11 Feb 2017 09:59:54 +0100
Subject: [PATCH] Added --log option to save log messages in JSON format to a
 file.

See #3392.
---
 MANUAL.txt             |  6 ++++++
 pandoc.cabal           | 19 ++-----------------
 src/Text/Pandoc/App.hs | 12 ++++++++++++
 3 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/MANUAL.txt b/MANUAL.txt
index 36bc8e5ac..105ef8b1e 100644
--- a/MANUAL.txt
+++ b/MANUAL.txt
@@ -356,6 +356,12 @@ General options
 
 :   Exit with error status if there are any warnings.
 
+`--log=`*FILE*
+
+:   Write log messages in machine-readable JSON format to
+    *FILE*.  All messages above DEBUG level will be written,
+    regardless of verbosity settings (`--verbose`, `--quiet`).
+
 `--list-input-formats`
 
 :   List supported input formats, one per line.
diff --git a/pandoc.cabal b/pandoc.cabal
index a817cdab2..255057cfa 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -275,6 +275,7 @@ Library
                  extensible-exceptions >= 0.1 && < 0.2,
                  pandoc-types >= 1.17 && < 1.18,
                  aeson >= 0.7 && < 1.2,
+                 aeson-pretty >= 0.8 && < 0.9,
                  tagsoup >= 0.13.7 && < 0.15,
                  base64-bytestring >= 0.1 && < 1.1,
                  zlib >= 0.5 && < 0.7,
@@ -435,23 +436,7 @@ Library
 
 Executable pandoc
   Build-Depends: pandoc,
-                 pandoc-types >= 1.17 && < 1.18,
-                 base >= 4.2 && <5,
-                 directory >= 1.2 && < 1.4,
-                 filepath >= 1.1 && < 1.5,
-                 text >= 0.11 && < 1.3,
-                 bytestring >= 0.9 && < 0.11,
-                 extensible-exceptions >= 0.1 && < 0.2,
-                 skylighting >= 0.1.1.3 && < 0.2,
-                 aeson >= 0.7.0.5 && < 1.2,
-                 yaml >= 0.8.8.2 && < 0.9,
-                 containers >= 0.1 && < 0.6,
-                 HTTP >= 4000.0.5 && < 4000.4,
-                 mtl >= 2.2 && < 2.3
-  if flag(network-uri)
-     Build-Depends: network-uri >= 2.6 && < 2.7, network >= 2.6
-  else
-     Build-Depends: network >= 2 && < 2.6
+                 base >= 4.2 && < 5
   Ghc-Options:   -rtsopts -with-rtsopts=-K16m -Wall -fno-warn-unused-do-bind
   Ghc-Prof-Options: -fprof-auto-exported -rtsopts -with-rtsopts=-K16m
 
diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs
index 91d0711c1..df1ed9fde 100644
--- a/src/Text/Pandoc/App.hs
+++ b/src/Text/Pandoc/App.hs
@@ -71,6 +71,7 @@ import qualified Data.ByteString.Lazy as B
 import qualified Data.ByteString as BS
 import qualified Data.Map as M
 import Data.Aeson (eitherDecode', encode)
+import Data.Aeson.Encode.Pretty (encodePretty)
 import Data.Yaml (decode)
 import qualified Data.Yaml as Yaml
 import qualified Data.Text as T
@@ -348,6 +349,9 @@ convertWithOpts opts = do
                              x <- f
                              rs <- getLog
                              return (x, rs)
+        case optLogFile opts of
+             Nothing -> return ()
+             Just logfile -> B.writeFile logfile (encodePretty reports)
         let isWarning msg = messageVerbosity msg == WARNING
         when (optFailIfWarnings opts && any isWarning reports) $
             err 3 "Failing because there were warnings."
@@ -487,6 +491,7 @@ data Opt = Opt
     , optDumpArgs          :: Bool    -- ^ Output command-line arguments
     , optIgnoreArgs        :: Bool    -- ^ Ignore command-line arguments
     , optVerbosity         :: Verbosity  -- ^ Verbosity of diagnostic output
+    , optLogFile           :: Maybe FilePath -- ^ File to write JSON log output
     , optFailIfWarnings    :: Bool    -- ^ Fail on warnings
     , optReferenceLinks    :: Bool    -- ^ Use reference links in writing markdown, rst
     , optReferenceLocation :: ReferenceLocation -- ^ location for footnotes and link references in markdown output
@@ -552,6 +557,7 @@ defaultOpts = Opt
     , optDumpArgs              = False
     , optIgnoreArgs            = False
     , optVerbosity             = WARNING
+    , optLogFile               = Nothing
     , optFailIfWarnings        = False
     , optReferenceLinks        = False
     , optReferenceLocation     = EndOfDocument
@@ -1289,6 +1295,12 @@ options =
                   (\opt -> return opt { optFailIfWarnings = True }))
                  "" -- "Exit with error status if there were  warnings."
 
+    , Option "" ["log"]
+                 (ReqArg
+                  (\arg opt -> return opt{ optLogFile = Just arg })
+                "FILE")
+                "" -- "Log messages in JSON format to this file."
+
     , Option "" ["bash-completion"]
                  (NoArg
                   (\_ -> do