TuttleOFX  1
ofxsLog.cpp
Go to the documentation of this file.
00001 /*
00002  * OFX Support Library, a library that skins the OFX plug-in API with C++ classes.
00003  * Copyright (C) 2004-2005 The Open Effects Association Ltd
00004  * Author Bruno Nicoletti bruno@thefoundry.co.uk
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * Redistributions of source code must retain the above copyright notice,
00010  * this list of conditions and the following disclaimer.
00011  * Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  * Neither the name The Open Effects Association Ltd, nor the names of its
00015  * contributors may be used to endorse or promote products derived from this
00016  * software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00022  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00025  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  * The Open Effects Association Ltd
00030  * 1 Wardour St
00031  * London W1D 6PA
00032  * England
00033  *
00034  *
00035  */
00036 
00037 /** @file This file contains the body of functions used for logging ofx problems etc...
00038  *
00039  * The log file is written to using printf style functions, rather than via c++ iostreams.
00040  *
00041  */
00042 
00043 #include <string>
00044 #include <cassert>
00045 #include <cstdio>
00046 #include <cstdarg>
00047 #include <cstdlib>
00048 
00049 //#define OFX_LOG
00050 
00051 namespace OFX {
00052 namespace Log {
00053 
00054 /** @brief log file */
00055 static FILE* gLogFP = 0;
00056 
00057 /// environment variable for the log file
00058 #define kLogFileEnvVar "OFX_PLUGIN_LOGFILE"
00059 
00060 /** @brief the global logfile name */
00061 static std::string gLogFileName( getenv( kLogFileEnvVar ) ? getenv( kLogFileEnvVar ) : "ofxTestLog.txt" );
00062 
00063 /** @brief global indent level, not MP sane */
00064 static int gIndent = 0;
00065 
00066 /** @brief Sets the name of the log file. */
00067 void setFileName( const std::string& value )
00068 {
00069         gLogFileName = value;
00070 }
00071 
00072 /** @brief Opens the log file, returns whether this was sucessful or not. */
00073 bool open( void )
00074 {
00075         #ifdef OFX_LOG
00076         if( !gLogFP )
00077         {
00078                 gLogFP = fopen( gLogFileName.c_str(), "w" );
00079                 return gLogFP != 0;
00080         }
00081         #endif
00082         return gLogFP != 0;
00083 }
00084 
00085 /** @brief Closes the log file. */
00086 void close( void )
00087 {
00088         if( gLogFP )
00089         {
00090                 fclose( gLogFP );
00091         }
00092         gLogFP = 0;
00093 }
00094 
00095 /** @brief Indent it, not MP sane at the moment */
00096 void indent( void )
00097 {
00098         ++gIndent;
00099 }
00100 
00101 /** @brief Outdent it, not MP sane at the moment */
00102 void outdent( void )
00103 {
00104         --gIndent;
00105 }
00106 
00107 /** @brief do the indenting */
00108 static void doIndent( void )
00109 {
00110         if( open() )
00111         {
00112                 for( int i = 0; i < gIndent; i++ )
00113                 {
00114                         fputs( "    ", gLogFP );
00115                 }
00116         }
00117 }
00118 
00119 /** @brief Prints to the log file. */
00120 void print( const char* format, ... )
00121 {
00122         if( open() )
00123         {
00124                 doIndent();
00125                 va_list args;
00126                 va_start( args, format );
00127                 vfprintf( gLogFP, format, args );
00128                 fputc( '\n', gLogFP );
00129                 fflush( gLogFP );
00130                 va_end( args );
00131         }
00132 }
00133 
00134 /** @brief Prints to the log file only if the condition is true and prepends a warning notice. */
00135 void warning( bool condition, const char* format, ... )
00136 {
00137         if( condition && open() )
00138         {
00139                 doIndent();
00140                 fputs( "WARNING : ", gLogFP );
00141 
00142                 va_list args;
00143                 va_start( args, format );
00144                 vfprintf( gLogFP, format, args );
00145                 fputc( '\n', gLogFP );
00146                 va_end( args );
00147 
00148                 fflush( gLogFP );
00149         }
00150 }
00151 
00152 /** @brief Prints to the log file only if the condition is true and prepends an error notice. */
00153 void error( bool condition, const char* format, ... )
00154 {
00155         if( condition && open() )
00156         {
00157                 doIndent();
00158                 fputs( "ERROR : ", gLogFP );
00159 
00160                 va_list args;
00161                 va_start( args, format );
00162                 vfprintf( gLogFP, format, args );
00163                 fputc( '\n', gLogFP );
00164                 va_end( args );
00165 
00166                 fflush( gLogFP );
00167         }
00168 }
00169 
00170 };
00171 };