From f267dc4a8729179ed99ec591af2ba434cb644755 Mon Sep 17 00:00:00 2001 From: Steve Reinhardt Date: Tue, 19 Oct 2004 20:00:20 -0400 Subject: Clean up/standardize handling of various output files. No more non-intuitive behavior shifts depending on whether outputDirectory is set (at the expense of backwards compatibility). outputDirectory is now always valid, defaults to ".". dev/etherdump.cc: Use makeOutputStream() to create output file. New behavior: actually complain if dump file can't be opened, instead of quietly ignoring the problem. dev/etherdump.hh: dev/simconsole.cc: dev/simconsole.hh: Use makeOutputStream() to create output file. sim/builder.cc: sim/builder.hh: sim/main.cc: builderStream() is now *configStream. sim/serialize.cc: outputDirectory is now always valid, no need to check. sim/universe.cc: Clean up/standardize handling of various output files. No more non-intuitive behavior shifts depending on whether outputDirectory is set (at the expense of backwards compatibility). outputDirectory is now always valid, defaults to ".". New function makeOutputStream() does "the right thing" to associate a stream with a filename. --HG-- extra : convert_revision : a03c58c547221b3906e0d6f55e4a569843f2d646 --- dev/etherdump.cc | 39 ++++++++++----------------------------- dev/etherdump.hh | 6 +++--- dev/simconsole.cc | 31 +++++++++++++------------------ dev/simconsole.hh | 2 +- 4 files changed, 27 insertions(+), 51 deletions(-) (limited to 'dev') diff --git a/dev/etherdump.cc b/dev/etherdump.cc index 27817d456..485d5599c 100644 --- a/dev/etherdump.cc +++ b/dev/etherdump.cc @@ -42,11 +42,9 @@ using std::string; -EtherDump::EtherDump(const string &name, const string &file, int max) - : SimObject(name), maxlen(max) +EtherDump::EtherDump(const string &name, std::ostream *_stream, int max) + : SimObject(name), stream(_stream), maxlen(max) { - if (!file.empty()) - stream.open(file.c_str()); } #define DLT_EN10MB 1 // Ethernet (10Mb) @@ -74,9 +72,6 @@ struct pcap_pkthdr { void EtherDump::init() { - if (!stream.is_open()) - return; - curtime = time(NULL); s_freq = ticksPerSecond; us_freq = ticksPerSecond / ULL(1000000); @@ -91,7 +86,7 @@ EtherDump::init() hdr.sigfigs = 0; hdr.linktype = DLT_EN10MB; - stream.write(reinterpret_cast(&hdr), sizeof(hdr)); + stream->write(reinterpret_cast(&hdr), sizeof(hdr)); /* * output an empty packet with the current time so that we know @@ -103,9 +98,9 @@ EtherDump::init() pkthdr.microseconds = 0; pkthdr.caplen = 0; pkthdr.len = 0; - stream.write(reinterpret_cast(&pkthdr), sizeof(pkthdr)); + stream->write(reinterpret_cast(&pkthdr), sizeof(pkthdr)); - stream.flush(); + stream->flush(); } void @@ -116,9 +111,9 @@ EtherDump::dumpPacket(PacketPtr &packet) pkthdr.microseconds = (curTick / us_freq) % ULL(1000000); pkthdr.caplen = std::min(packet->length, maxlen); pkthdr.len = packet->length; - stream.write(reinterpret_cast(&pkthdr), sizeof(pkthdr)); - stream.write(reinterpret_cast(packet->data), pkthdr.caplen); - stream.flush(); + stream->write(reinterpret_cast(&pkthdr), sizeof(pkthdr)); + stream->write(reinterpret_cast(packet->data), pkthdr.caplen); + stream->flush(); } BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherDump) @@ -130,28 +125,14 @@ END_DECLARE_SIM_OBJECT_PARAMS(EtherDump) BEGIN_INIT_SIM_OBJECT_PARAMS(EtherDump) - INIT_PARAM(file, "file to dump packets to"), + INIT_PARAM_DFLT(file, "file to dump packets to", "etherdump"), INIT_PARAM_DFLT(maxlen, "max portion of packet data to dump", 96) END_INIT_SIM_OBJECT_PARAMS(EtherDump) CREATE_SIM_OBJECT(EtherDump) { - string filename; - if (file.isValid()) { - filename = file; - - if (filename[0] != '/' && !outputDirectory.empty()) - filename = outputDirectory + filename; - } else { - if (outputDirectory.empty()) { - filename = "etherdump"; - } else { - filename = outputDirectory + "etherdump"; - } - } - - return new EtherDump(getInstanceName(), filename, maxlen); + return new EtherDump(getInstanceName(), makeOutputStream(file), maxlen); } REGISTER_SIM_OBJECT("EtherDump", EtherDump) diff --git a/dev/etherdump.hh b/dev/etherdump.hh index 62364359e..b127d05e2 100644 --- a/dev/etherdump.hh +++ b/dev/etherdump.hh @@ -43,7 +43,7 @@ class EtherDump : public SimObject { private: - std::ofstream stream; + std::ostream *stream; const int maxlen; void dumpPacket(PacketPtr &packet); void init(); @@ -53,9 +53,9 @@ class EtherDump : public SimObject Tick us_freq; public: - EtherDump(const std::string &name, const std::string &file, int max); + EtherDump(const std::string &name, std::ostream *_stream, int max); - inline void dump(PacketPtr &pkt) { if (stream.is_open()) dumpPacket(pkt); } + inline void dump(PacketPtr &pkt) { dumpPacket(pkt); } }; #endif // __ETHERDUMP_H__ diff --git a/dev/simconsole.cc b/dev/simconsole.cc index a15057402..b2afb3f84 100644 --- a/dev/simconsole.cc +++ b/dev/simconsole.cc @@ -72,27 +72,22 @@ SimConsole::Event::process(int revent) cons->detach(); } -SimConsole::SimConsole(const string &name, const string &file, int num) +SimConsole::SimConsole(const string &name, std::ostream *os, int num) : SimObject(name), event(NULL), number(num), in_fd(-1), out_fd(-1), - listener(NULL), txbuf(16384), rxbuf(16384), outfile(NULL) + listener(NULL), txbuf(16384), rxbuf(16384), outfile(os) #if TRACING_ON == 1 , linebuf(16384) #endif { - if (!file.empty()) - outfile = new ofstream(file.c_str()); - if (outfile) outfile->setf(ios::unitbuf); - } SimConsole::~SimConsole() { close(); - if (outfile) - delete outfile; + closeOutputStream(outfile); } void @@ -311,7 +306,7 @@ BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole) INIT_PARAM(listener, "console listener"), INIT_PARAM(intr_control, "interrupt controller"), - INIT_PARAM_DFLT(output, "file to dump output to", ""), + INIT_PARAM(output, "file to dump output to"), INIT_PARAM_DFLT(append_name, "append name() to filename", true), INIT_PARAM_DFLT(number, "console number", 0) @@ -319,18 +314,18 @@ END_INIT_SIM_OBJECT_PARAMS(SimConsole) CREATE_SIM_OBJECT(SimConsole) { - string filename = output; - if (filename.empty()) { - if (!outputDirectory.empty()) - filename = outputDirectory + getInstanceName(); + string filename; + + if (!output.isValid()) { + filename = getInstanceName(); + } else if (append_name) { + filename = (string)output + "." + getInstanceName(); } else { - if (append_name) - filename += "." + getInstanceName(); - if (!outputDirectory.empty()) - filename = outputDirectory + filename; + filename = output; } - SimConsole *console = new SimConsole(getInstanceName(), filename, number); + SimConsole *console = new SimConsole(getInstanceName(), + makeOutputStream(filename), number); ((ConsoleListener *)listener)->add(console); return console; diff --git a/dev/simconsole.hh b/dev/simconsole.hh index 138e2e36a..c5a281834 100644 --- a/dev/simconsole.hh +++ b/dev/simconsole.hh @@ -70,7 +70,7 @@ class SimConsole : public SimObject ConsoleListener *listener; public: - SimConsole(const std::string &name, const std::string &file, int num); + SimConsole(const std::string &name, std::ostream *os, int num); ~SimConsole(); protected: -- cgit v1.2.3