Line data Source code
1 : //# MemoryLogSink.h: save distributed log messages 2 : //# Copyright (C) 2005 3 : //# Associated Universities, Inc. Washington DC, USA. 4 : //# 5 : //# This library is free software; you can redistribute it and/or modify it 6 : //# under the terms of the GNU Library General Public License as published by 7 : //# the Free Software Foundation; either version 2 of the License, or (at your 8 : //# option) any later version. 9 : //# 10 : //# This library is distributed in the hope that it will be useful, but WITHOUT 11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 13 : //# License for more details. 14 : //# 15 : //# You should have received a copy of the GNU Library General Public License 16 : //# along with this library; if not, write to the Free Software Foundation, 17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 18 : //# 19 : //# Correspondence concerning AIPS++ should be addressed as follows: 20 : //# Internet email: aips2-request@nrao.edu. 21 : //# Postal address: AIPS++ Project Office 22 : //# National Radio Astronomy Observatory 23 : //# 520 Edgemont Road 24 : //# Charlottesville, VA 22903-2475 USA 25 : //# 26 : //# $Id$ 27 : 28 : #include <casacore/casa/System/Aipsrc.h> 29 : #include <stdcasa/TSLogSink.h> 30 : #include <casacore/casa/Logging/LogFilter.h> 31 : #include <casacore/casa/Logging/StreamLogSink.h> 32 : #include <casacore/casa/Exceptions/Error.h> 33 : #include <casacore/casa/Utilities/Assert.h> 34 : #include <casacore/casa/OS/Time.h> 35 : #include <casacore/casa/Quanta/MVTime.h> 36 : 37 : using namespace casacore; 38 : namespace casa { //# NAMESPACE CASA - BEGIN 39 : 40 0 : String TSLogSink::localId( ) { 41 0 : return String("TSLogSink"); 42 : } 43 : 44 0 : String TSLogSink::id( ) const { 45 0 : return String("TSLogSink"); 46 : } 47 : 48 1 : TSLogSink::TSLogSink(const std::string &path) 49 1 : : LogSinkInterface() 50 : { 51 : #ifdef AIPS_LOG4CPLUS 52 : BasicConfigurator config; 53 : config.configure(); 54 : #else 55 1 : logsink = 0 ; 56 1 : logfile = 0 ; 57 1 : send2cerr = false; 58 1 : setLogSink(path); 59 : #endif 60 1 : } 61 : 62 0 : TSLogSink::TSLogSink (LogMessage::Priority filter) 63 0 : : LogSinkInterface(LogFilter(filter)) 64 : { 65 : #ifndef AIPS_LOG4CPLUS 66 0 : logsink = 0 ; 67 0 : logfile = 0 ; 68 0 : setLogSink(); 69 0 : send2cerr = false; 70 : #endif 71 0 : } 72 : 73 0 : TSLogSink::TSLogSink (const LogFilterInterface& filter) 74 0 : : LogSinkInterface(filter) 75 : { 76 : #ifndef AIPS_LOG4CPLUS 77 0 : logsink = 0 ; 78 0 : logfile = 0 ; 79 0 : setLogSink(); 80 0 : send2cerr = false; 81 : #endif 82 0 : } 83 : 84 : #ifndef AIPS_LOG4CPLUS 85 : // 86 : // if you supply a logname it uses that, otherwise it looks for a logname in 87 : // the aiprc file, else it defaults to the old standby. 88 : // 89 330 : void TSLogSink::setLogSink(String logname){ 90 : 91 330 : if(!logname.size()){ 92 0 : String logfileKey="user.logfile"; 93 0 : String logname2; 94 0 : if(!Aipsrc::find(logname2, logfileKey)){ 95 0 : logname = "casa.log"; 96 : } else { 97 0 : logname = logname2; 98 : } 99 : } 100 330 : if (logfile) { 101 329 : logfile->close() ; 102 329 : delete logfile ; 103 : } 104 330 : logfile = new std::ofstream(logname.c_str(), ios::app); 105 330 : if(logfile) { 106 330 : delete logsink ; 107 330 : logsink = new StreamLogSink(LogMessage::NORMAL, logfile); 108 : } 109 330 : if(!logsink) 110 0 : cerr << "Unable to log to " << logname << endl; 111 330 : return; 112 : } 113 : #endif 114 : 115 0 : TSLogSink::TSLogSink (const TSLogSink& other) 116 0 : : LogSinkInterface (other) 117 : { 118 : //copy_other (other); 119 0 : LogSinkInterface::operator= (other); 120 0 : } 121 : 122 0 : TSLogSink& TSLogSink::operator= (const TSLogSink& other) 123 : { 124 0 : if (this != &other) { 125 0 : copy_other (other); 126 : } 127 0 : return *this; 128 : } 129 : 130 0 : void TSLogSink::copy_other (const TSLogSink& other) 131 : { 132 0 : LogSinkInterface::operator= (other); 133 0 : } 134 : 135 0 : TSLogSink::~TSLogSink() 136 : { 137 : #ifndef AIPS_LOG4CPLUS 138 0 : logfile->close(); 139 0 : delete logfile; 140 0 : delete logsink; 141 : #endif 142 0 : } 143 : 144 7121823 : const LogFilterInterface& TSLogSink::filter () const{return logsink->filter();} 145 : 146 477 : LogSinkInterface& TSLogSink::filter (const LogFilterInterface& newfilter) 147 : { 148 477 : LogSinkInterface::filter(newfilter); 149 477 : return logsink->filter(newfilter); 150 : } 151 : 152 1517176 : Bool TSLogSink::postLocally (const LogMessage& message) 153 : { 154 1517176 : Bool posted = false; 155 1517176 : if (this->filter().pass(message)) { 156 1325280 : String tmp; 157 662640 : message.origin().objectID().toString(tmp); 158 1325280 : LogOrigin theOrigin(message.origin()); 159 662640 : if(!message.origin().taskName().length()){ 160 424704 : theOrigin.taskName(LogSinkInterface::taskName); 161 : } else { 162 237936 : theOrigin.taskName(message.origin().taskName()); 163 : } 164 662640 : logsink->setTaskName(theOrigin.taskName()); 165 662640 : const_cast<LogMessage &>(message).origin(theOrigin); 166 : 167 : 168 : #ifdef AIPS_LOG4CPLUS 169 : switch (message.priority()) 170 : { 171 : case LogMessage::DEBUGGING: 172 : LOG4CPLUS_DEBUG(this, tmp); 173 : break; 174 : 175 : case LogMessage::NORMAL: 176 : LOG4CPLUS_INFO(this, tmp); 177 : break; 178 : 179 : case LogMessage::WARN: 180 : LOG4CPLUS_WARN(this, tmp); 181 : break; 182 : 183 : case LogMessage::SEVERE: 184 : this, tmp); 185 : break; 186 : 187 : default: 188 : } 189 : #else 190 662640 : logsink->postLocally(message); 191 662640 : posted = true; 192 662640 : switch (message.priority()) 193 : { 194 4601 : case LogMessage::DEBUGGING: 195 4601 : if(send2cerr) 196 0 : std::cerr << message.toString() << std::endl; 197 4601 : break; 198 128275 : case LogMessage::DEBUG1: 199 128275 : if(send2cerr) 200 0 : std::cerr << message.toString() << std::endl; 201 128275 : break; 202 22014 : case LogMessage::DEBUG2: 203 22014 : if(send2cerr) 204 0 : std::cerr << message.toString() << std::endl; 205 22014 : break; 206 431433 : case LogMessage::NORMAL: 207 431433 : if(send2cerr) 208 0 : std::cerr << message.toString() << std::endl; 209 431433 : break; 210 9554 : case LogMessage::NORMAL1: 211 9554 : if(send2cerr) 212 0 : std::cerr << message.toString() << std::endl; 213 9554 : break; 214 16618 : case LogMessage::NORMAL2: 215 16618 : if(send2cerr) 216 0 : std::cerr << message.toString() << std::endl; 217 16618 : break; 218 43494 : case LogMessage::NORMAL3: 219 43494 : if(send2cerr) 220 0 : std::cerr << message.toString() << std::endl; 221 43494 : break; 222 96 : case LogMessage::NORMAL4: 223 96 : if(send2cerr) 224 0 : std::cerr << message.toString() << std::endl; 225 96 : break; 226 0 : case LogMessage::NORMAL5: 227 0 : if(send2cerr) 228 0 : std::cerr << message.toString() << std::endl; 229 0 : break; 230 4941 : case LogMessage::WARN: 231 : // if(send2cerr) // jagonzal: casalog.showconsole should control also if WARN/SEVERE msg go to console 232 4941 : std::cerr << message.toString() << std::endl; 233 4941 : break; 234 1614 : case LogMessage::SEVERE: 235 : // if(send2cerr) // jagonzal: casalog.showconsole should control also if WARN/SEVERE msg go to console 236 1614 : std::cerr << message.toString() << std::endl; 237 1614 : break; 238 0 : default: 239 0 : break; 240 : } 241 : #endif 242 : } 243 : 244 1517176 : return posted; 245 : } 246 : 247 0 : void TSLogSink::cerrToo(Bool cerr2){ 248 0 : send2cerr = cerr2; 249 0 : } 250 : 251 : } //# NAMESPACE CASA - END 252 :