diff --git a/matlab/dynare.m b/matlab/dynare.m
index 12072a4255afe50ec82b9eb41a99d916fdc7bd69..0ba128ad5cee9b015658a554cc1057a2d982f7fc 100644
--- a/matlab/dynare.m
+++ b/matlab/dynare.m
@@ -102,7 +102,21 @@ for i=2:nargin
 end
 
 [status, result] = system(command);
+
 disp(result)
+
+% Save preprocessor result in logfile (if `no_log' option not present)
+no_log = 0;
+for i=2:nargin
+    no_log = no_log || strcmp(varargin{i-1}, "nolog");
+end
+if ~no_log
+    logname = [ substr(fname, 1, -4) ".log" ];
+    fid = fopen(logname, "w");
+    fputs(fid, result);
+    fclose(fid);
+end
+
 if status
     % Should not use "error(result)" since message will be truncated if too long
     error('DYNARE: preprocessing failed')
diff --git a/preprocessor/DynareMain.cc b/preprocessor/DynareMain.cc
index 23f75e07ad5809eaf62325428805d4e9bb794c3c..79201e391195f399be7c3bc833d7a19c3bfd50d6 100644
--- a/preprocessor/DynareMain.cc
+++ b/preprocessor/DynareMain.cc
@@ -30,6 +30,8 @@ using namespace std;
 #endif
 #include "macro/MacroDriver.hh"
 
+#include <unistd.h>
+
 /* Prototype for second part of main function
    Splitting main() in two parts was necessary because ParsingDriver.h and MacroDriver.h can't be
    included simultaneously (because of Bison limitations).
@@ -58,6 +60,13 @@ usage()
 int
 main(int argc, char **argv)
 {
+  /*
+    Redirect stderr to stdout.
+    Made necessary because MATLAB/Octave can only capture stdout (but not
+    stderr), in order to put it in the logfile (see issue #306)
+  */
+  dup2(STDOUT_FILENO, STDERR_FILENO);
+
   if (argc < 2)
     {
       cerr << "Missing model file!" << endl;
diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc
index 1fbe8a386ee98d8ad8e57d714e79c65713caa013..b628c9b396a3a4ba0f2449efb0e6f4fae7781ce5 100644
--- a/preprocessor/ModFile.cc
+++ b/preprocessor/ModFile.cc
@@ -477,11 +477,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b
   mOutputFile << "global_initialization;" << endl
               << "diary off;" << endl;
   if (!no_log)
-    mOutputFile << "logname_ = '" << basename << ".log';" << endl
-                << "if exist(logname_, 'file')" << endl
-                << "    delete(logname_)" << endl
-                << "end" << endl
-                << "diary(logname_)" << endl;
+    mOutputFile << "diary('" << basename << ".log');" << endl;
 
   if (console)
     mOutputFile << "options_.console_mode = 1;" << endl