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