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: casa-feedback@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 5 : TSLogSink::TSLogSink(const std::string &path) 49 5 : : LogSinkInterface() 50 : { 51 : #ifdef AIPS_LOG4CPLUS 52 : BasicConfigurator config; 53 : config.configure(); 54 : #else 55 5 : logsink = 0 ; 56 5 : logfile = 0 ; 57 5 : send2cerr = false; 58 5 : setLogSink(path); 59 : #endif 60 5 : } 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 341 : void TSLogSink::setLogSink(String logname){ 90 : 91 341 : 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 0 : } 100 341 : if (logfile) { 101 336 : logfile->close() ; 102 336 : delete logfile ; 103 : } 104 341 : logfile = new std::ofstream(logname.c_str(), ios::app); 105 341 : if(logfile) { 106 341 : delete logsink ; 107 341 : logsink = new StreamLogSink(LogMessage::NORMAL, logfile); 108 : } 109 341 : if(!logsink) 110 0 : cerr << "Unable to log to " << logname << endl; 111 341 : 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 8 : TSLogSink::~TSLogSink() 136 : { 137 : #ifndef AIPS_LOG4CPLUS 138 4 : logfile->close(); 139 4 : delete logfile; 140 4 : delete logsink; 141 : #endif 142 8 : } 143 : 144 7033499 : const LogFilterInterface& TSLogSink::filter () const{return logsink->filter();} 145 : 146 503 : LogSinkInterface& TSLogSink::filter (const LogFilterInterface& newfilter) 147 : { 148 503 : LogSinkInterface::filter(newfilter); 149 503 : return logsink->filter(newfilter); 150 : } 151 : 152 1437280 : Bool TSLogSink::postLocally (const LogMessage& message) 153 : { 154 1437280 : Bool posted = false; 155 1437280 : if (this->filter().pass(message)) { 156 576932 : String tmp; 157 576932 : message.origin().objectID().toString(tmp); 158 576932 : LogOrigin theOrigin(message.origin()); 159 576932 : if(!message.origin().taskName().length()){ 160 351404 : theOrigin.taskName(LogSinkInterface::taskName); 161 : } else { 162 225528 : theOrigin.taskName(message.origin().taskName()); 163 : } 164 576932 : logsink->setTaskName(theOrigin.taskName()); 165 576932 : 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 576932 : logsink->postLocally(message); 191 576932 : posted = true; 192 576932 : 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 78153 : case LogMessage::DEBUG1: 199 78153 : if(send2cerr) 200 0 : std::cerr << message.toString() << std::endl; 201 78153 : break; 202 13410 : case LogMessage::DEBUG2: 203 13410 : if(send2cerr) 204 0 : std::cerr << message.toString() << std::endl; 205 13410 : break; 206 442251 : case LogMessage::NORMAL: 207 442251 : if(send2cerr) 208 0 : std::cerr << message.toString() << std::endl; 209 442251 : break; 210 6117 : case LogMessage::NORMAL1: 211 6117 : if(send2cerr) 212 0 : std::cerr << message.toString() << std::endl; 213 6117 : break; 214 9501 : case LogMessage::NORMAL2: 215 9501 : if(send2cerr) 216 0 : std::cerr << message.toString() << std::endl; 217 9501 : break; 218 15980 : case LogMessage::NORMAL3: 219 15980 : if(send2cerr) 220 0 : std::cerr << message.toString() << std::endl; 221 15980 : break; 222 66 : case LogMessage::NORMAL4: 223 66 : if(send2cerr) 224 0 : std::cerr << message.toString() << std::endl; 225 66 : break; 226 0 : case LogMessage::NORMAL5: 227 0 : if(send2cerr) 228 0 : std::cerr << message.toString() << std::endl; 229 0 : break; 230 5142 : case LogMessage::WARN: 231 : // if(send2cerr) // jagonzal: casalog.showconsole should control also if WARN/SEVERE msg go to console 232 5142 : std::cerr << message.toString() << std::endl; 233 5142 : break; 234 1711 : case LogMessage::SEVERE: 235 : // if(send2cerr) // jagonzal: casalog.showconsole should control also if WARN/SEVERE msg go to console 236 1711 : std::cerr << message.toString() << std::endl; 237 1711 : break; 238 0 : default: 239 0 : break; 240 : } 241 : #endif 242 576932 : } 243 : 244 1437280 : return posted; 245 : } 246 : 247 0 : void TSLogSink::cerrToo(Bool cerr2){ 248 0 : send2cerr = cerr2; 249 0 : } 250 : 251 : } //# NAMESPACE CASA - END 252 :