00001 // ----------------------------------------------------------------------^ 00002 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone 00003 // (mailto: <gcalderone@ifc.inaf.it>) 00004 // 00005 // This file is part of MCS. 00006 // 00007 // MCS is free software; you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation; either version 2 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // MCS is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with MCS; if not, write to the Free Software 00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00020 // 00021 // ----------------------------------------------------------------------$ 00022 00023 00024 #include "mcs.hh" 00025 using namespace mcs; 00026 00027 00028 void mcs::Event::init(string atFile, unsigned int atLine, 00029 RetValue type, int code) 00030 { 00031 if (! flInitialized) { 00032 memset(vmsg, 0, sizeof(vmsg)); 00033 00034 if (custom_init_vmsg) 00035 (*custom_init_vmsg)(vmsg); 00036 00037 init_vmsg(); 00038 00039 flInitialized = true; 00040 } 00041 00042 latFile = atFile; 00043 latLine = atLine; 00044 lcode = code ; 00045 lsubcode = 0 ; 00046 ltype = type ; 00047 00048 buf[0] = '\0'; 00049 } 00050 00051 00052 00053 mcs::Event::Event(string atFile, unsigned int atLine, RetValue type, int code, 00054 string s1, string s2, string s3) 00055 { 00056 init(atFile, atLine, type, code); 00057 sprintf(buf, vmsg[code], s1.c_str(), s2.c_str(), s3.c_str()); 00058 lmsg = buf; 00059 } 00060 00061 mcs::Event::Event(string atFile, unsigned int atLine, RetValue type, int code, 00062 string s1, int i1) 00063 { 00064 init(atFile, atLine, type, code); 00065 sprintf(buf, vmsg[code], s1.c_str(), i1); 00066 lmsg = buf; 00067 lsubcode = i1; 00068 } 00069 00070 mcs::Event::Event(string atFile, unsigned int atLine, RetValue type, int code, 00071 int i1, string s1) 00072 { 00073 init(atFile, atLine, type, code); 00074 sprintf(buf, vmsg[code], i1, s1.c_str()); 00075 lmsg = buf; 00076 } 00077 00078 mcs::Event::Event(string atFile, unsigned int atLine, RetValue type, int code, 00079 int i1, int i2) 00080 { 00081 init(atFile, atLine, type, code); 00082 sprintf(buf, vmsg[code], i1, i2); 00083 lmsg = buf; 00084 lsubcode = i2; 00085 } 00086 00087 00088 00089 mcs::Event::~Event() 00090 {} 00091 00092 int mcs::Event::code() 00093 { 00094 return lcode; 00095 } 00096 00097 00098 00099 RetValue mcs::Event::type() 00100 { 00101 return ltype; 00102 } 00103 00104 string mcs::Event::msg() 00105 { 00106 return lmsg; 00107 } 00108 00109 00110 string mcs::Event::where() 00111 { 00112 string lerror; 00113 char buf[30]; 00114 00115 sprintf(buf, "%d", latLine); 00116 lerror = latFile + ":" + string(buf); 00117 00118 sprintf(buf, " (%d) ", lcode); 00119 00120 return lerror; 00121 } 00122 00123 00124 string mcs::Event::file() 00125 { 00126 return latFile; 00127 } 00128 00129 00130 unsigned int mcs::Event::line() 00131 { 00132 return latLine; 00133 } 00134 00135 00136 string mcs::Event::codemsg() 00137 { 00138 char buf[5]; 00139 sprintf(buf, "#%03d", code()); 00140 return (buf + msg()); 00141 } 00142 00143 00144 00145 int mcs::Event::subcode() 00146 { 00147 return lsubcode; 00148 } 00149 00150 Event& mcs::Event::subcode(int subcode) 00151 { 00152 lsubcode = subcode; 00153 return *this; 00154 } 00155 00156 string mcs::Event::static_vmsg(unsigned int i) 00157 { 00158 string ret = ""; 00159 00160 if ((i>=0) && (i < MCS_VMSG_SIZE)) 00161 if (vmsg[i]) 00162 ret = string(vmsg[i]); 00163 00164 return ret; 00165 } 00166 00167 00168 const char* mcs::Event::vmsg[MCS_VMSG_SIZE]; 00169 bool mcs::Event::flInitialized = false; 00170 void (*mcs::Event::custom_init_vmsg) (const char* vmsg[MCS_VMSG_SIZE]) = NULL; 00171 00172 00173 00174 00175 00176 00177 #ifndef DOXYGEN_SKIP 00178 #if ENABLE_DEBUG 00179 pthread_t mcs::Debug::thrs[MCS_DEBUG_NTHR]; 00180 bool mcs::Debug::thrmap[MCS_DEBUG_NTHR]; 00181 int mcs::Debug::dummy = mcs::Debug::init(); 00182 00183 int mcs::Debug::init() 00184 { 00185 int i; 00186 for (i=0; i<MCS_DEBUG_NTHR; i++) { 00187 thrs[i] = 0; 00188 thrmap[i] = false; 00189 } 00190 return 0; 00191 } 00192 00193 mcs::Debug::Debug() 00194 { 00195 active = false; 00196 pThis = 0; 00197 } 00198 00199 mcs::Debug::~Debug() 00200 {} 00201 00202 00203 void mcs::Debug::setDebug(string lclassName, void* lpthis, 00204 bool lactive) 00205 { 00206 className = lclassName; 00207 pThis = lpthis; 00208 active = lactive; 00209 } 00210 00211 void mcs::Debug::setDebug() 00212 { active = true; } 00213 00214 00215 bool mcs::Debug::getDebugFlag() 00216 { return active; } 00217 00218 00219 #define DEBUG_MAXLENGTH 80 00220 string mcs::Debug::debug(const char* file, int line, const char* function) 00221 { 00222 int i; 00223 bool found = false; 00224 for (i=0; i<MCS_DEBUG_NTHR; i++) { 00225 if (thrmap[i] && thrs[i] == pthread_self()) { 00226 found = true; 00227 break; 00228 } 00229 } 00230 00231 if (! found) { 00232 for (i=0; i<MCS_DEBUG_NTHR; i++) 00233 if (! thrmap[i]) { 00234 thrmap[i] = true; 00235 thrs[i] = pthread_self(); 00236 break; 00237 } 00238 } 00239 00240 00241 char buf[DEBUG_MAXLENGTH]; 00242 sprintf(buf, "*** THR %02d, %p, %s:%04d %s:%s ", i, pThis, file, line, 00243 className.c_str(), function); 00244 buf[DEBUG_MAXLENGTH - 1] = 0; 00245 return string(buf); 00246 } 00247 00248 00249 #endif //ENABLE_DEBUG 00250 #endif
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha3
|