casa  $Rev:20696$
MsPlotLogger.h
Go to the documentation of this file.
00001 //# MsPlot.h: this defines a message logger for the MS plotting classes.
00002 //# Copyright (C) 2007
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: MsPlot.h,v 1.7.2.27 2006/10/12 18:22:01 sjaeger Exp $
00028 //#
00029 //# -------------------------------------------------------------------------
00030 //# Change Log
00031 //# Date        Name        Description
00032 //# 11/14/2007  S. Jaeger   Fixed a typo.
00033 
00034 
00035 #ifndef CASA_MSPLOT_LOGGER_H
00036 #define CASA_MSPLOT_LOGGER_H
00037 
00038 //# Include files
00039 #include <casa/Logging/LogIO.h>
00040 #include <casa/BasicSL/String.h>
00041   
00042 #define LOG0 0
00043 // <summary>
00044 // A wrapper around the CASA logging facilities for the MS Plotter
00045 // </summary>
00046 
00047 // <use visibility=local>   or   <use visibility=export>
00048 // <use visibility=local>
00049 
00050 
00051 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00052 //#! for example:
00053 //#!  <reviewed reviewer="pshannon@nrao.edu" date="1994/10/10" tests="tMyClass, t1MyClass" demos="dMyClass, d1MyClass">
00054 //#!  </reviewed>
00055 // </reviewed>
00056 
00057 // <prerequisite>
00058 //#! Classes or concepts you should understand before using this class.
00059 //   <li> CASA::LogIO
00060 // </prerequisite>
00061 //
00062 // <etymology>
00063 // This class simplifies some of the typing needed in the MS Plot classes
00064 // to make the code more readable.
00065 // </etymology>
00066 //
00067 // <synopsis>
00068 // This class provides some set debugging information for the entry and
00069 // exit of each method, this can be very useful in determining where an
00070 // error is occuring.
00071 //
00072 // It also provides a method reduces some of the details that are
00073 // needed for messages to the logger in general.
00074 //
00075 // It might be useful to put this class at a higher-level for all CASA
00076 // classes to use, but for now it is here.
00077 // </synopsis>
00078 //
00079 // <example>
00080 // This example shows how to create and use the MSLogger class.
00081 // <srcblock>
00082 // //Create the MSLogger class
00083 // MsPlotLogger *log = new MsPlotLogger( "MsPlot" );
00084 //
00085 // // Register a debug message indicating entering and exiting a method.
00086 // log->fnEnter( "myFunc", "myFunc( arg1, arg2 )" );
00087 // log->setFnName( "myFunction" );
00088 //
00089 // // Log an error, standard, and debug message
00090 // log->logMessage( "Error: incorrect input", "LogMessage::ERROR", True );
00091 // log->logMessage( "Some earth shattering results", LogMessage::NORMAL );
00092 // log->debugMessage( "Cool we've found a result!" );
00093 //
00094 // Use the << operator to log a INFO3 message.
00095 // *log << LogOrigin( "myClass", "myMethod" )
00096 //      << LogMessage::NORMAL3
00097 //      << "Created Table Query Strings: " << tblQueryStrs
00098 //      << LogIO::POST;
00099 // </srcblock>
00100 // </example>
00101 //
00102 // <motivation>
00103 // I got tired of typing the same things over and over again.
00104 //
00105 // Also wanted to add some consistency to the messaging, for example to
00106 // make sure the LogOrigin was set.    
00107 // </motivation>
00108 //
00109 // <templating arg=T>
00110 // </templating>
00111 //
00112 // <thrown>
00113 //    <li> AIPSError
00114 //    <li>
00115 // </thrown>
00116 //
00117 // <todo asof="2007/10/29">
00118 //   <li> Look at it making it generic for all CASA 
00119 //   <li> Perhaps provide ways of "streaming" the error messages
00120 //        instead of making it all a single String.    
00121 // </todo>
00122 namespace casa { //# NAMESPACE CASA - BEGIN
00123   
00124 
00125 class MsPlotLogger : public casa::LogIO
00126 {
00127  public:
00128     //#! Constructors
00129     // Simple constructor, assumes there is no Class to be added to the
00130     // message origin.
00131     MsPlotLogger( const String& classNm, const String FnName )
00132     {
00133         itsClassNm=classNm;
00134         itsFnName=FnName;
00135 #if LOG0
00136         origin( LogOrigin( "MsPlotLogger", "constructor" ) );
00137         priority( LogMessage::DEBUG1 );
00138         output() << "Created MsPlotLogger for class: " << itsClassNm;
00139         post();
00140 #endif
00141     };
00142 
00143     // Constructor. The Class name is given, which will be used for
00144     // all messages created.
00145     MsPlotLogger( const String& classNm )
00146     {
00147         itsClassNm=classNm;
00148         itsFnName="";
00149 #if LOG0
00150         origin( LogOrigin( "MsPlotLogger", "constructor" ) );
00151         priority( LogMessage::DEBUG1 );
00152         output() << "Created MsPlotLogger for class: " << itsClassNm;
00153         post();
00154 #endif
00155     };
00156 
00157     // Simple constructor, assumes there is no Class to be added to the
00158     // message origin.
00159     MsPlotLogger() 
00160     {
00161         itsClassNm="";
00162         itsFnName="";
00163 #if LOG0
00164         origin( LogOrigin( "MsPlotLogger", "constructor" ) );
00165         priority( LogMessage::DEBUG1 );
00166         output() << "Created MsPlotLogger";
00167         post();
00168 #endif
00169     };
00170 
00171     // Destructor
00172     ~MsPlotLogger()
00173     {
00174 #if LOG0
00175         origin( LogOrigin( "MsPlotLogger", "constructor" ) );
00176         priority( LogMessage::DEBUG1 );
00177         output() << "Destroying MSLogger for class " << itsClassNm;
00178         post();
00179 #endif
00180     };
00181 
00182     //#!//////////////////////////////////////////////////////////////////////
00183     // <group>
00184     // Methods to set and get the class name used with this logger.
00185     void setClassName( String classNm ) { itsClassNm=classNm; };
00186     String getClassName() { return itsClassNm; };
00187     //</group>
00188 
00189 
00190     //#!//////////////////////////////////////////////////////////////////////
00191     // <group>
00192     // Methods to set and get the function name associated with logged messages.
00193     void setFnName( String fnName ) { itsFnName=fnName; };
00194     String getFnName() { return itsFnName; };
00195     //</group>
00196 
00197 
00198     //#  ///////////////////////////////////////////////////////////////////
00199     // Generic debug message added to log when entering a function
00200     void
00201     fnEnter( String fnName, String fnCall )
00202     {
00203         itsFnName = fnName;
00204         origin( LogOrigin( itsClassNm, fnName ) );
00205         priority( LogMessage::DEBUG1 );
00206         output() << "Entered Function: ";
00207         output() << (const char*)fnCall.c_str();
00208         post();
00209     };
00210 
00211     //# ///////////////////////////////////////////////////////////////////
00212     // <group>
00213     // Generic debug message added to log when exiting a function
00214     void
00215     fnExit( String fnName)
00216     {
00217         origin( LogOrigin( itsClassNm, fnName ) );
00218         priority( LogMessage::DEBUG1 );
00219         output() << "Exiting Function: ";
00220         output() << (const char*)fnName.c_str();
00221         post();
00222         itsFnName="";
00223     };
00224     
00225     void
00226     fnExit()
00227     {
00228         fnExit( itsFnName );
00229     };
00230     // </group>
00231 
00232 
00233     //#!//////////////////////////////////////////////////////////////////////
00234     //<group>  
00235     // Log a message to the CASA logging facilities.
00236     void
00237     logMessage( const String &msg, const String& classname, const String& fnname,
00238             LogMessage::Priority msglevel=LogMessage::NORMAL2,
00239             Bool onconsole=False,
00240             Bool throwexcep=False )
00241     {
00242         itsClassNm=classname;
00243         itsFnName=fnname;
00244         origin( LogOrigin( classname, fnname ) );
00245         priority( msglevel );
00246         output() << (const char *)msg.c_str();
00247         if ( onconsole )
00248             localSink().cerrToo( True );
00249         
00250         if ( throwexcep  )
00251             postThenThrow();
00252         else
00253             post();
00254 
00255         if ( onconsole )
00256             localSink().cerrToo( False );
00257     };
00258 
00259     void
00260     logMessage( const String &msg, const String& fnname,
00261             LogMessage::Priority msglevel=LogMessage::NORMAL2,
00262             Bool onconsole=False,
00263             Bool throwexcep=False )
00264     {
00265         logMessage( msg, itsClassNm, fnname, msglevel, onconsole, throwexcep );
00266     };
00267 
00268     void
00269     logMessage( const String &msg,
00270             LogMessage::Priority msglevel=LogMessage::NORMAL2,
00271             Bool onconsole=False,
00272             Bool throwexcep=False )
00273     {
00274         logMessage( msg, itsClassNm, itsFnName, msglevel, onconsole, throwexcep );
00275     };
00276     // </group>
00277 
00278     //#!//////////////////////////////////////////////////////////////////////
00279     // <group>
00280     // Log a debugging message to the CASA logging facilities.
00281     void
00282     debugMessage( const String &msg, const String& classname,
00283             const String& fnname,
00284             LogMessage::Priority msglevel=LogMessage::DEBUGGING )
00285     {
00286         itsClassNm=classname;
00287         itsFnName=fnname;
00288 
00289         logMessage( msg, classname, fnname, msglevel, False, False );
00290     };
00291 
00292     void
00293     debugMessage( const String &msg, const String& fnname,
00294             LogMessage::Priority msglevel=LogMessage::DEBUGGING )
00295     {
00296         debugMessage( msg, itsClassNm, fnname, msglevel );
00297     };
00298     
00299     void
00300     debugMessage( const String &msg,
00301             LogMessage::Priority msglevel=LogMessage::DEBUGGING )
00302     {
00303         debugMessage( msg, itsClassNm, itsFnName, msglevel );
00304     };
00305     // </group>
00306     
00307  private:
00308     //#! Data Members
00309     // Name of the class the msgs. belong to.
00310     String itsClassNm;
00311 
00312     // Name of the function/method the msgs. belong to.
00313     String itsFnName;
00314 
00315     
00316 };  // class MsPlotLogger;
00317 } //# NAMESPACE CASA - END
00318 
00319 
00320 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines