UM: Dump the EvaluationContext when evaluation of a policy fails.

This gives the log reader a complete picture of the state of variables
at the time when a policy evaluation has failed.

We slightly revise the JSON output of EvaluationContext::DumpContext()
to remove trailing whitespaces (newline, etc).

BUG=chromium:387937
TEST=Observed context dumping during unit testing.

Change-Id: I33712a896225f3be97bc313468f89f9cd724673b
Reviewed-on: https://chromium-review.googlesource.com/205896
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/update_manager/evaluation_context.cc b/update_manager/evaluation_context.cc
index eab0fea..2f827c1 100644
--- a/update_manager/evaluation_context.cc
+++ b/update_manager/evaluation_context.cc
@@ -9,6 +9,7 @@
 
 #include <base/bind.h>
 #include <base/json/json_writer.h>
+#include <base/strings/string_util.h>
 #include <base/values.h>
 
 #include "update_engine/utils.h"
@@ -186,6 +187,7 @@
   base::JSONWriter::WriteWithOptions(&value,
                                      base::JSONWriter::OPTIONS_PRETTY_PRINT,
                                      &json_str);
+  base::TrimWhitespaceASCII(json_str, base::TRIM_TRAILING, &json_str);
 
   return json_str;
 }
diff --git a/update_manager/evaluation_context_unittest.cc b/update_manager/evaluation_context_unittest.cc
index ab07a12..eafb219 100644
--- a/update_manager/evaluation_context_unittest.cc
+++ b/update_manager/evaluation_context_unittest.cc
@@ -400,7 +400,7 @@
             "      \"fake_int\": \"42\",\n"
             "      \"fake_poll\": \"Hello \\\"world\\\"!\"\n"
             "   }\n"
-            "}\n",
+            "}",
             eval_ctx_->DumpContext());
 }
 
diff --git a/update_manager/update_manager-inl.h b/update_manager/update_manager-inl.h
index 7d7049e..b5d3651 100644
--- a/update_manager/update_manager-inl.h
+++ b/update_manager/update_manager-inl.h
@@ -38,7 +38,8 @@
     status = (policy_.get()->*policy_method)(ec, state_.get(), &error, result,
                                              args...);
     LOG_IF(WARNING, status == EvalStatus::kFailed)
-        << "Evaluating policy failed: " << error;
+        << "Evaluating policy failed: " << error
+        << "\nEvaluation context: " << ec->DumpContext();
   }
 
   // If evaluating the main policy failed, defer to the default policy.
@@ -58,7 +59,6 @@
 
   LOG(INFO) << "Evaluating " << policy_name << " END";
 
-  // TODO(deymo): Log the actual state used from the EvaluationContext.
   return status;
 }