]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
tools/cephfs: error handling in EventOutput 1739/head
authorJohn Spray <john.spray@inktank.com>
Tue, 20 May 2014 11:48:49 +0000 (12:48 +0100)
committerJohn Spray <john.spray@inktank.com>
Tue, 20 May 2014 13:43:55 +0000 (14:43 +0100)
Print error instead of hitting assertion if
we e.g. can't create directory.

Signed-off-by: John Spray <john.spray@inktank.com>
src/tools/cephfs/EventOutput.cc
src/tools/cephfs/EventOutput.h
src/tools/cephfs/JournalTool.cc

index 63cb574521d4da846b921d5e79c04824c1eba54b..e5cbf2478716d0354030909e1396c0e665e18523 100644 (file)
 #include "EventOutput.h"
 
 
-void EventOutput::binary() const
+int EventOutput::binary() const
 {
   // Binary output, files
   int r = ::mkdir(path.c_str(), 0755);
   if (r != 0) {
     std::cerr << "Error creating output directory: " << cpp_strerror(r) << std::endl;
-    assert(r == 0);
+    return r;
   }
 
   for (JournalScanner::EventMap::const_iterator i = scan.events.begin(); i != scan.events.end(); ++i) {
@@ -44,11 +44,16 @@ void EventOutput::binary() const
     std::ofstream bin_file(file_path.c_str(), std::ofstream::out | std::ofstream::binary);
     le_bin.write_stream(bin_file);
     bin_file.close();
+    if (bin_file.fail()) {
+      return -EIO;
+    }
   }
   std::cerr << "Wrote output to binary files in directory '" << path << "'" << std::endl;
+
+  return 0;
 }
 
-void EventOutput::json() const
+int EventOutput::json() const
 {
   JSONFormatter jf(true);
   std::ofstream out_file(path.c_str(), std::ofstream::out);
@@ -66,7 +71,13 @@ void EventOutput::json() const
   jf.close_section();  // journal
   jf.flush(out_file);
   out_file.close();
-  std::cerr << "Wrote output to JSON file '" << path << "'" << std::endl;
+
+  if (out_file.fail()) {
+    return -EIO;
+  } else {
+    std::cerr << "Wrote output to JSON file '" << path << "'" << std::endl;
+    return 0;
+  }
 }
 
 void EventOutput::list() const
index 2c93794dbd9c4fb930351b8f4c54edfdb880352d..65d968409b4e30be03d517793f2486a22a77541b 100644 (file)
@@ -34,8 +34,8 @@ class EventOutput
 
     void summary() const;
     void list() const;
-    void json() const;
-    void binary() const;
+    int json() const;
+    int binary() const;
 };
 
 #endif // EVENT_OUTPUT_H
index c11cabafd3314dfcee1cf1d1e002c5dc1bfb1c5c..fa6bf2b1bd2781ebe400cba5976a87847520d857 100644 (file)
@@ -378,20 +378,25 @@ int JournalTool::main_event(std::vector<const char*> &argv)
   // Generate output
   // ===============
   EventOutput output(js, output_path);
+  int output_result = 0;
   if (output_style == "binary") {
-      output.binary();
+      output_result = output.binary();
   } else if (output_style == "json") {
-      output.json();
+      output_result = output.json();
   } else if (output_style == "summary") {
       output.summary();
   } else if (output_style == "list") {
       output.list();
   } else {
-    derr << "Bad output command '" << output_style << "'" << dendl;
+    std::cerr << "Bad output command '" << output_style << "'" << std::endl;
     return -EINVAL;
   }
 
-  return 0;
+  if (output_result != 0) {
+    std::cerr << "Error writing output: " << cpp_strerror(output_result) << std::endl;
+  }
+
+  return output_result;
 }
 
 /**