BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Tools/Log.h
Go to the documentation of this file.
00001 // Copyright (c) 2005  Stanford University (USA).
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation; version 2.1 of the License.
00007 // See the file LICENSE.LGPL distributed with CGAL.
00008 //
00009 // Licensees holding a valid commercial license may use this file in
00010 // accordance with the commercial license agreement provided with the software.
00011 //
00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 //
00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Kinetic_data_structures/include/CGAL/Tools/Log.h $
00016 // $Id: Log.h 41622 2008-01-14 22:01:20Z drussel $
00017 // 
00018 //
00019 // Author(s)     : Daniel Russel <drussel@alumni.princeton.edu>
00020 
00021 #ifndef CGAL_LOG_H_
00022 #define CGAL_LOG_H_
00023 #include <CGAL/basic.h>
00024 #include <CGAL/Tools/utility_macros.h>
00025 #include <iostream>
00026 #include <fstream>
00027 #include <ios>
00028 
00029 CGAL_BEGIN_NAMESPACE
00030 
00031 class Log
00032 {
00033 public: 
00034   enum Level {NONE=0, SOME=2, LOTS=3};
00035   
00036   enum Target {COUT, FILE, DEVNULL};
00037 private:
00038   struct State {
00039     Target target_;
00040     Level level_;
00041     std::ofstream fstream_;
00042     std::ofstream null_;
00043     std::ofstream maple_;
00044     bool maple_is_open_;
00045     bool output_maple_;
00046     State(){
00047       level_= NONE;
00048       target_= COUT;
00049       null_.open("/dev/null");
00050       //maple_.open("maple.log");
00051       maple_is_open_=false;
00052       output_maple_=true;
00053     }
00054   };
00055   static State state_;
00056 public:
00057   // The different types of logs supported
00058   /*  MAPLE is a log which should be able to be fed directly in to
00059       maple and preferably will produce obviously good or bad outwhen
00060       when evaluated.
00061   */
00062 
00063   static Level level() {return state_.level_;}
00064   static void set_level(Level l) {state_.level_=l;}
00065  
00066 
00067   static std::ostream &stream(Level l) {
00068     if (is_output(l)) {
00069       if (state_.target_== COUT) {
00070         return std::cout;
00071       }
00072       else {
00073         return state_.fstream_;
00074       }
00075     }
00076     else {
00077       return state_.null_;
00078     }
00079   }
00080 
00081   static bool is_output(Level l) {
00082     return l <= level();
00083   }
00084   static Target target() {return state_.target_;}
00085   static CGAL_SET(Target, target, state_.target_=k);
00086   static CGAL_SET(std::string, filename, state_.fstream_.open(k.c_str()));
00087 
00088   static bool is_output_maple(){return state_.output_maple_;}
00089   
00090   static void set_output_maple(bool b) {
00091     state_.output_maple_=b;
00092   }
00093   std::ofstream &maple_stream() {
00094     if (!state_.maple_is_open_) {
00095       state_.maple_is_open_=true;
00096       state_.maple_.open("maple.log");
00097     }
00098     return state_.maple_;
00099   }
00100 private:
00101   Log() {
00102    
00103   }
00104 };
00105 
00106 
00107 
00108 #ifndef CGAL_DISABLE_LOGGING
00109 #define CGAL_LOG(level, expr) if (CGAL::Log::is_output(level))\
00110     { CGAL::Log::stream(level) << expr << std::flush;};
00111 #define CGAL_LOG_WRITE(level, expr) if (CGAL::Log::is_output(level))\
00112 {std::ostream &LOG_STREAM= CGAL::Log::stream(level); expr;}
00113 #define CGAL_ERROR(expr) std::cerr << expr << std::endl;
00114 #define CGAL_ERROR_WRITE(expr) {std::ostream &LOG_STREAM= std::cerr; expr; std::cerr << std::endl;}
00115 #define CGAL_SET_LOG_LEVEL(level) CGAL::Log::set_level(level);
00116 #define CGAL_GET_LOG_LEVEL CGAL::Log::level();
00117 
00118 template <class T>
00119 inline int CGAL_assertion_strip_unsigned(const T&t) {
00120   return static_cast<int>(t);
00121 }
00122 
00123 
00124 /*inline int CGAL_assertion_strip_unsigned(size_t t) {
00125   return static_cast<int>(t);
00126   }*/
00127 
00128 #define CGAL_assert_equal(a,b) do {if (a != b) { CGAL_ERROR("" #a " = " << a); CGAL_ERROR("" #b " = " << b); CGAL_assertion(a ==b);} } while (0)
00129 #define CGAL_check_bounds(a,b,e) do {if (CGAL::CGAL_assertion_strip_unsigned(a) < CGAL::CGAL_assertion_strip_unsigned(b) || CGAL::CGAL_assertion_strip_unsigned(a) >=CGAL::CGAL_assertion_strip_unsigned(e)){ CGAL_ERROR("" #a " = " << a); CGAL_ERROR("[" #b "..." #e ") = [" << b << "..." << e << ")"); CGAL_error();} } while (0)
00130 
00131 #else
00132 #define CGAL_LOG(l,e)
00133 #define CGAL_LOG_WRITE(l,e)
00134 #define CGAL_ERROR(e)
00135 #define CGAL_ERROR_WRITE(e)
00136 #define CGAL_SET_LOG_LEVEL(l)
00137 #define CGAL_assert_equal(a,b) 
00138 #define CGAL_check_bounds(a,b,c)
00139 #endif
00140 
00141 struct Set_log_state{
00142   Set_log_state(Log::Level l) {
00143     old_level_= CGAL_GET_LOG_LEVEL;
00144     CGAL_SET_LOG_LEVEL(l);
00145   }
00146   ~Set_log_state() {
00147     CGAL_SET_LOG_LEVEL(old_level_);
00148   }
00149 
00150   Log::Level old_level_;
00151 };
00152 
00153 CGAL_END_NAMESPACE
00154 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines