From ce5aa33a33d135ec4120fa2d9db156a00e32070b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 15 Oct 2020 17:14:53 +0200
Subject: [PATCH] k-order DLL: no longer write journal file on-disk, unless
 options_.debug = true

Closes: #1735
---
 dynare++/kord/journal.hh                             | 12 ++++++++----
 .../k_order_perturbation/k_order_perturbation.cc     | 10 +++++++++-
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/dynare++/kord/journal.hh b/dynare++/kord/journal.hh
index 63b77a9b93..58ce81772e 100644
--- a/dynare++/kord/journal.hh
+++ b/dynare++/kord/journal.hh
@@ -1,6 +1,6 @@
 /*
  * Copyright © 2004 Ondra Kamenik
- * Copyright © 2019 Dynare Team
+ * Copyright © 2019-2020 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -57,14 +57,18 @@ struct SystemResources
 
 class Journal : public std::ofstream
 {
-  int ord;
-  int depth;
+  int ord{0};
+  int depth{0};
 public:
   explicit Journal(const std::string &fname)
-    : std::ofstream(fname), ord(0), depth(0)
+    : std::ofstream(fname)
   {
     printHeader();
   }
+  /* Constructor that does not initialize the std::ofstream. To be used when an
+     on-disk journal is not wanted. */
+  Journal() = default;
+  Journal &operator=(Journal &&) = default;
   ~Journal() override
   {
     flush();
diff --git a/mex/sources/k_order_perturbation/k_order_perturbation.cc b/mex/sources/k_order_perturbation/k_order_perturbation.cc
index 4ae1151db7..c091db3399 100644
--- a/mex/sources/k_order_perturbation/k_order_perturbation.cc
+++ b/mex/sources/k_order_perturbation/k_order_perturbation.cc
@@ -121,6 +121,11 @@ extern "C" {
       mexErrMsgTxt("options_.threads.k_order_perturbation be a numeric scalar");
     int num_threads = static_cast<int>(mxGetScalar(num_threads_mx));
 
+    const mxArray *debug_mx = mxGetField(options_mx, 0, "debug");
+    if (!(debug_mx && mxIsLogicalScalar(debug_mx)))
+      mexErrMsgTxt("options_.debug should be a logical scalar");
+    bool debug = static_cast<bool>(mxGetScalar(debug_mx));
+
     // Extract various fields from M_
     const mxArray *fname_mx = mxGetField(M_mx, 0, "fname");
     if (!(fname_mx && mxIsChar(fname_mx) && mxGetM(fname_mx) == 1))
@@ -197,7 +202,10 @@ extern "C" {
 
     try
       {
-        Journal journal(fname + ".jnl");
+        // Journal is not written on-disk, unless options_.debug = true (see #1735)
+        Journal journal;
+        if (debug)
+          journal = Journal{fname + ".jnl"};
 
         std::unique_ptr<DynamicModelAC> dynamicModelFile;
         if (use_dll)
-- 
GitLab