| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of LEXIExporter |
|---|
| 4 | |
|---|
| 5 | Copyright 2006 NDS Limited |
|---|
| 6 | |
|---|
| 7 | Author(s): |
|---|
| 8 | Lasse Tassing |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | ----------------------------------------------------------------------------- |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #include "StdAfx.h" |
|---|
| 27 | |
|---|
| 28 | // Static instance pointer |
|---|
| 29 | CLogSystem* CLogSystem::m_pThis=NULL; |
|---|
| 30 | |
|---|
| 31 | CLogSystem::CLogSystem(void) |
|---|
| 32 | { |
|---|
| 33 | m_eMinLogLevel=LOG_INFO; |
|---|
| 34 | InitializeCriticalSection(&m_Crit); |
|---|
| 35 | } |
|---|
| 36 | CLogSystem::~CLogSystem(void) |
|---|
| 37 | { |
|---|
| 38 | if(m_pThis==this) m_pThis=NULL; |
|---|
| 39 | |
|---|
| 40 | EnterCriticalSection(&m_Crit); |
|---|
| 41 | |
|---|
| 42 | // Release all receivers |
|---|
| 43 | for(unsigned i=0;i<m_lReceivers.size();i++) |
|---|
| 44 | m_lReceivers[i]->Release(); |
|---|
| 45 | m_lReceivers.clear(); |
|---|
| 46 | |
|---|
| 47 | DeleteCriticalSection(&m_Crit); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | // Static get function - so we can reach the logsystem |
|---|
| 51 | CLogSystem *CLogSystem::Get(void) |
|---|
| 52 | { |
|---|
| 53 | if(m_pThis==NULL) m_pThis=new CLogSystem; |
|---|
| 54 | return m_pThis; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | // Add a custom level message |
|---|
| 58 | void CLogSystem::LogMessage(const void *pModule, int iLevel, const char *pszText, ...) |
|---|
| 59 | { |
|---|
| 60 | // Check if we want to log this message? |
|---|
| 61 | if(iLevel<m_eMinLogLevel) return; |
|---|
| 62 | |
|---|
| 63 | EnterCriticalSection(&m_Crit); |
|---|
| 64 | try |
|---|
| 65 | { |
|---|
| 66 | static char buff1[4096]; |
|---|
| 67 | static char buff2[4096]; |
|---|
| 68 | |
|---|
| 69 | // Get the module descriptor |
|---|
| 70 | char *pszModule="Legacy"; |
|---|
| 71 | if(pModule) |
|---|
| 72 | { |
|---|
| 73 | pszModule="Unknown"; |
|---|
| 74 | m_lModules.find((unsigned int)pModule, pszModule); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // Parse the string |
|---|
| 78 | va_list args; |
|---|
| 79 | va_start(args, pszText); |
|---|
| 80 | _vsnprintf(buff1, sizeof(buff1) , pszText, args); |
|---|
| 81 | |
|---|
| 82 | // Create string containing both module and logmessage |
|---|
| 83 | _snprintf(buff2, sizeof(buff2), "%s: %s\r\n", pszModule, buff1); |
|---|
| 84 | |
|---|
| 85 | // Get timestamp |
|---|
| 86 | SYSTEMTIME lSysTime; |
|---|
| 87 | GetLocalTime(&lSysTime); |
|---|
| 88 | _snprintf(buff1, sizeof(buff1), "%02u:%02u:%02u/%02u", lSysTime.wHour, lSysTime.wMinute, lSysTime.wSecond, lSysTime.wMilliseconds); |
|---|
| 89 | |
|---|
| 90 | // Distribute message to receivers |
|---|
| 91 | for(unsigned i=0;i<m_lReceivers.size();i++) |
|---|
| 92 | if(m_lReceivers[i]!=pModule) m_lReceivers[i]->ReceiveLogMessage(lSysTime, buff1, iLevel, buff2); |
|---|
| 93 | |
|---|
| 94 | } catch(...) |
|---|
| 95 | { |
|---|
| 96 | // Nothing much to do, is there? |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | LeaveCriticalSection(&m_Crit); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | // Register a system module |
|---|
| 103 | void CLogSystem::RegisterModule(const void *pModulePointer, const char *pszModuleDesc) |
|---|
| 104 | { |
|---|
| 105 | EnterCriticalSection(&m_Crit); |
|---|
| 106 | m_lModules.map((int)pModulePointer, _strdup(pszModuleDesc)); |
|---|
| 107 | LeaveCriticalSection(&m_Crit); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | // Register a system module |
|---|
| 111 | void CLogSystem::UnregisterModule(const void *pModulePointer) |
|---|
| 112 | { |
|---|
| 113 | EnterCriticalSection(&m_Crit); |
|---|
| 114 | char *pOldDesc=NULL; |
|---|
| 115 | m_lModules.erase((int)pModulePointer, pOldDesc); |
|---|
| 116 | if(pOldDesc) free(pOldDesc); |
|---|
| 117 | LeaveCriticalSection(&m_Crit); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | // Add a log receiver |
|---|
| 121 | void CLogSystem::AddReceiver(ILogReceiver *pReceiver) |
|---|
| 122 | { |
|---|
| 123 | pReceiver->AddRef(); |
|---|
| 124 | m_lReceivers.push_back(pReceiver); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // Remove a log receiver |
|---|
| 128 | void CLogSystem::RemoveReceiver(ILogReceiver *pReceiver) |
|---|
| 129 | { |
|---|
| 130 | for(unsigned int i=0;i<m_lReceivers.size();i++) |
|---|
| 131 | { |
|---|
| 132 | if(m_lReceivers[i]==pReceiver) |
|---|
| 133 | { |
|---|
| 134 | pReceiver->Release(); |
|---|
| 135 | m_lReceivers.erase(i); |
|---|
| 136 | return; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | } |
|---|