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 
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 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 
00143 
00144 
00145 
00146