casa
$Rev:20696$
|
00001 //# Diagnostic.h: debugging diagnostic for dbus clents 00002 //# Copyright (C) 2012 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 00028 #ifndef CASA_DBUS_DIAGNOSTIC_H_ 00029 #define CASA_DBUS_DIAGNOSTIC_H_ 00030 #include <string> 00031 #include <stdarg.h> 00032 00033 namespace casa { 00034 namespace dbus { 00035 00036 class Diagnostic { 00037 public: 00038 friend class init_diagnostic_object_t; 00039 void argv( int argc_, const char *argv_[] ) { 00040 kernel_t &k = lock_kernel( ); 00041 if ( k.do_log( ) ) k.argv(argc_,argv_); 00042 release_kernel( ); 00043 } 00044 void argv( int argc_, char *argv_[] ) { 00045 kernel_t &k = lock_kernel( ); 00046 if ( k.do_log( ) ) k.argv(argc_,argv_); 00047 release_kernel( ); 00048 } 00049 Diagnostic( ) { } 00050 virtual ~Diagnostic( ) { } 00051 00052 void error(const char *fmt, ...) { 00053 kernel_t &k = lock_kernel( ); 00054 if ( k.do_log( ) ) { 00055 va_list argp; 00056 va_start(argp, fmt); 00057 verror(k, fmt, argp); 00058 va_end(argp); 00059 } 00060 } 00061 00062 void info(const char *fmt, ...) { 00063 kernel_t &k = lock_kernel( ); 00064 if ( k.do_log( ) ) { 00065 va_list argp; 00066 va_start(argp, fmt); 00067 vinfo(k, fmt, argp); 00068 va_end(argp); 00069 } 00070 } 00071 00072 private: 00073 00074 struct kernel_t { 00075 kernel_t( ); 00076 kernel_t( FILE *f ); 00077 bool do_log( ) const { return fptr != 0; } 00078 ~kernel_t( ) { if ( fptr ) fclose(fptr); } 00079 void argv( int argc_, const char *argv_[] ); 00080 void argv( int argc_, char *argv_[] ); 00081 FILE *fptr; 00082 pid_t pid; 00083 std::string name; /**** any non-argv messages should include 'name' ****/ 00084 }; 00085 00086 /* void verror(Diagnostic::kernel_t &, const char *fmt, va_list argp); */ 00087 /* void vinfo(Diagnostic::kernel_t &, const char *fmt, va_list argp); */ 00088 void verror(kernel_t &, const char *fmt, va_list argp); 00089 void vinfo(kernel_t &, const char *fmt, va_list argp); 00090 00091 kernel_t &lock_kernel( ); 00092 void release_kernel( ) { } 00093 void output_prologue( ); 00094 void output_epilogue( ); 00095 }; 00096 00097 Diagnostic diagnostic; 00098 00099 static class init_diagnostic_object_t { 00100 public: 00101 init_diagnostic_object_t( ) { if ( count++ == 0 ) diagnostic.output_prologue( ); } 00102 ~init_diagnostic_object_t( ) { if ( --count == 0 ) { diagnostic.output_epilogue( ); } } 00103 private: 00104 static unsigned long count; 00105 } init_diagnostic_object_; 00106 } 00107 } 00108 00109 #endif