casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PlotLogger.h
Go to the documentation of this file.
00001 //# PlotLogger.h: Classes to perform various logging actions for the plotter.
00002 //# Copyright (C) 2009
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 //# $Id: $
00027 #ifndef PLOTLOGGER_H_
00028 #define PLOTLOGGER_H_
00029 
00030 #include <graphics/GenericPlotter/PlotOptions.h>
00031 
00032 #include <casa/Logging/LogSinkInterface.h>
00033 #include <casa/Utilities/CountedPtr.h>
00034 
00035 #include <map>
00036 #include <time.h>
00037 #include <vector>
00038 
00039 #include <casa/namespace.h>
00040 using namespace std;
00041 
00042 namespace casa {
00043 
00044 //# Forward declarations
00045 class PlotCanvas;
00046 class Plotter;
00047 
00048 
00049 // Superclass for all messages going through the PlotLogger.  This class has
00050 // been refactored to just be a thin layer on top of LogMessage.
00051 class PlotLogMessage : public LogMessage {
00052 public:
00053     // Static //
00054     
00055     // Default event type.
00056     static const int DEFAULT_EVENT_TYPE;
00057     
00058     
00059     // Non-Static //
00060     
00061     // Constructor which takes an optional priority.
00062     PlotLogMessage(int eventType = DEFAULT_EVENT_TYPE);
00063     
00064     // Constructor which takes the origin(s) and an optional priority.
00065     PlotLogMessage(const String& origin1, const String& origin2,
00066             int eventType = DEFAULT_EVENT_TYPE);
00067     
00068     // Constructor which takes the origin(s), the message, and an optional
00069     // priority.
00070     PlotLogMessage(const String& origin1, const String& origin2,
00071             const String& message, int eventType = DEFAULT_EVENT_TYPE);
00072     
00073     // Copy constructor.
00074     PlotLogMessage(const PlotLogMessage& copy);
00075     
00076     // Destructor.
00077     virtual ~PlotLogMessage();
00078     
00079     
00080     // Returns the event type of this message.
00081     virtual int eventType() const;
00082     
00083 protected:
00084     // Event type, either a value from PlotLogger::Event, or a custom
00085     // user-defined value.
00086     int m_eventType;
00087 };
00088 
00089 
00090 // Used to report time and memory measurements.  This functionality can be
00091 // accessed either directly with a PlotLogMeasurement object or indirectly
00092 // through the PlotLogger class.  Message is:
00093 // END.\tTime: [time] [timeUnits].  Memory: [memory] [memoryUnits].
00094 // If the measurement has not been ended, calls stopMeasurement() first.
00095 class PlotLogMeasurement : public PlotLogMessage {
00096 public:
00097     // Static //
00098     
00099     // Available time units.  Currently only seconds because that's all that
00100     // C++ natively supports.
00101     enum TimeUnit {
00102         SECOND
00103     };
00104     
00105     // Available memory units.
00106     enum MemoryUnit {
00107         BYTE, KILOBYTE, MEGABYTE
00108     };
00109     
00110     // Default units.
00111     // <group>
00112     static const TimeUnit DEFAULT_TIME_UNIT;
00113     static const MemoryUnit DEFAULT_MEMORY_UNIT;
00114     // </group>
00115     
00116     // Get a string representation of the given time/memory unit.
00117     // <group>
00118     static String timeUnits(TimeUnit t);   
00119     static String memoryUnits(MemoryUnit m);
00120     // </group>
00121     
00122     
00123     // Non-Static //
00124     
00125     // Constructor which takes the origin(s), optional time and memory
00126     // units, and an optional priority.  Also calls startMeasurement().
00127     PlotLogMeasurement(const String& origin1, const String& origin2,
00128                        TimeUnit timeUnit = DEFAULT_TIME_UNIT,
00129                        MemoryUnit memoryUnit = DEFAULT_MEMORY_UNIT,
00130                        int eventType = DEFAULT_EVENT_TYPE);
00131     
00132     // Copy constructor.
00133     PlotLogMeasurement(const PlotLogMeasurement& copy);
00134     
00135     // Destructor.
00136     ~PlotLogMeasurement();
00137     
00138     
00139     // Returns the time/memory when the measurement started.
00140     // <group>
00141     time_t startTime() const;
00142     unsigned int startMemory() const;
00143     // </group>
00144     
00145     // Returns the time/memory difference between when the measurement started
00146     // and when the measurement ended.  Invalid if the measurement was never
00147     // started and ended.
00148     // <group>
00149     double time() const;    
00150     double memory() const;
00151     // </group>
00152     
00153     // Returns the time/memory units for this measurement.
00154     // <group>
00155     TimeUnit timeUnit() const;
00156     MemoryUnit memoryUnit() const;
00157     // </group>
00158    
00159     // Starts the measurement by setting the start time and memory.
00160     // Measurement automatically begins when the object is constructed, but
00161     // can be restarted as desired.
00162     void startMeasurement();
00163     
00164     // Calculates the measurements from the last starting point to this point,
00165     // and generates the log message.
00166     void stopMeasurement();
00167     
00168 private:
00169     // Start time
00170     time_t m_startTime;
00171     
00172     // Start memory
00173     unsigned int m_startMemory;
00174     
00175     // Time and memory differences
00176     double m_time, m_memory;
00177     
00178     // Time unit
00179     TimeUnit m_timeUnit;
00180     
00181     // Memory unit
00182     MemoryUnit m_memoryUnit;
00183 };
00184 
00185 
00186 // Used to report located indices.  Basically just a container for the results
00187 // of a PlotCanvas::locate.
00188 class PlotLogLocate : public PlotLogMessage {
00189 public:
00190     // Static //
00191     
00192     // Convenience access to PlotCanvas::locate() to return a PlotLogLocate
00193     // message.
00194     static PlotLogLocate canvasLocate(PlotCanvas* canvas,
00195             const PlotRegion& region);
00196     
00197     
00198     // Non-Static //
00199     
00200     // Constructor which takes the two origins, the region that was located,
00201     // and the located indices (see PlotCanvas::locate()).  If
00202     // deleteIndicesOnDestruction is true, the given indices vector will be
00203     // deleted when this message is destroyed.  This should be used with care
00204     // if multiple PlotLogLocates using the same located indices are being
00205     // used.
00206     PlotLogLocate(const String& origin1, const String& origin2,
00207             const PlotRegion& locateRegion,
00208             vector<vector<pair<unsigned int,unsigned int> > >* locatedIndices,
00209             int eventType = DEFAULT_EVENT_TYPE,
00210             bool deleteIndicesOnDestruction = true);
00211     
00212     // Copy constructor.  NOTE: will set the deleteIndicesOnDestruction flag on
00213     // the original to false so that the indices won't be deleted from out
00214     // under the copy.
00215     PlotLogLocate(const PlotLogLocate& copy);
00216     
00217     // Destructor.
00218     ~PlotLogLocate();
00219     
00220     
00221     // Returns the region that was located.
00222     const PlotRegion& locateRegion() const;
00223     
00224     // Returns the total number of points that were located.
00225     unsigned int numLocatedIndices() const;
00226     
00227     // Returns the number of plots that were searched.
00228     unsigned int numSearchedPlots() const;
00229     
00230     // Returns the located indices.
00231     vector<vector<pair<unsigned int, unsigned int> > >* indices() const;
00232     
00233     // Returns the located indices for the plot at the given index, or NULL for
00234     // invalid.
00235     vector<pair<unsigned int, unsigned int> >* plotIndices(
00236             unsigned int index) const;
00237     
00238     // Returns whether or not this message will delete the indices vector on
00239     // destruction or not.
00240     bool willDeleteIndices() const;
00241     
00242 private:
00243     // Region.
00244     PlotRegion m_region;
00245     
00246     // Indices.
00247     vector<vector<pair<unsigned int, unsigned int> > >* m_indices;
00248     
00249     // Should delete indices.
00250     bool m_shouldDelete;
00251 };
00252 
00253 
00254 // Subclass of PlotLogMessage to unify messages for method entering/exiting.
00255 class PlotLogMethod : public PlotLogMessage {
00256 public:
00257     // Constructor which takes the class and method names, a flag for whether
00258     // the method is entering or exiting, and an optional additional message
00259     // and priority.
00260     PlotLogMethod(const String& className, const String& methodName,
00261             bool entering, const String& message = String(),
00262             int eventType = DEFAULT_EVENT_TYPE);
00263     
00264     // Destructor.
00265     ~PlotLogMethod();
00266 };
00267 
00268 
00269 // Subclass of PlotLogMessage to unify messages for object creation/deletion.
00270 class PlotLogObject : public PlotLogMessage {
00271 public:
00272     // Constructor which takes the class name and object address, a flag for
00273     // whether the object is being created or destroyed, and an optional
00274     // additional message and priority.
00275     PlotLogObject(const String& className, void* address, bool creation,
00276             const String& message = String(),
00277             int eventType = DEFAULT_EVENT_TYPE);
00278     
00279     // Destructor.
00280     ~PlotLogObject();
00281 };
00282 
00283 
00284 // Subclass of LogFilterInterface to filter on both event flags and minimum
00285 // priority.
00286 class PlotLoggerFilter : public LogFilterInterface {
00287 public:
00288     // Constructor which takes optional event flags and minimum priority.
00289     PlotLoggerFilter(int eventFlags, LogMessage::Priority minPriority);
00290     
00291     // Destructor.
00292     ~PlotLoggerFilter();
00293     
00294     // Implements LogFilterInterface::clone().
00295     LogFilterInterface* clone() const;
00296     
00297     // Implements LogFilterInterface::pass().
00298     Bool pass(const LogMessage& message) const;
00299     
00300     // Gets/Sets the event flags.
00301     // <group>
00302     int eventFlags() const;
00303     void setEventFlags(int flags);
00304     // </group>
00305     
00306     // Gets/Sets the minimum priority.
00307     // <group>
00308     LogMessage::Priority minimumPriority() const;
00309     void setMinimumPriority(LogMessage::Priority minPriority);
00310     // </group>
00311     
00312 private:
00313     // Event flags.
00314     int m_eventFlags;
00315     
00316     // Minimum priority.
00317     LogMessage::Priority m_minPriority;
00318 };
00319 
00320 
00321 // A PlotLogger is used to log messages to an underlying CASA log object, as
00322 // well as provide access to different logging functionality like measurements.
00323 // PlotLogger is associated with a single Plotter object and should be used by
00324 // all children of that Plotter (canvases, plot items, etc.) to report their
00325 // behavior if the proper flag is turned on.  The logger can also filter out
00326 // messages by priority.  Therefore a message must BOTH have its event type
00327 // flag turned on, AND meet the priority minimum in order to be posted to the
00328 // log.  The exception to this is for the MSG_* event types, which are logged
00329 // as long as they meet the filtered priority requirement.  The actual log can
00330 // either be the global sink, or can be set to a file.
00331 class PlotLogger {
00332 public:
00333     // Static //
00334     
00335     // Event types //
00336     
00337     // Event types that are always allowed.
00338     // <group>
00339     static const int MSG_INFO  = -1;
00340     static const int MSG_WARN  = -2;
00341     static const int MSG_ERROR = -3;
00342     // </group>
00343     
00344     // Miscellaneous debugging messages.
00345     static const int MSG_DEBUG       = 1;
00346     
00347     // Replotting/redrawing the whole GUI.
00348     static const int DRAW_TOTAL      = 2;
00349     
00350     // Replotting/redrawing each plot item.
00351     static const int DRAW_INDIVIDUAL = 4;
00352     
00353     // Entering/exiting major methods.
00354     static const int METHODS_MAJOR   = 8;
00355     
00356     // Creation/deletion of major objects.
00357     static const int OBJECTS_MAJOR   = 16;
00358     
00359     // Exporting canvases to file.
00360     static const int EXPORT_TOTAL    = 32;
00361     
00362     
00363     // No events.
00364     static const int NO_EVENTS = 0;
00365     
00366     // All events as a flag.
00367     static int ALL_EVENTS_FLAG();
00368     
00369     // All events as a vector.
00370     static vector<int> ALL_EVENTS();
00371     
00372     
00373     // Registers an extended event type with the given name and optional
00374     // priority and returns its value.
00375     static int REGISTER_EVENT_TYPE(const String& name,
00376             LogMessage::Priority priority = LogMessage::NORMAL);
00377     
00378     // Unregisters the given extended event type.  If a priority has been set,
00379     // it is NOT removed.
00380     // <group>
00381     static void UNREGISTER_EVENT_TYPE(int event);
00382     static void UNREGISTER_EVENT_TYPE(const String& name);
00383     // </group>
00384     
00385     // Returns all the event names.
00386     static vector<String> EVENT_NAMES();
00387     
00388     // Converts between an event type and its name.
00389     // <group>
00390     static String EVENT(int type);
00391     static int EVENT(const String& name);
00392     // </group>
00393     
00394     // Returns an event flag from the given vector.
00395     // <group>
00396     static int FLAG_FROM_EVENTS(const vector<int>& events);
00397     static int FLAG_FROM_EVENTS(const vector<String>& names);
00398     // </group>
00399     
00400     // Returns an event flag for all events that meet the given minimum
00401     // priority.
00402     static int FLAG_FROM_PRIORITY(LogMessage::Priority minPriority);
00403     
00404     
00405     // Gets/Sets the message priority for the given log event.  Uses a default
00406     // if the event has not been set.
00407     // <group>
00408     static LogMessage::Priority EVENT_PRIORITY(int event);
00409     static void SET_EVENT_PRIORITY(int event, LogMessage::Priority priority);
00410     // </group>
00411     
00412     
00413     // Disables the global sink until enableGlobalSink() is called.  Can be
00414     // used when something posts undesirably to both the local and global logs.
00415     static void disableGlobalSink();
00416     
00417     // Re-enables the global sink.  See disableGlobalSink().
00418     static void enableGlobalSink();
00419     
00420     
00421     // Non-Static //
00422     
00423     // Constructor which takes the Plotter this logger is associated with.  The
00424     // global log sink is used, and the minimum priority filter is set.
00425     PlotLogger(Plotter* plotter, int filterEventFlags = NO_EVENTS,
00426             LogMessage::Priority filterMinPriority = LogMessage::DEBUGGING);
00427     
00428     // Destructor.
00429     virtual ~PlotLogger();
00430     
00431     
00432     // Log IO Methods //
00433     
00434     // Gets the log sink interface.
00435     // <group>
00436     CountedPtr<LogSinkInterface> sink();
00437     const CountedPtr<LogSinkInterface> sink() const;
00438     // </group>
00439     
00440     // Gets a copy of the log sink interface, IF it is not the global.
00441     LogSinkInterface* localSinkCopy() const;
00442     
00443     // Gets/Sets the log sink file location.  If the filename is empty, it
00444     // means the global sink.
00445     // <group>
00446     const String& sinkLocation() const;
00447     void setSinkLocation(const String& logFile);
00448     // </group>
00449     
00450     // Returns true if the logger is currently using the global sink, false
00451     // otherwise.
00452     bool usingGlobalSink() const { return sinkLocation().empty(); }
00453     
00454     
00455     // Filtering Methods //
00456     
00457     // Gets/Sets the log filter priority level.
00458     // <group>
00459     LogMessage::Priority filterMinPriority() const;
00460     void setFilterMinPriority(PlotLogMessage::Priority minPriority);
00461     // </group>
00462     
00463     // Gets/Sets the log filter event flag for a single event type.
00464     // <group>
00465     bool filterEventFlag(int flag) const;
00466     void setFilterEventFlag(int flag, bool on);
00467     // </group>
00468     
00469     // Gets/Sets the log filter event flag(s).  The flag should be a bitwise-or
00470     // of one or more values in PlotLogger events and any extended event types.
00471     // <group>
00472     int filterEventFlags() const;
00473     void setFilterEventFlags(int flags);
00474     // <group>
00475     
00476     
00477     // Message Methods //
00478     
00479     // Posts the given message to the underlying log sink.
00480     // <group>
00481     void postMessage(const PlotLogMessage& message);
00482     void postMessage(const String& origin1, const String& origin2,
00483                      const String& message,
00484                      int eventType = PlotLogMessage::DEFAULT_EVENT_TYPE);
00485     // </group>
00486     
00487     
00488     // Measurement Methods //
00489     
00490     // Marks the logger to begin a time/memory measurement.  Measurement marks
00491     // can be recursive.  Returns a generic message saying that measurement has
00492     // begun, which will be also posted to the log if postStartMessage is true.
00493     PlotLogMessage markMeasurement(const String& origin1,const String& origin2,
00494             int eventType = PlotLogMessage::DEFAULT_EVENT_TYPE,
00495             bool postStartMessage = true);
00496     
00497     // Gets the measurement since the last mark.  The message will also be
00498     // posted to the log if postReleaseMessage is true.
00499     PlotLogMeasurement releaseMeasurement(bool postReleaseMessage = true);
00500     
00501     
00502     // Locate Methods //
00503     
00504     // Calls locate on the given canvas and returns the result as a message
00505     // that will also be posted if postLocateMessage is true.
00506     PlotLogLocate locate(PlotCanvas* canvas, const PlotRegion& region,
00507             int eventType = MSG_INFO, bool postLocateMessage = true);
00508     
00509 private:
00510     // Plotter.
00511     Plotter* m_plotter;
00512     
00513     // Log sink.
00514     CountedPtr<LogSinkInterface> m_logger;
00515     
00516     // Log filter.
00517     PlotLoggerFilter m_filter;
00518     
00519     // Log sink location.
00520     String m_loggerLocation;
00521     
00522     // Current measurement marks.
00523     vector<PlotLogMeasurement> m_measurements;
00524 
00525     
00526     // Static //
00527     
00528     // Registered extended types.
00529     static vector<int> EXTENDED_TYPES;
00530     
00531     // Registered extended type names.
00532     static vector<String> EXTENDED_NAMES;
00533     
00534     // Map from log event to priority.
00535     static map<int, LogMessage::Priority> EVENT_PRIORITIES;
00536     
00537     // Disabled old global filter, or null.
00538     static LogFilterInterface* DISABLED_GLOBAL_FILTER;
00539 };
00540 typedef CountedPtr<PlotLogger> PlotLoggerPtr;
00541 
00542 }
00543 
00544 #endif /* PLOTLOGGER_H_ */