summaryrefslogtreecommitdiff
path: root/src/base/output.cc
diff options
context:
space:
mode:
authorClint Smullen <cws3k@cs.virginia.edu>2008-11-15 23:42:11 -0500
committerClint Smullen <cws3k@cs.virginia.edu>2008-11-15 23:42:11 -0500
commit3087be945d84031d7d6a9cd45bd90d5767d1b7cc (patch)
treed31827399f743c8ab629e4e49125d9522b90282c /src/base/output.cc
parent4514f565e3dfe1de41bbaec05f3f0074e5299bac (diff)
downloadgem5-3087be945d84031d7d6a9cd45bd90d5767d1b7cc.tar.xz
Output: Include gzstream package to allow automatically-gzipped output
The gzstream package provides an ostream-interface for writing gzipped files. The package comes from: http://www.cs.unc.edu/Research/compgeom/gzstream/ And is distributed under the LGPL license. Both the license and version information has been preservered, though all other files in the package have been purged. Minor modifications to the code have been made. The output module detects when a filename ends in .gz and constructs an ogzstream object instead of an ofstream object. This works for both the create(...) and find(...) commands. Additionally, since gzstream objects needs to be closed to ensure proper file termination, I have the output deconstructor deleting all ostream's that it manages on behalf of find(...). At the moment, the only output file that I know this functionality works for is stats, i.e. by specifying "--stats-file=m5stats.txt.gz" on the command line.
Diffstat (limited to 'src/base/output.cc')
-rw-r--r--src/base/output.cc77
1 files changed, 55 insertions, 22 deletions
diff --git a/src/base/output.cc b/src/base/output.cc
index 5a0f2ea70..ea13e23d4 100644
--- a/src/base/output.cc
+++ b/src/base/output.cc
@@ -36,6 +36,8 @@
#include <fstream>
+#include <gzstream.hh>
+
#include "base/misc.hh"
#include "base/output.hh"
@@ -50,7 +52,45 @@ OutputDirectory::OutputDirectory()
{}
OutputDirectory::~OutputDirectory()
-{}
+{
+ for (map_t::iterator i = files.begin(); i != files.end(); i++) {
+ if (i->second)
+ delete i->second;
+ }
+}
+
+std::ostream *
+OutputDirectory::checkForStdio(const string &name) const
+{
+ if (name == "cerr" || name == "stderr")
+ return &cerr;
+
+ if (name == "cout" || name == "stdout")
+ return &cout;
+
+ return NULL;
+}
+
+ostream *
+OutputDirectory::openFile(const string &filename,
+ ios_base::openmode mode) const
+{
+ if (filename.find(".gz", filename.length()-3) < filename.length()) {
+ ogzstream *file = new ogzstream(filename.c_str(), mode);
+
+ if (!file->is_open())
+ fatal("Cannot open file %s", filename);
+
+ return file;
+ } else {
+ ofstream *file = new ofstream(filename.c_str(), mode);
+
+ if (!file->is_open())
+ fatal("Cannot open file %s", filename);
+
+ return file;
+ }
+}
void
OutputDirectory::setDirectory(const string &d)
@@ -66,7 +106,7 @@ OutputDirectory::setDirectory(const string &d)
}
const string &
-OutputDirectory::directory()
+OutputDirectory::directory() const
{
if (dir.empty())
panic("Output directory not set!");
@@ -74,8 +114,8 @@ OutputDirectory::directory()
return dir;
}
-string
-OutputDirectory::resolve(const string &name)
+inline string
+OutputDirectory::resolve(const string &name) const
{
return (name[0] != '/') ? dir + name : name;
}
@@ -83,16 +123,14 @@ OutputDirectory::resolve(const string &name)
ostream *
OutputDirectory::create(const string &name, bool binary)
{
- if (name == "cerr" || name == "stderr")
- return &cerr;
-
- if (name == "cout" || name == "stdout")
- return &cout;
+ ostream *file = checkForStdio(name);
+ if (file)
+ return file;
- ofstream *file = new ofstream(resolve(name).c_str(),
- ios::trunc | binary ? ios::binary : (ios::openmode)0);
- if (!file->is_open())
- fatal("Cannot open file %s", name);
+ string filename = resolve(name);
+ ios_base::openmode mode =
+ ios::trunc | binary ? ios::binary : (ios::openmode)0;
+ file = openFile(filename, mode);
return file;
}
@@ -100,21 +138,16 @@ OutputDirectory::create(const string &name, bool binary)
ostream *
OutputDirectory::find(const string &name)
{
- if (name == "cerr" || name == "stderr")
- return &cerr;
-
- if (name == "cout" || name == "stdout")
- return &cout;
+ ostream *file = checkForStdio(name);
+ if (file)
+ return file;
string filename = resolve(name);
map_t::iterator i = files.find(filename);
if (i != files.end())
return (*i).second;
- ofstream *file = new ofstream(filename.c_str(), ios::trunc);
- if (!file->is_open())
- fatal("Cannot open file %s", filename);
-
+ file = openFile(filename);
files[filename] = file;
return file;
}