LCOV - code coverage report
Current view: top level - stdcasa - TSLogSink.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 42 126 33.3 %
Date: 2024-10-29 13:38:20 Functions: 6 15 40.0 %

          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           3 : TSLogSink::TSLogSink(const std::string &path)
      49           3 : : LogSinkInterface()
      50             : {
      51             : #ifdef AIPS_LOG4CPLUS
      52             :     BasicConfigurator config;
      53             :     config.configure();
      54             : #else
      55           3 :    logsink = 0 ;
      56           3 :    logfile = 0 ;
      57           3 :    send2cerr = false;
      58           3 :    setLogSink(path);
      59             : #endif
      60           3 : }
      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           6 : void TSLogSink::setLogSink(String logname){
      90             :    
      91           6 :    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           6 :    if (logfile) {
     101           3 :      logfile->close() ;
     102           3 :      delete logfile ;
     103             :    }
     104           6 :    logfile = new std::ofstream(logname.c_str(), ios::app);
     105           6 :    if(logfile) {
     106           6 :      delete logsink ;
     107           6 :      logsink = new StreamLogSink(LogMessage::NORMAL, logfile);
     108             :    }
     109           6 :    if(!logsink)
     110           0 :            cerr << "Unable to log to " << logname << endl;
     111           6 :    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           6 : TSLogSink::~TSLogSink()
     136             : {
     137             : #ifndef AIPS_LOG4CPLUS
     138           3 :    logfile->close();
     139           3 :    delete logfile;
     140           3 :    delete logsink;
     141             : #endif
     142           6 : }
     143             : 
     144          15 : 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          15 : Bool TSLogSink::postLocally (const LogMessage& message)
     153             : {
     154          15 :   Bool posted = false;
     155          15 :   if (this->filter().pass(message)) {
     156          15 :     String tmp;
     157          15 :     message.origin().objectID().toString(tmp);
     158          15 :     LogOrigin theOrigin(message.origin());
     159          15 :     if(!message.origin().taskName().length()){
     160          15 :        theOrigin.taskName(LogSinkInterface::taskName);
     161             :     } else {
     162           0 :        theOrigin.taskName(message.origin().taskName());
     163             :     }
     164          15 :     logsink->setTaskName(theOrigin.taskName());
     165          15 :     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          15 :     logsink->postLocally(message);
     191          15 :     posted = true;
     192          15 :     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          15 :         case LogMessage::NORMAL:
     207          15 :                 if(send2cerr)
     208           0 :                    std::cerr << message.toString() << std::endl;
     209          15 :                 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          15 :   }
     243             : 
     244          15 :   return posted;
     245             : }
     246             : 
     247           0 : void TSLogSink::cerrToo(Bool cerr2){
     248           0 :         send2cerr = cerr2;
     249           0 : }
     250             : 
     251             : } //# NAMESPACE CASA - END
     252             : 

Generated by: LCOV version 1.16