00001 #ifndef GSGL_DATA_LOG_H 00002 #define GSGL_DATA_LOG_H 00003 00004 // 00005 // $Id: log.hpp 2 2008-03-01 20:58:50Z kulibali $ 00006 // 00007 // Copyright (c) 2008, The Periapsis Project. All rights reserved. 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are 00011 // met: 00012 // 00013 // * Redistributions of source code must retain the above copyright notice, 00014 // this list of conditions and the following disclaimer. 00015 // 00016 // * Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // * Neither the name of the The Periapsis Project nor the names of its 00021 // contributors may be used to endorse or promote products derived from 00022 // this software without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00025 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00026 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00027 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00028 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00029 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00030 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00031 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00034 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 // 00036 00037 #include "data/data.hpp" 00038 #include "data/global.hpp" 00039 #include "data/string.hpp" 00040 00041 namespace gsgl 00042 { 00043 00044 namespace io 00045 { 00046 class ft_stream; 00047 } 00048 00049 00050 namespace data 00051 { 00052 00053 /// Base class for log targets. 00054 class DATA_API log_target 00055 { 00056 public: 00057 log_target(); 00058 virtual ~log_target(); 00059 00060 virtual void print_line(const gsgl::string &) = 0; 00061 }; // class log_target 00062 00063 00064 /// Global logger that sends log lines to the various log targets. 00065 /// The global logger object will be created on the first call to \c global_logger(), and will destroy itself when the last log target goes out of scope. 00066 class DATA_API logger 00067 : public global_register<log_target> 00068 { 00069 static int global_log_level; 00070 00071 logger(); 00072 virtual ~logger(); 00073 00074 public: 00075 enum 00076 { 00077 LOG_LEVEL_NONE = 0, 00078 LOG_LEVEL_BASIC = 1, 00079 LOG_LEVEL_MEDIUM = 5, 00080 LOG_LEVEL_HIGH = 10, 00081 LOG_LEVEL_ULTRA = 100 00082 }; 00083 00084 void print_line(int log_level, const gsgl::string &); 00085 00086 static void set_global_log_level(int new_log_level); 00087 00088 /// Reimplemented to enable auto-creation. 00089 static logger *global_instance(); 00090 }; // class logger 00091 00092 00093 /// A log target that prints to a file. 00094 class DATA_API file_log_target 00095 : public log_target 00096 { 00097 io::ft_stream *f; 00098 00099 public: 00100 file_log_target(const gsgl::string & fname); 00101 virtual ~file_log_target(); 00102 00103 void print_line(const gsgl::string &); 00104 }; // class file_log_target 00105 00106 00107 /// A log target that prints to a console (or debug output). 00108 class DATA_API debug_log_target 00109 : public data::singleton<debug_log_target>, public log_target 00110 { 00111 public: 00112 debug_log_target(); 00113 virtual ~debug_log_target(); 00114 00115 void print_line(const gsgl::string &); 00116 }; // class debug_log_target 00117 00118 00119 } // namespace data 00120 00121 00122 DATA_API inline void log(int log_level, const gsgl::string & s) 00123 { 00124 gsgl::data::logger *lgr = gsgl::data::logger::global_instance(); 00125 if (lgr) 00126 lgr->print_line(log_level, s); 00127 } // log() 00128 00129 00130 DATA_API inline void log(const gsgl::string & s) 00131 { 00132 gsgl::data::logger *lgr = gsgl::data::logger::global_instance(); 00133 if (lgr) 00134 lgr->print_line(gsgl::data::logger::LOG_LEVEL_BASIC, s); 00135 } // log() 00136 00137 00138 } // namespage gsgl 00139 00140 #endif