Make sure that output files are always checked success before they're used.
authorAli Saidi <saidi@eecs.umich.edu>
Thu, 15 May 2008 23:10:26 +0000 (19:10 -0400)
committerAli Saidi <saidi@eecs.umich.edu>
Thu, 15 May 2008 23:10:26 +0000 (19:10 -0400)
Make OutputDirectory::resolve() private and change the functions using
resolve() to instead use create().

--HG--
extra : convert_revision : 36d4be629764d0c4c708cec8aa712cd15f966453

src/base/output.cc
src/base/output.hh
src/dev/etherdump.cc
src/dev/etherdump.hh
src/mem/cache/tags/split.cc

index 9d02a4a719a8b2b2c861a0d07af1c075852a9040..5a1768a76f655af3e72b9ef9d3023f3fb629c54f 100644 (file)
@@ -98,7 +98,7 @@ OutputDirectory::create(const string &name, bool binary)
     ofstream *file = new ofstream(resolve(name).c_str(),
             ios::trunc | binary ?  ios::binary : (ios::openmode)0);
     if (!file->is_open())
-        panic("Cannot open file %s", name);
+        fatal("Cannot open file %s", name);
 
     return file;
 }
@@ -119,7 +119,7 @@ OutputDirectory::find(const string &name)
 
     ofstream *file = new ofstream(filename.c_str(), ios::trunc);
     if (!file->is_open())
-        panic("Cannot open file %s", filename);
+        fatal("Cannot open file %s", filename);
 
     files[filename] = file;
     return file;
index 5de0c4005f6999dbbd1969934b72494df615009f..b1d1d7e7d6f5da76ce469145e2d2d95564fd26e5 100644 (file)
@@ -43,6 +43,8 @@ class OutputDirectory
     map_t files;
     std::string dir;
 
+    std::string resolve(const std::string &name);
+
   public:
     OutputDirectory();
     ~OutputDirectory();
@@ -50,7 +52,6 @@ class OutputDirectory
     void setDirectory(const std::string &dir);
     const std::string &directory();
 
-    std::string resolve(const std::string &name);
     std::ostream *create(const std::string &name, bool binary = false);
     std::ostream *find(const std::string &name);
 
index 4710935212d6464875035e3900cec2d1ed7c0d5e..07e52f36d5846b1c4922252d491f6ed08d002637 100644 (file)
@@ -45,7 +45,7 @@
 using std::string;
 
 EtherDump::EtherDump(const Params *p)
-    : SimObject(p), stream(simout.resolve(p->file).c_str()),
+    : SimObject(p), stream(simout.create(p->file, true)),
       maxlen(p->maxlen)
 {
 }
@@ -86,7 +86,7 @@ EtherDump::init()
     hdr.sigfigs = 0;
     hdr.linktype = DLT_EN10MB;
 
-    stream.write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
+    stream->write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
 
     /*
      * output an empty packet with the current time so that we know
@@ -98,9 +98,9 @@ EtherDump::init()
     pkthdr.microseconds = 0;
     pkthdr.caplen = 0;
     pkthdr.len = 0;
-    stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
+    stream->write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
 
-    stream.flush();
+    stream->flush();
 }
 
 void
@@ -111,9 +111,9 @@ EtherDump::dumpPacket(EthPacketPtr &packet)
     pkthdr.microseconds = (curTick / Clock::Int::us) % ULL(1000000);
     pkthdr.caplen = std::min(packet->length, maxlen);
     pkthdr.len = packet->length;
-    stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
-    stream.write(reinterpret_cast<char *>(packet->data), pkthdr.caplen);
-    stream.flush();
+    stream->write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
+    stream->write(reinterpret_cast<char *>(packet->data), pkthdr.caplen);
+    stream->flush();
 }
 
 EtherDump *
index 1027ce4d01b9b01b9179120723ad087ce647870c..733e61c97db9a1636e1cd70a086251df42641c4f 100644 (file)
@@ -46,7 +46,7 @@
 class EtherDump : public SimObject
 {
   private:
-    std::ofstream stream;
+    std::ostream *stream;
     const int maxlen;
     void dumpPacket(EthPacketPtr &packet);
     void init();
index 0df85cc92c4bed11f8665cc1df03bc51b92cc96a..5bed44bd34211b86fc28dca128566fe67ac206bf 100644 (file)
@@ -379,13 +379,12 @@ Split::cleanupRefs()
     else if (lru_net)
         lru_net->cleanupRefs();
 
-    ofstream memPrint(simout.resolve("memory_footprint.txt").c_str(),
-                      ios::trunc);
+    ostream *memPrint = simout.create("memory_footprint.txt");
 
     // this shouldn't be here but it happens at the end, which is what i want
     memIter end = memHash.end();
     for (memIter iter = memHash.begin(); iter != end; ++iter) {
-        ccprintf(memPrint, "%8x\t%d\n", (*iter).first, (*iter).second);
+        ccprintf(*memPrint, "%8x\t%d\n", (*iter).first, (*iter).second);
     }
 }