Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2004-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file OutputDevice.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @date 2004
19 : ///
20 : // Static storage of an output device and its base (abstract) implementation
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <map>
25 : #include <fstream>
26 : #include <sstream>
27 : #include <string>
28 : #include <iomanip>
29 : #ifdef WIN32
30 : #define NOMINMAX
31 : #include <windows.h>
32 : #undef NOMINMAX
33 : #endif
34 : #include "OutputDevice.h"
35 : #include "OutputDevice_File.h"
36 : #include "OutputDevice_COUT.h"
37 : #include "OutputDevice_CERR.h"
38 : #include "OutputDevice_Network.h"
39 : #include "PlainXMLFormatter.h"
40 : #include <utils/common/StringUtils.h>
41 : #include <utils/common/UtilExceptions.h>
42 : #include <utils/common/FileHelpers.h>
43 : #include <utils/common/ToString.h>
44 : #include <utils/common/MsgHandler.h>
45 : #include <utils/options/OptionsCont.h>
46 : #include <utils/options/OptionsIO.h>
47 :
48 :
49 : // ===========================================================================
50 : // static member definitions
51 : // ===========================================================================
52 : std::map<std::string, OutputDevice*> OutputDevice::myOutputDevices;
53 : int OutputDevice::myPrevConsoleCP = -1;
54 :
55 :
56 : // ===========================================================================
57 : // static method definitions
58 : // ===========================================================================
59 : OutputDevice&
60 23600861 : OutputDevice::getDevice(const std::string& name, bool usePrefix) {
61 : #ifdef WIN32
62 : // fix the windows console output on first call
63 : if (myPrevConsoleCP == -1) {
64 : myPrevConsoleCP = GetConsoleOutputCP();
65 : SetConsoleOutputCP(CP_UTF8);
66 : }
67 : #endif
68 : // check whether the device has already been aqcuired
69 23600861 : if (myOutputDevices.find(name) != myOutputDevices.end()) {
70 23303329 : return *myOutputDevices[name];
71 : }
72 : // build the device
73 297532 : const OptionsCont& oc = OptionsCont::getOptions();
74 297532 : const int len = (int)name.length();
75 950624 : bool isParquet = (oc.exists("output.format") && oc.getString("output.format") == "parquet") || (len > 8 && name.substr(len - 8) == ".parquet");
76 : #ifndef HAVE_PARQUET
77 : if (isParquet) {
78 : WRITE_WARNING("Compiled without Parquet support, falling back to XML.")
79 : isParquet = false;
80 : }
81 : #endif
82 : OutputDevice* dev = nullptr;
83 : // check whether the device shall print to stdout
84 297532 : if (name == "stdout") {
85 82953 : dev = OutputDevice_COUT::getDevice();
86 214579 : } else if (name == "stderr") {
87 127484 : dev = OutputDevice_CERR::getDevice();
88 87095 : } else if (FileHelpers::isSocket(name)) {
89 : try {
90 3 : const bool ipv6 = name[0] == '['; // IPv6 addresses may be written like '[::1]:8000'
91 3 : const size_t sepIndex = name.find(":", ipv6 ? name.find("]") : 0);
92 3 : const int port = StringUtils::toInt(name.substr(sepIndex + 1));
93 6 : dev = new OutputDevice_Network(ipv6 ? name.substr(1, sepIndex - 2) : name.substr(0, sepIndex), port);
94 0 : } catch (NumberFormatException&) {
95 0 : throw IOError("Given port number '" + name.substr(name.find(":") + 1) + "' is not numeric.");
96 0 : } catch (EmptyData&) {
97 0 : throw IOError(TL("No port number given."));
98 0 : }
99 : } else {
100 87092 : std::string name2 = (name == "nul" || name == "NUL") ? "/dev/null" : name;
101 174190 : if (usePrefix && oc.isSet("output-prefix") && name2 != "/dev/null") {
102 2192 : std::string prefix = oc.getString("output-prefix");
103 : const std::string::size_type metaTimeIndex = prefix.find("TIME");
104 1096 : if (metaTimeIndex != std::string::npos) {
105 0 : const time_t rawtime = std::chrono::system_clock::to_time_t(OptionsIO::getLoadTime());
106 : char buffer [80];
107 0 : struct tm* timeinfo = localtime(&rawtime);
108 0 : strftime(buffer, 80, "%Y-%m-%d-%H-%M-%S", timeinfo);
109 0 : prefix.replace(metaTimeIndex, 4, buffer);
110 : }
111 2192 : name2 = FileHelpers::prependToLastPathComponent(prefix, name);
112 : }
113 174184 : if (usePrefix && oc.isSet("output-suffix") && name2 != "/dev/null") {
114 132 : std::string suffix = oc.getString("output-suffix");
115 : const std::string::size_type metaTimeIndex = suffix.find("TIME");
116 66 : if (metaTimeIndex != std::string::npos) {
117 0 : const time_t rawtime = std::chrono::system_clock::to_time_t(OptionsIO::getLoadTime());
118 : char buffer [80];
119 0 : struct tm* timeinfo = localtime(&rawtime);
120 0 : strftime(buffer, 80, "%Y-%m-%d-%H-%M-%S", timeinfo);
121 0 : suffix.replace(metaTimeIndex, 4, buffer);
122 : }
123 132 : name2 = FileHelpers::appendBeforeExtension(name2, suffix);
124 : }
125 87176 : name2 = StringUtils::substituteEnvironment(name2, &OptionsIO::getLoadTime());
126 87092 : dev = new OutputDevice_File(name2, isParquet);
127 : }
128 1270613 : if ((oc.exists("output.format") && oc.getString("output.format") == "csv") || (len > 4 && name.substr(len - 4) == ".csv") || (len > 7 && name.substr(len - 7) == ".csv.gz")) {
129 84 : dev->setFormatter(new CSVFormatter(oc.getString("output.column-header"), oc.getString("output.column-separator")[0]));
130 : }
131 : #ifdef HAVE_PARQUET
132 297448 : if (isParquet) {
133 138 : dev->setFormatter(new ParquetFormatter(oc.getString("output.column-header"), oc.getString("output.compression")));
134 : }
135 : #endif
136 297448 : dev->setPrecision();
137 297448 : dev->getOStream() << std::setiosflags(std::ios::fixed);
138 594510 : dev->myWriteMetadata = oc.exists("write-metadata") && oc.getBool("write-metadata");
139 297448 : myOutputDevices[name] = dev;
140 297448 : return *dev;
141 : }
142 :
143 :
144 : bool
145 969134 : OutputDevice::createDeviceByOption(const std::string& optionName,
146 : const std::string& rootElement,
147 : const std::string& schemaFile,
148 : const int maximumDepth) {
149 969134 : if (!OptionsCont::getOptions().isSet(optionName)) {
150 : return false;
151 : }
152 50856 : OutputDevice& dev = OutputDevice::getDevice(OptionsCont::getOptions().getString(optionName));
153 25406 : dev.setExpectedAttributes(0, maximumDepth);
154 25406 : if (rootElement != "") {
155 50810 : dev.writeXMLHeader(rootElement, schemaFile);
156 : }
157 : return true;
158 : }
159 :
160 :
161 : OutputDevice&
162 22973584 : OutputDevice::getDeviceByOption(const std::string& optionName) {
163 22973584 : std::string devName = OptionsCont::getOptions().getString(optionName);
164 22973584 : if (myOutputDevices.find(devName) == myOutputDevices.end()) {
165 0 : throw InvalidArgument("Output device '" + devName + "' for option '" + optionName + "' has not been created.");
166 : }
167 45947168 : return OutputDevice::getDevice(devName);
168 : }
169 :
170 :
171 : void
172 0 : OutputDevice::flushAll() {
173 0 : for (auto item : myOutputDevices) {
174 : item.second->flush();
175 : }
176 0 : }
177 :
178 :
179 : void
180 168901 : OutputDevice::closeAll(bool keepErrorRetrievers) {
181 : std::vector<OutputDevice*> errorDevices;
182 : std::vector<OutputDevice*> nonErrorDevices;
183 498551 : for (std::map<std::string, OutputDevice*>::iterator i = myOutputDevices.begin(); i != myOutputDevices.end(); ++i) {
184 329650 : if (MsgHandler::getErrorInstance()->isRetriever(i->second)) {
185 169319 : errorDevices.push_back(i->second);
186 : } else {
187 160331 : nonErrorDevices.push_back(i->second);
188 : }
189 : }
190 329232 : for (OutputDevice* const dev : nonErrorDevices) {
191 : try {
192 160331 : dev->close();
193 0 : } catch (const IOError& e) {
194 0 : WRITE_ERROR(TL("Error on closing output devices."));
195 0 : WRITE_ERROR(e.what());
196 0 : }
197 : }
198 168901 : if (!keepErrorRetrievers) {
199 255188 : for (OutputDevice* const dev : errorDevices) {
200 : try {
201 127735 : dev->close();
202 0 : } catch (const IOError& e) {
203 : std::cerr << "Error on closing error output devices." << std::endl;
204 0 : std::cerr << e.what() << std::endl;
205 0 : }
206 : }
207 : #ifdef WIN32
208 : if (myPrevConsoleCP != -1) {
209 : SetConsoleOutputCP(myPrevConsoleCP);
210 : }
211 : #endif
212 : }
213 168901 : }
214 :
215 :
216 : // ===========================================================================
217 : // member method definitions
218 : // ===========================================================================
219 1753895 : OutputDevice::OutputDevice(const int defaultIndentation, const std::string& filename) :
220 1753895 : myFilename(filename), myFormatter(new PlainXMLFormatter(defaultIndentation)) {
221 1753895 : }
222 :
223 :
224 1753560 : OutputDevice::~OutputDevice() {
225 1753560 : delete myFormatter;
226 1753560 : }
227 :
228 :
229 : bool
230 0 : OutputDevice::ok() {
231 0 : return getOStream().good();
232 : }
233 :
234 :
235 : const std::string&
236 0 : OutputDevice::getFilename() {
237 0 : return myFilename;
238 : }
239 :
240 : void
241 297354 : OutputDevice::close() {
242 680405 : while (closeTag()) {}
243 401317 : for (std::map<std::string, OutputDevice*>::iterator i = myOutputDevices.begin(); i != myOutputDevices.end(); ++i) {
244 401317 : if (i->second == this) {
245 : myOutputDevices.erase(i);
246 : break;
247 : }
248 : }
249 297354 : MsgHandler::removeRetrieverFromAllInstances(this);
250 297354 : delete this;
251 297354 : }
252 :
253 :
254 : void
255 11406044 : OutputDevice::setPrecision(int precision) {
256 11406044 : getOStream() << std::setprecision(precision);
257 11406044 : }
258 :
259 :
260 : bool
261 96887 : OutputDevice::writeXMLHeader(const std::string& rootElement,
262 : const std::string& schemaFile,
263 : std::map<SumoXMLAttr, std::string> attrs,
264 : bool includeConfig) {
265 96887 : if (schemaFile != "") {
266 92672 : attrs[SUMO_ATTR_XMLNS] = "http://www.w3.org/2001/XMLSchema-instance";
267 92672 : attrs[SUMO_ATTR_SCHEMA_LOCATION] = "http://sumo.dlr.de/xsd/" + schemaFile;
268 : }
269 96887 : return myFormatter->writeXMLHeader(getOStream(), rootElement, attrs, myWriteMetadata, includeConfig);
270 : }
271 :
272 :
273 : OutputDevice&
274 7440496 : OutputDevice::openTag(const std::string& xmlElement) {
275 7440496 : myFormatter->openTag(getOStream(), xmlElement);
276 7440496 : return *this;
277 : }
278 :
279 :
280 : OutputDevice&
281 14062431 : OutputDevice::openTag(const SumoXMLTag& xmlElement) {
282 14062431 : myFormatter->openTag(getOStream(), xmlElement);
283 14062431 : return *this;
284 : }
285 :
286 :
287 : bool
288 21886521 : OutputDevice::closeTag(const std::string& comment) {
289 21886521 : if (myFormatter->closeTag(getOStream(), comment)) {
290 21572725 : postWriteHook();
291 21572724 : return true;
292 : }
293 : return false;
294 : }
295 :
296 :
297 : void
298 25233238 : OutputDevice::postWriteHook() {}
299 :
300 :
301 : void
302 4954354 : OutputDevice::inform(const std::string& msg, const bool progress) {
303 4954354 : if (progress) {
304 32659 : getOStream() << msg;
305 : } else {
306 9843390 : getOStream() << msg << '\n';
307 : }
308 4954354 : postWriteHook();
309 4954354 : }
310 :
311 :
312 : const SumoXMLAttrMask
313 25998 : OutputDevice::parseWrittenAttributes(const std::vector<std::string>& attrList, const std::string& desc, const std::map<std::string, SumoXMLAttrMask>& special) {
314 25998 : SumoXMLAttrMask result;
315 32538 : for (std::string attrName : attrList) {
316 6540 : if (attrName == "all") {
317 : result.set();
318 : } else if (special.count(attrName) > 0) {
319 : result |= special.find(attrName)->second;
320 : } else {
321 : if (SUMOXMLDefinitions::Attrs.hasString(attrName)) {
322 6478 : int attrNr = SUMOXMLDefinitions::Attrs.get(attrName);
323 6478 : if (attrNr < (int)result.size()) {
324 6468 : result.set(attrNr);
325 : } else {
326 30 : WRITE_ERRORF(TL("Attribute '%' is not support for filtering written attributes in %."), attrName, desc);
327 : }
328 : } else {
329 0 : WRITE_ERRORF(TL("Unknown attribute '%' to write in %."), attrName, desc);
330 : }
331 : }
332 : }
333 25998 : return result;
334 : }
335 :
336 :
337 : /****************************************************************************/
|