log_base.cpp

Go to the documentation of this file.
00001 #ifndef LOG_BASE_IMPLEMENTATION_FILE
00002 #define LOG_BASE_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : log_base                                                          *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1996-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "function.h"
00019 #include "istring.h"
00020 #include "log_base.h"
00021 #include "mutex.h"
00022 #include "set.cpp"
00023 
00024 log_base::log_base()
00025 : _lock(new mutex),
00026   _filter_set(new int_set()),
00027   _ending(pick_ending_for_platform())
00028 {}
00029 
00030 
00031 log_base::log_base(const log_base &to_copy)
00032 : object_base(),
00033   _lock(new mutex),
00034   _filter_set(new int_set(*to_copy._filter_set)),
00035   _ending(to_copy._ending)
00036 {}
00037 
00038 log_base::~log_base()
00039 {
00040   WHACK(_filter_set);
00041   WHACK(_lock);
00042 }
00043 
00044 log_base &log_base::operator =(const log_base &to_copy)
00045 {
00046   if (this == &to_copy) return *this;
00047   *_filter_set = *to_copy._filter_set;
00048   _ending = to_copy._ending;
00049   return *this;
00050 }
00051 
00052 outcome log_base::log(const char *info, int filter)
00053 { return log(istring(info), filter); }
00054 
00055 void log_base::clear_filters()
00056 {
00057   auto_synchronizer l(*_lock);
00058   _filter_set->clear();
00059 }
00060 
00061 void log_base::remove_filter(int old_filter)
00062 {
00063   auto_synchronizer l(*_lock);
00064   _filter_set->remove(old_filter);
00065 }
00066 
00067 void log_base::add_filter(int new_filter)
00068 {
00069   auto_synchronizer l(*_lock);
00070   _filter_set->add(new_filter);
00071 }
00072 
00073 void log_base::stuff_filter_set(const int_set &new_list)
00074 {
00075   auto_synchronizer l(*_lock);
00076   *_filter_set = new_list;
00077 }
00078 
00079 bool log_base::member(int filter)
00080 {
00081   auto_synchronizer l(*_lock);
00082   if (filter == NEVER_PRINT) return false;
00083   if (_filter_set->member(ALWAYS_PRINT)) return true;
00084   if (_filter_set->member(NEVER_PRINT)) return false;
00085   return (filter == ALWAYS_PRINT) || _filter_set->member(filter);
00086 }
00087 
00088 int_set log_base::filter_set()
00089 {
00090   auto_synchronizer l(*_lock);
00091   return *_filter_set;
00092 }
00093 
00094 log_base::line_ending log_base::eol()
00095 {
00096   auto_synchronizer l(*_lock);
00097   return _ending;
00098 }
00099 
00100 void log_base::eol(line_ending to_set)
00101 {
00102   auto_synchronizer l(*_lock);
00103   _ending = (to_set == NO_ENDING)? NO_ENDING
00104       : (to_set == CRLF_AT_END)? CRLF_AT_END : LF_AT_END;
00105 }
00106 
00107 log_base::line_ending log_base::pick_ending_for_platform()
00108 {
00109 #ifdef __UNIX__
00110   // obviously a unix OS, unless someone's playing games with us.
00111   return LF_AT_END;
00112 #elif defined(__WIN32__)
00113   // smells like DOS.
00114   return CRLF_AT_END;
00115 #else
00116   // pick the unix default if we can't tell.
00117   return LF_AT_END;
00118 #endif
00119 }
00120 
00121 const char *log_base::string_for_ending(line_ending end)
00122 {
00123   static const char *CRLF_AT_END_STRING = "\r\n";
00124   static const char *LF_AT_END_STRING = "\n";
00125   static const char *NO_ENDING_STRING = "";
00126 
00127   switch (end) {
00128     case CRLF_AT_END: return CRLF_AT_END_STRING;
00129     case NO_ENDING: return NO_ENDING_STRING;
00130     case LF_AT_END:  // fall-through to default.
00131     default: return LF_AT_END_STRING;
00132   }
00133 }
00134 
00135 const char *log_base::platform_ending()
00136 { return string_for_ending(pick_ending_for_platform()); }
00137 
00138 istring log_base::get_ending() { return string_for_ending(_ending); }
00139 
00140 void log_base::get_ending(istring &to_end) { to_end += get_ending(); }
00141 
00142 
00143 #endif //LOG_BASE_IMPLEMENTATION_FILE
00144 

Generated on Fri Nov 28 04:29:08 2008 for HOOPLE Libraries by  doxygen 1.5.1