threaddict.cpp

Go to the documentation of this file.
00001 
00008 /**************************************************************************
00009 
00010    begin                : Sat Sep 09 2007
00011    copyright            : (C) 2007 by Ewald Arnold
00012    email                : log4sendpp at ewald-arnold dot de
00013 
00014    This program is free software; you can redistribute it and/or modify
00015    it under the terms of the GNU Lesser General Public License as
00016    published by the Free Software Foundation; either version 2 of the License,
00017    or (at your option) any later version.
00018 
00019    This program is distributed in the hope that it will be useful,
00020    but WITHOUT ANY WARRANTY; without even the implied warranty of
00021    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022    GNU General Public License for more details.
00023 
00024    You should have received a copy of the GNU Lesser General Public License
00025    along with this program; if not, write to the Free Software
00026    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00027 
00028  **/
00029 
00030 
00032 #define LOG4SENDPP_NEED_EXPORTS
00033 #include <log4sendpp/log4sendpp.h>  // always first header
00034 
00035 #include <log4sendpp/mutex.h>
00036 #include <log4sendpp/threaddict.h>
00037 #include <log4sendpp/arguments.h>
00038 
00039 
00040 LOG4SENDPP_NS_START
00041 
00042 
00043 LOG4SENDPP_ANON_NS_START
00044 
00045   Mutex             dictMutex;  
00046 
00047 LOG4SENDPP_NS_END
00048 
00049 
00050 ThreadDictionary ThreadDictionary::threadDict;
00051 
00052 
00053 LOG4SENDPP_API_IMPL0 ThreadDictionary::ThreadDictionary()
00054 {
00055 }
00056 
00057 
00058 LOG4SENDPP_API_IMPL(void)
00059   ThreadDictionary::add(Handle handle, const LOG4SENDPP_STD_NS::string &nickname)
00060 {
00061   LOG4SENDPP_STD_NS::string name = nickname;
00062   if (nickname.length() == 0)
00063     name = LOG4SENDPP_STD_NS::string("0x") + LOG4SENDPP_NS::number((unsigned long) handle, 16);
00064 
00065   Mutex::Locker lock (dictMutex);
00066 
00067   handles.push_back(handle);
00068   names.push_back(name);
00069 }
00070 
00071 
00072 LOG4SENDPP_API_IMPL(void) ThreadDictionary::remove(Handle handle)
00073 {
00074   Mutex::Locker lock (dictMutex);
00075 
00076   for (int i = handles.size()-1; i >= 0; --i)
00077     if (handles[i] == handle)
00078     {
00079       handles.erase(handles.begin() + i);
00080       names.erase(names.begin() + i);
00081     }
00082 }
00083 
00084 
00085 LOG4SENDPP_API_IMPL(unsigned) ThreadDictionary::numThreads() const
00086 {
00087   Mutex::Locker lock (dictMutex);
00088 
00089   return handles.size();
00090 }
00091 
00092 
00093 LOG4SENDPP_API_IMPL(LOG4SENDPP_STD_NS::string)
00094   ThreadDictionary::getNickname(Handle handle) const
00095 {
00096   Mutex::Locker lock (dictMutex);
00097 
00098   for (unsigned i = 0; i < handles.size(); ++i)
00099     if (handles[i] == handle)
00100       return names[i];
00101 
00102   return "??";
00103 }
00104 
00105 
00106 LOG4SENDPP_API_IMPL(ThreadDictionary::Handle)
00107   ThreadDictionary::getCurrentHandle()
00108 {
00109 #ifdef __WIN32__
00110   return (HANDLE) GetCurrentThreadId();
00111 #elif defined (__unix__)
00112    return pthread_self();
00113 #else
00114  #error Plattform spezifischer Code fehlt
00115 #endif
00116 }
00117 
00118 
00119 LOG4SENDPP_API_IMPL(LOG4SENDPP_STD_NS::string)
00120   ThreadDictionary::getCurrentNickname()
00121 {
00122   return get()->getNickname(getCurrentHandle());
00123 }
00124 
00125 
00126 LOG4SENDPP_API_IMPL(ThreadDictionary *) ThreadDictionary::get()
00127 {
00128   return &threadDict;
00129 }
00130 
00131 
00132 LOG4SENDPP_NS_END
00133 

Generated on Sat Nov 24 14:41:22 2007 for log4sendpp by  doxygen 1.5.3