Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/MrsLog.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2010 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include <marsyas/MrsLog.h>
00020 #include <marsyas/common_source.h>
00021 
00022 #include <iostream>
00023 #include <fstream>
00024 
00025 using namespace Marsyas;
00026 using namespace std;
00027 
00028 mrs_string MrsLog::fname_ = "marsyas.log";
00029 bool MrsLog::warnings_off_ = false;
00030 bool MrsLog::messages_off_ = false;
00031 
00032 MrsLog::log_function_t MrsLog::message_function_ = 0;
00033 MrsLog::log_function_t MrsLog::warning_function_ = 0;
00034 MrsLog::log_function_t MrsLog::error_function_ = 0;
00035 MrsLog::log_function_t MrsLog::debug_function_ = 0;
00036 MrsLog::log_function_t MrsLog::diagnostic_function_ = 0;
00037 
00038 void
00039 MrsLog::setLogFile(mrs_string fname)
00040 {
00041   fname_ = fname;
00042 }
00043 
00044 void MrsLog::setMessageFunction(log_function_t function)
00045 {
00046   message_function_ = function;
00047 }
00048 
00049 void MrsLog::setWarningFunction(log_function_t function)
00050 {
00051   warning_function_ = function;
00052 }
00053 
00054 void MrsLog::setErrorFunction(log_function_t function)
00055 {
00056   error_function_ = function;
00057 }
00058 
00059 void MrsLog::setDebugFunction(log_function_t function)
00060 {
00061   debug_function_ = function;
00062 }
00063 
00064 void MrsLog::setDiagnosticFunction(log_function_t function)
00065 {
00066   diagnostic_function_ = function;
00067 }
00068 
00069 void MrsLog::setAllFunctions(log_function_t function)
00070 {
00071   message_function_ = function;
00072   warning_function_ = function;
00073   error_function_ = function;
00074   debug_function_ = function;
00075   diagnostic_function_ = function;
00076 }
00077 
00078 void
00079 MrsLog::mrsMessage(const ostringstream& oss)
00080 {
00081 #ifdef MARSYAS_LOG_MESSAGES
00082   if (!messages_off_)
00083   {
00084     if (message_function_)
00085       message_function_(oss.str());
00086 
00087 #ifdef MARSYAS_LOG2STDOUT
00088     cout << "[MRS_MESSAGE] " << oss.str() << endl;
00089 #endif
00090 
00091 #ifdef MARSYAS_LOG2STDERR
00092     std::cerr << "[MRS_MESSAGE] " << oss.str() << endl;
00093 #endif
00094 
00095 #ifdef MARSYAS_LOG2FILE
00096     ofstream ofs(fname_.c_str(), ios::out | ios::app);
00097     if (ofs.fail())
00098       return;
00099     if (!(ofs << "[MRS_MESSAGE] " <<  oss.str() << endl))
00100     {
00101       ofs.close();
00102       return;
00103     }
00104     ofs.close();
00105     return;
00106 
00107 #endif
00108   }
00109 
00110 
00111 #else
00112   (void) oss;
00113 #endif
00114 
00115 }
00116 
00117 
00118 
00119 void
00120 MrsLog::mrsErr(const ostringstream& oss)
00121 {
00122 #ifdef MARSYAS_LOG_ERRORS
00123 
00124   if (error_function_)
00125     error_function_(oss.str());
00126 
00127 #ifdef MARSYAS_LOG2STDOUT
00128   cout << "[MRSERR] " << oss.str() << endl;
00129 #endif
00130 
00131 #ifdef MARSYAS_LOG2STDERR
00132   std::cerr << "[MRSERR] " << oss.str() << endl;
00133 #endif
00134 
00135 #ifdef MARSYAS_LOG2FILE
00136   ofstream ofs(fname_.c_str(), ios::out | ios::app);
00137   if (ofs.fail())
00138     return;
00139   if (!(ofs << "[MRSERR] " << oss.str() << endl))
00140   {
00141     ofs.close();
00142     return;
00143   }
00144   ofs.close();
00145   return;
00146 #endif
00147 
00148 #else
00149   (void) oss;
00150 #endif
00151 }
00152 
00153 void
00154 MrsLog::mrsWarning(const ostringstream& oss)
00155 {
00156 #ifdef MARSYAS_LOG_WARNINGS
00157   if (!warnings_off_)
00158   {
00159     if (warning_function_)
00160       warning_function_(oss.str());
00161 
00162 #ifdef MARSYAS_LOG2STDOUT
00163     cout << "[MRS_WARNING] " << oss.str() << endl;
00164 #endif
00165 
00166 #ifdef MARSYAS_LOG2STDERR
00167     std::cerr << "[MRS_WARNING] " << oss.str() << endl;
00168 #endif
00169 
00170 #ifdef MARSYAS_LOG2FILE
00171     ofstream ofs(fname_.c_str(), ios::out | ios::app);
00172     if (ofs.fail())
00173       return;
00174     if (!(ofs << "[MRS_WARNING] " <<  oss.str() << endl))
00175     {
00176       ofs.close();
00177       return;
00178     }
00179     ofs.close();
00180     return;
00181 
00182 #endif
00183   }
00184 
00185 
00186 #else
00187   (void) oss;
00188 #endif
00189 
00190 }
00191 
00192 void
00193 MrsLog::mrsDiagnostic(const ostringstream& oss)
00194 {
00195 #ifdef MARSYAS_LOG_DIAGNOSTICS
00196 
00197   if (diagnostic_function_)
00198     diagnostic_function_(oss.str());
00199 
00200 #ifdef MARSYAS_LOG2STDOUT
00201   cout << "[MRS_DIAG] " << oss.str() << endl;
00202 #endif
00203 
00204 
00205 #ifdef MARSYAS_LOG2STDERR
00206   std::cerr << "[MRS_DIAG] " << oss.str() << endl;
00207 #endif
00208 
00209 #ifdef MARSYAS_LOG2FILE
00210   ofstream ofs(fname_.c_str(), ios::out | ios::app);
00211   if (ofs.fail())
00212     return;
00213   if (!(ofs << "[MRS_DIAG] " <<  oss.str() << endl))
00214   {
00215     ofs.close();
00216     return;
00217   }
00218   ofs.close();
00219   return;
00220 #endif
00221 
00222 #else
00223   (void) oss;
00224 #endif
00225 
00226 
00227 }
00228 
00229 void
00230 MrsLog::mrsDebug(const ostringstream& oss)
00231 {
00232   (void) oss;
00233 
00234   if (debug_function_)
00235     debug_function_(oss.str());
00236 
00237 #ifdef MARSYAS_LOG2STDOUT
00238   cout << "[MRS_DEBUG] " << oss.str() << endl;
00239 #endif
00240 
00241 #ifdef MARSYAS_LOG2STDERR
00242   std::cerr << "[MRS_DEBUG] " << oss.str() << endl;
00243 #endif
00244 
00245 
00246 #ifdef MARSYAS_LOG2FILE
00247   ofstream ofs(fname_.c_str(), ios::out | ios::app);
00248   if (ofs.fail())
00249     return;
00250   if (!(ofs << "[MRS_DEBUG] " <<  oss.str() << endl))
00251   {
00252     ofs.close();
00253     return;
00254   }
00255   ofs.close();
00256   return;
00257 #endif
00258 }
00259 
00260 void
00261 MrsLog::mrsAssert(const char *strFile, unsigned uLine)
00262 {
00263 #ifdef MARSYAS_ASSERTS
00264   fflush(NULL);
00265   fprintf(stderr, "\nMARSYAS Assertion failed: %s, line %u\n",
00266           strFile, uLine);
00267   fflush(stderr);
00268   abort();
00269 #else
00270   (void) strFile;
00271   (void) uLine;
00272 #endif
00273 }