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