| 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 | CConsoleLogger::CConsoleLogger(bool bLogTimes, ELogLevel eMinLevel) |
|---|
| 29 | { |
|---|
| 30 | m_bLogTimes=bLogTimes; |
|---|
| 31 | |
|---|
| 32 | // Setup colors |
|---|
| 33 | memset(m_wColors,0xFF,sizeof(WORD)*255); |
|---|
| 34 | m_wColors[LOG_INFO] =FOREGROUND_GREEN|FOREGROUND_INTENSITY; |
|---|
| 35 | m_wColors[LOG_DEBUG] =FOREGROUND_BLUE|FOREGROUND_INTENSITY; |
|---|
| 36 | m_wColors[LOG_WARNING] =FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY; |
|---|
| 37 | m_wColors[LOG_ERROR] =FOREGROUND_RED|FOREGROUND_INTENSITY; |
|---|
| 38 | |
|---|
| 39 | m_eMinLevel=eMinLevel; |
|---|
| 40 | AllocConsole(); |
|---|
| 41 | m_hConsoleHandle=GetStdHandle(STD_OUTPUT_HANDLE); |
|---|
| 42 | } |
|---|
| 43 | CConsoleLogger::~CConsoleLogger(void) |
|---|
| 44 | { |
|---|
| 45 | FreeConsole(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | // Set level color. Mostly used for custom logging levels. |
|---|
| 49 | void CConsoleLogger::SetLevelColor(int iMessageLevel, WORD wColor) |
|---|
| 50 | { |
|---|
| 51 | if(iMessageLevel<255 && iMessageLevel>=0) m_wColors[iMessageLevel]=wColor; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | // Called from the logsystem |
|---|
| 55 | void CConsoleLogger::ReceiveLogMessage(SYSTEMTIME &LogTime, const char *pszTimeStr, int iMessageLevel, const char *pszMessage) |
|---|
| 56 | { |
|---|
| 57 | // Do we want to write the message? |
|---|
| 58 | if(iMessageLevel<m_eMinLevel) return; |
|---|
| 59 | |
|---|
| 60 | // Setup color |
|---|
| 61 | if(iMessageLevel<255 && m_wColors[iMessageLevel]!=0xFFFF) |
|---|
| 62 | SetConsoleTextAttribute(m_hConsoleHandle, m_wColors[iMessageLevel]); |
|---|
| 63 | |
|---|
| 64 | // Write to console |
|---|
| 65 | DWORD dwWritten; |
|---|
| 66 | if(m_bLogTimes) |
|---|
| 67 | WriteConsole(m_hConsoleHandle, pszTimeStr, (DWORD)strlen(pszTimeStr), &dwWritten, NULL); |
|---|
| 68 | WriteConsole(m_hConsoleHandle, pszMessage, (DWORD)strlen(pszMessage), &dwWritten, NULL); |
|---|
| 69 | } |
|---|