MercuryLog.cpp

Go to the documentation of this file.
00001 #include "MercuryLog.h"
00002 #include "MercuryINI.h"
00003 #include "MercuryUtil.h"
00004 #include "MercuryMessages.h"
00005 #include <stdio.h>
00006 
00008 MercuryLog LOG;
00009 
00010 #ifndef _EE
00011 #define INFO_FILE "info.txt"
00012 #define LOG_FILE "log.txt"
00013 #else
00014 #define INFO_FILE "host:info.txt"
00015 #define LOG_FILE "host:log.txt"
00016 #endif
00017 
00018 //For Broadcast Logging
00019 void MESSAGE_LOG( const MString &message, void * data, PStack & info )
00020 {
00021     switch ( (MVPtr)data )
00022     {
00023     case 1:
00024         LOG.Info( ssprintf( "%s", info.PopItem().GetValueS().c_str() ) );
00025         break;
00026     case 2:
00027         LOG.Log( ssprintf( "%s", info.PopItem().GetValueS().c_str() ) );
00028         break;
00029     case 3:
00030         LOG.Warn( ssprintf( "%s", info.PopItem().GetValueS().c_str() ) );
00031         break;
00032     };
00033 }
00034 
00035 MercuryLog::MercuryLog()
00036 {
00037     m_infoVerbose = 0;
00038     m_logVerbose = 0;
00039     m_warnVerbose = 0;
00040 }
00041 
00042 void MercuryLog::Warn( const MString & message, int verbosity )
00043 {
00044     if (verbosity > m_warnVerbose)
00045         return;
00046 
00047     m_mutex.Wait();
00048     Log("***********************************************");
00049     Log("WARNING:" + message );
00050     Log("***********************************************");
00051 
00052     Info("WARNING:" + message );
00053     m_mutex.UnLock();
00054 }
00055 
00056 void MercuryLog::Log( const MString & message, int verbosity )
00057 {
00058     if (verbosity > m_logVerbose)
00059         return;
00060 
00061     m_mutex.Wait();
00062     CheckInit();
00063     float CurTime = float(m_tmrStart->Age());
00064     int Min = int(CurTime) / 60;
00065     int Sec = int(CurTime) % 60;
00066     int Ms = int( CurTime * 1000.0f ) - int( CurTime ) * 1000  ;
00067     MString LogText = ssprintf( "%02d:%02d.%03d: %s\n", Min, Sec, Ms, message.c_str() );
00068     if ( m_bLogToDisk )
00069         fwrite( LogText.c_str(), LogText.length(), 1, m_fileLog );
00070     if ( m_bShowLogInConsole )
00071         printf( "%s", LogText.c_str() );
00072     m_mutex.UnLock();
00073 }
00074 
00075 void MercuryLog::Info( const MString & message, int verbosity )
00076 {
00077     if (verbosity > m_infoVerbose)
00078         return;
00079 
00080     m_mutex.Wait();
00081     CheckInit();
00082     MString LogText = ssprintf( "%s\n", message.c_str() );
00083     if ( m_bInfoToDisk )
00084         fwrite( LogText.c_str(), LogText.length(), 1, m_fileInfo );
00085     if ( m_bShowInfoInConsole )
00086         printf( "%s", LogText.c_str() );
00087     m_mutex.UnLock();
00088 }
00089 
00090 void MercuryLog::CheckInit()
00091 {
00092     if ( m_bInitilized )
00093         return;
00094 
00095     m_bInitilized = true;
00096     CheckPREFSMANSetup();
00097     m_bLogToDisk = PREFSMAN->GetValueB( "Log", "LogToDisk", true, true );
00098     m_bInfoToDisk = PREFSMAN->GetValueB( "Log", "InfoToDisk", true, true );
00099     m_bShowLogInConsole = PREFSMAN->GetValueB( "Log", "ShowLogInConsole", true, true );
00100     m_bShowInfoInConsole = PREFSMAN->GetValueB( "Log", "ShowInfoInConsole", true, true );
00101 
00102     m_tmrStart = new MercuryTimer;
00103     m_tmrStart->Touch();
00104 
00105     if ( m_bLogToDisk )
00106         m_fileLog = fopen( LOG_FILE, "w" );
00107 
00108     if ( m_bInfoToDisk )
00109         m_fileInfo = fopen( INFO_FILE, "w" );
00110 
00111     if ( MESSAGEMAN == NULL )
00112         MESSAGEMAN = new MercuryMessageManager;
00113 
00114     MESSAGEMAN->Subscribe( "LOG_Info", MercuryCallback(MESSAGE_LOG,(void*)1) );
00115     MESSAGEMAN->Subscribe( "LOG_Log", MercuryCallback(MESSAGE_LOG,(void*)2) );
00116     MESSAGEMAN->Subscribe( "LOG_Trace", MercuryCallback(MESSAGE_LOG,(void*)2) );
00117     MESSAGEMAN->Subscribe( "LOG_Warn", MercuryCallback(MESSAGE_LOG,(void*)3) );
00118 }
00119 
00120 /* 
00121  * Copyright (c) 2005-2006, Charles Lohr
00122  * All rights reserved.
00123  *
00124  * Redistribution and use in source and binary forms, with or
00125  * without modification, are permitted provided that the following
00126  * conditions are met:
00127  *  -   Redistributions of source code must retain the above
00128  *      copyright notice, this list of conditions and the following disclaimer.
00129  *  -   Redistributions in binary form must reproduce the above copyright
00130  *      notice, this list of conditions and the following disclaimer in
00131  *      the documentation and/or other materials provided with the distribution.
00132  *  -   Neither the name of the Mercury Engine nor the names of its
00133  *      contributors may be used to endorse or promote products derived from
00134  *      this software without specific prior written permission.
00135  *
00136  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00137  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00138  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00139  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00140  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00141  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00142  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00143  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00144  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00145  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00146  */

Hosted by SourceForge.net Logo