| 1 | /* |
| 2 | * Copyright (C) 2016 Edward d'Auvergne |
| 3 | * |
| 4 | * This file is part of the program FlightGear. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | #include <iomanip> |
| 22 | |
| 23 | #include "logging.hxx" |
| 24 | |
| 25 | #include <simgear/debug/logstream.hxx> |
| 26 | #include <simgear/scene/util/OsgIoCapture.hxx> |
| 27 | |
| 28 | |
| 29 | // The global stream capture data structure. |
| 30 | static capturedIO *_iostreams = NULL; |
| 31 | |
| 32 | |
| 33 | // capturedIO constructor. |
| 34 | capturedIO::capturedIO(sgDebugClass c, sgDebugPriority p) |
| 35 | { |
| 36 | callback_interleaved = new StreamLogCallback(sg_interleaved, c, p, false); |
| 37 | callback_bulk_only = new StreamLogCallback(sg_bulk_only, c, SG_BULK, true); |
| 38 | callback_debug_only = new StreamLogCallback(sg_debug_only, c, SG_DEBUG, true); |
| 39 | callback_info_only = new StreamLogCallback(sg_info_only, c, SG_INFO, true); |
| 40 | callback_warn_only = new StreamLogCallback(sg_warn_only, c, SG_WARN, true); |
| 41 | callback_alert_only = new StreamLogCallback(sg_alert_only, c, SG_ALERT, true); |
| 42 | |
| 43 | // Store the class as a string. |
| 44 | if (c == SG_ALL) |
| 45 | log_class = "SG_ALL"; |
| 46 | else { |
| 47 | std::stringstream stream; |
| 48 | stream << "0x" << std::right << std::setfill('0') << std::setw(8) << std::hex << c; |
| 49 | log_class = stream.str(); |
| 50 | } |
| 51 | |
| 52 | // Store the priority as a string. |
| 53 | if (p == SG_BULK) |
| 54 | log_priority = "SG_BULK"; |
| 55 | else if (p == SG_DEBUG) |
| 56 | log_priority = "SG_DEBUG"; |
| 57 | else if (p == SG_INFO) |
| 58 | log_priority = "SG_INFO"; |
| 59 | else if (p == SG_WARN) |
| 60 | log_priority = "SG_WARN"; |
| 61 | else if (p == SG_ALERT) |
| 62 | log_priority = "SG_ALERT"; |
| 63 | else if (p == SG_POPUP) |
| 64 | log_priority = "SG_POPUP"; |
| 65 | else if (p == SG_DEV_WARN) |
| 66 | log_priority = "SG_DEV_WARN"; |
| 67 | else if (p == SG_DEV_ALERT) |
| 68 | log_priority = "SG_DEV_ALERT"; |
| 69 | } |
| 70 | |
| 71 | // capturedIO destructor. |
| 72 | capturedIO::~capturedIO() |
| 73 | { |
| 74 | // Destroy the callback objects. |
| 75 | delete callback_interleaved; |
| 76 | delete callback_bulk_only; |
| 77 | delete callback_debug_only; |
| 78 | delete callback_info_only; |
| 79 | delete callback_warn_only; |
| 80 | delete callback_alert_only; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | // Return the global stream capture data structure, creating it if needed. |
| 85 | capturedIO & getIOstreams(sgDebugClass c, sgDebugPriority p) |
| 86 | { |
| 87 | // Initialise the global stream capture data structure, if needed. |
| 88 | if (!_iostreams) |
| 89 | _iostreams = new capturedIO(c, p); |
| 90 | |
| 91 | // Return a pointer to the global object. |
| 92 | return *_iostreams; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | // Set up to capture all the simgear logging priorities as separate streams. |
| 97 | void setupLogging(sgDebugClass c, sgDebugPriority p, bool split) |
| 98 | { |
| 99 | // Get the single logstream instance. |
| 100 | logstream &log = sglog(); |
| 101 | |
| 102 | // Set up the logstream testing mode. |
| 103 | log.setTestingMode(true); |
| 104 | |
| 105 | // OSG IO capture. |
| 106 | osg::setNotifyHandler(new SGNotifyHandler); |
| 107 | |
| 108 | // IO capture. |
| 109 | capturedIO &obj = getIOstreams(c, p); |
| 110 | if (!split) |
| 111 | log.addCallback(obj.callback_interleaved); |
| 112 | else { |
| 113 | if (p <= SG_BULK) |
| 114 | log.addCallback(obj.callback_bulk_only); |
| 115 | if (p <= SG_DEBUG) |
| 116 | log.addCallback(obj.callback_debug_only); |
| 117 | if (p <= SG_INFO) |
| 118 | log.addCallback(obj.callback_info_only); |
| 119 | if (p <= SG_WARN) |
| 120 | log.addCallback(obj.callback_warn_only); |
| 121 | if (p <= SG_ALERT) |
| 122 | log.addCallback(obj.callback_alert_only); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | // Deactivate all the simgear logging priority IO captures. |
| 128 | void stopLogging() |
| 129 | { |
| 130 | // Get the single logstream instance. |
| 131 | logstream &log = sglog(); |
| 132 | |
| 133 | // IO decapture. |
| 134 | capturedIO &obj = getIOstreams(); |
| 135 | log.removeCallback(obj.callback_interleaved); |
| 136 | log.removeCallback(obj.callback_bulk_only); |
| 137 | log.removeCallback(obj.callback_debug_only); |
| 138 | log.removeCallback(obj.callback_info_only); |
| 139 | log.removeCallback(obj.callback_warn_only); |
| 140 | log.removeCallback(obj.callback_alert_only); |
| 141 | |
| 142 | // Clean up the IO stream object. |
| 143 | delete _iostreams; |
| 144 | _iostreams = NULL; |
| 145 | |
| 146 | // Stop the simgear logstream. |
| 147 | simgear::shutdownLogging(); |
| 148 | } |