SUMO - Simulation of Urban MObility
OutputDevice.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Static storage of an output device and its base (abstract) implementation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2004-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <map>
34 #include <fstream>
35 #include <sstream>
36 #include <string>
37 #include <iomanip>
38 #include "OutputDevice.h"
39 #include "OutputDevice_File.h"
40 #include "OutputDevice_COUT.h"
41 #include "OutputDevice_CERR.h"
42 #include "OutputDevice_Network.h"
43 #include "PlainXMLFormatter.h"
47 #include <utils/common/ToString.h>
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 // ===========================================================================
57 // static member definitions
58 // ===========================================================================
59 std::map<std::string, OutputDevice*> OutputDevice::myOutputDevices;
60 
61 
62 // ===========================================================================
63 // static method definitions
64 // ===========================================================================
66 OutputDevice::getDevice(const std::string& name) {
67  // check whether the device has already been aqcuired
68  if (myOutputDevices.find(name) != myOutputDevices.end()) {
69  return *myOutputDevices[name];
70  }
71  // build the device
72  OutputDevice* dev = 0;
73  // check whether the device shall print to stdout
74  if (name == "stdout") {
76  } else if (name == "stderr") {
78  } else if (FileHelpers::isSocket(name)) {
79  try {
80  int port = TplConvert::_2int(name.substr(name.find(":") + 1).c_str());
81  dev = new OutputDevice_Network(name.substr(0, name.find(":")), port);
82  } catch (NumberFormatException&) {
83  throw IOError("Given port number '" + name.substr(name.find(":") + 1) + "' is not numeric.");
84  } catch (EmptyData&) {
85  throw IOError("No port number given.");
86  }
87  } else {
88  const size_t len = name.length();
89  std::string name2 = name;
90  if (OptionsCont::getOptions().isSet("output-prefix")) {
91  std::string prefix = OptionsCont::getOptions().getString("output-prefix");
92  size_t metaTimeIndex = prefix.find("TIME");
93  if (metaTimeIndex != std::string::npos) {
94  time_t rawtime;
95  char buffer [80];
96  time(&rawtime);
97  strftime(buffer, 80, "%F-%H-%M-%S", localtime(&rawtime));
98  prefix.replace(metaTimeIndex, 4, std::string(buffer));
99  }
100  name2 = FileHelpers::prependToLastPathComponent(prefix, name);
101  }
102  dev = new OutputDevice_File(name2, len > 4 && name.substr(len - 4) == ".sbx");
103  }
104  dev->setPrecision();
105  dev->getOStream() << std::setiosflags(std::ios::fixed);
106  myOutputDevices[name] = dev;
107  return *dev;
108 }
109 
110 
111 bool
112 OutputDevice::createDeviceByOption(const std::string& optionName,
113  const std::string& rootElement,
114  const std::string& schemaFile) {
115  if (!OptionsCont::getOptions().isSet(optionName)) {
116  return false;
117  }
118  OutputDevice& dev = OutputDevice::getDevice(OptionsCont::getOptions().getString(optionName));
119  if (rootElement != "") {
120  if (schemaFile != "") {
121  dev.writeXMLHeader(rootElement, "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/" + schemaFile + "\"");
122  } else {
123  dev.writeXMLHeader(rootElement);
124  }
125  }
126  return true;
127 }
128 
129 
131 OutputDevice::getDeviceByOption(const std::string& optionName) {
132  std::string devName = OptionsCont::getOptions().getString(optionName);
133  if (myOutputDevices.find(devName) == myOutputDevices.end()) {
134  throw InvalidArgument("Device '" + devName + "' has not been created.");
135  }
136  return OutputDevice::getDevice(devName);
137 }
138 
139 
140 void
142  std::vector<OutputDevice*> errorDevices;
143  std::vector<OutputDevice*> nonErrorDevices;
144  for (std::map<std::string, OutputDevice*>::iterator i = myOutputDevices.begin(); i != myOutputDevices.end(); ++i) {
145  if (MsgHandler::getErrorInstance()->isRetriever(i->second)) {
146  errorDevices.push_back(i->second);
147  } else {
148  nonErrorDevices.push_back(i->second);
149  }
150  }
151  for (std::vector<OutputDevice*>::iterator i = nonErrorDevices.begin(); i != nonErrorDevices.end(); ++i) {
152  try {
153  (*i)->close();
154  } catch (const IOError& e) {
155  WRITE_ERROR("Error on closing output devices.");
156  WRITE_ERROR(e.what());
157  }
158  }
159  for (std::vector<OutputDevice*>::iterator i = errorDevices.begin(); i != errorDevices.end(); ++i) {
160  try {
161  (*i)->close();
162  } catch (const IOError& e) {
163  std::cerr << "Error on closing error output devices." << std::endl;
164  std::cerr << e.what() << std::endl;
165  }
166  }
167 }
168 
169 
170 std::string
171 OutputDevice::realString(const SUMOReal v, const int precision) {
172  std::ostringstream oss;
173  if (v == 0) {
174  return "0";
175  }
176  if (v < pow(10., -precision)) {
177  oss.setf(std::ios::scientific, std::ios::floatfield);
178  } else {
179  oss.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
180  oss.setf(std::ios::showpoint); // print decimal point
181  oss << std::setprecision(precision);
182  }
183  oss << v;
184  return oss.str();
185 }
186 
187 
188 // ===========================================================================
189 // member method definitions
190 // ===========================================================================
191 OutputDevice::OutputDevice(const bool binary, const unsigned int defaultIndentation)
192  : myAmBinary(binary) {
193  if (binary) {
195  } else {
196  myFormatter = new PlainXMLFormatter(defaultIndentation);
197  }
198 }
199 
200 
202  delete myFormatter;
203 }
204 
205 
206 bool
208  return getOStream().good();
209 }
210 
211 
212 void
214  while (closeTag()) {}
215  for (std::map<std::string, OutputDevice*>::iterator i = myOutputDevices.begin(); i != myOutputDevices.end(); ++i) {
216  if (i->second == this) {
217  myOutputDevices.erase(i);
218  break;
219  }
220  }
221  delete this;
222 }
223 
224 
225 void
226 OutputDevice::setPrecision(unsigned int precision) {
227  getOStream() << std::setprecision(precision);
228 }
229 
230 
231 bool
232 OutputDevice::writeXMLHeader(const std::string& rootElement,
233  const std::string& attrs, const std::string& comment) {
234  return myFormatter->writeXMLHeader(getOStream(), rootElement, attrs, comment);
235 }
236 
237 
239 OutputDevice::openTag(const std::string& xmlElement) {
240  myFormatter->openTag(getOStream(), xmlElement);
241  return *this;
242 }
243 
244 
246 OutputDevice::openTag(const SumoXMLTag& xmlElement) {
247  myFormatter->openTag(getOStream(), xmlElement);
248  return *this;
249 }
250 
251 
252 bool
254  if (myFormatter->closeTag(getOStream())) {
255  postWriteHook();
256  return true;
257  }
258  return false;
259 }
260 
261 
262 void
264 
265 
266 void
267 OutputDevice::inform(const std::string& msg, const char progress) {
268  if (progress != 0) {
269  getOStream() << msg << progress;
270  } else {
271  getOStream() << msg << '\n';
272  }
273  postWriteHook();
274 }
275 
276 
277 /****************************************************************************/
278 
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:191
void close()
Closes the device and removes it from the dictionary.
SumoXMLTag
Numbers representing SUMO-XML - element names.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual bool closeTag(std::ostream &into)=0
Closes the most recently opened tag.
static std::map< std::string, OutputDevice * > myOutputDevices
map from names to output devices
Definition: OutputDevice.h:342
static std::string prependToLastPathComponent(const std::string &prefix, const std::string &path)
prepend the given prefix to the last path component of the given file path
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:94
OutputDevice(const bool binary=false, const unsigned int defaultIndentation=0)
Constructor.
An output device for TCP/IP network connections.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void setPrecision(unsigned int precision=OUTPUT_ACCURACY)
Sets the precison or resets it to default.
Output formatter for plain XML output.
static void closeAll()
static OutputDevice * getDevice()
Returns the single cout instance.
An output device that encapsulates an ofstream.
virtual void openTag(std::ostream &into, const std::string &xmlElement)=0
Opens an XML tag.
virtual bool ok()
returns the information whether one can write into the device
Output formatter for plain XML output.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static int _2int(const E *const data)
Definition: TplConvert.h:114
virtual ~OutputDevice()
Destructor.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
static std::string realString(const SUMOReal v, const int precision=OUTPUT_ACCURACY)
Helper method for string formatting.
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:347
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="")
Creates the device using the output definition stored in the named option.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:218
virtual bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::string &attrs="", const std::string &comment="")=0
Writes an XML header with optional configuration.
static OutputDevice * getDevice()
Returns the single cerr instance.
virtual std::ostream & getOStream()=0
Returns the associated ostream.
virtual void postWriteHook()
Called after every write access.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.