Back to index

lightning-sunbird  0.9+nobinonly
logger.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "xp.h"
00039 #include "logger.h"
00040 #include "loghlp.h"
00041 
00042 #ifdef XP_MAC
00043        CLogger *pLogger = new CLogger();
00044 #endif
00045 
00046 /********************************/
00047 /*       CLogger class          */
00048 /********************************/
00049 
00050 CLogger::CLogger(LPSTR szTarget) :
00051   m_pPlugin(NULL),
00052   m_pPluginInstance(NULL),
00053   m_pLog(NULL),
00054   m_pLogFile(NULL),
00055   m_bShowImmediately(FALSE),
00056   m_bLogToFile(FALSE),
00057   m_bLogToFrame(TRUE),
00058   m_bBlockLogToFile(TRUE),
00059   m_bBlockLogToFrame(FALSE),
00060   m_pStream(NULL),
00061   m_dwStartTime(0xFFFFFFFF),
00062   m_iStringDataWrap(LOGGER_DEFAULT_STRING_WRAP),
00063   m_bStale(FALSE)
00064 {
00065   if(szTarget != NULL)
00066     strcpy(m_szTarget, szTarget);
00067   else
00068     strcpy(m_szTarget, "");
00069 
00070   m_pLog = new CLogItemList();
00071   strcpy(m_szStreamType, "text/plain");
00072 }
00073 
00074 CLogger::~CLogger()
00075 {
00076   if(m_pLogFile != NULL)
00077     delete m_pLogFile;
00078 
00079   if(m_pLog != NULL)
00080     delete m_pLog;
00081 }
00082 
00083 void CLogger::associate(CPluginBase * pPlugin)
00084 {
00085   m_pPlugin = pPlugin;
00086   m_pPluginInstance = m_pPlugin->getNPInstance();
00087 }
00088 
00089 void CLogger::restorePreferences(LPSTR szFileName)
00090 {
00091   XP_GetPrivateProfileString(SECTION_LOG, KEY_RECORD_SEPARATOR, "", m_szItemSeparator, 
00092                              sizeof(m_szItemSeparator), szFileName);
00093   m_iStringDataWrap = XP_GetPrivateProfileInt(SECTION_LOG, KEY_STRING_WRAP, 
00094                                               LOGGER_DEFAULT_STRING_WRAP, szFileName);
00095 }
00096 
00097 BOOL CLogger::onNPP_DestroyStream(NPStream * npStream)
00098 {
00099   if(npStream == m_pStream) // Navigator itself destroys it
00100   {
00101     m_pStream = NULL;
00102     return TRUE;
00103   }
00104   return FALSE;
00105 }
00106 
00107 static void FixUpOutputString(char * aString)
00108 {
00109   // replace angle brackets with rect brackets
00110   char * p = aString;
00111   while((p = strstr(p, "<")))
00112     *p = '[';
00113 
00114   p = aString;
00115   while((p = strstr(p, ">")))
00116     *p = ']';
00117 }
00118 
00119 BOOL CLogger::appendToLog(NPAPI_Action action, DWORD dwTickEnter, DWORD dwTickReturn, 
00120                           DWORD dwRet, 
00121                           DWORD dw1, DWORD dw2, DWORD dw3, DWORD dw4, 
00122                           DWORD dw5, DWORD dw6, DWORD dw7)
00123 {
00124   if(m_pLog == NULL)
00125     return FALSE;
00126 
00127   if(!m_bLogToFrame && !m_bLogToFile)
00128     return TRUE;
00129 
00130   DWORD dwTimeEnter;
00131   DWORD dwTimeReturn;
00132   if(m_dwStartTime == 0xFFFFFFFF)
00133   {
00134     m_dwStartTime = dwTickEnter;
00135     dwTimeEnter = 0L;
00136   }
00137   else
00138     dwTimeEnter = dwTickEnter - m_dwStartTime;
00139 
00140   dwTimeReturn = dwTickReturn - m_dwStartTime;
00141 
00142   LogItemStruct * plis = makeLogItemStruct(action, dwTimeEnter, dwTimeReturn, dwRet, 
00143                                            dw1, dw2, dw3, dw4, dw5, dw6, dw7);
00144 
00145   static char szOutput[1024];
00146 
00147   // dump to file
00148   if(m_bLogToFile && !m_bBlockLogToFile)
00149   {
00150     if(m_pLogFile == NULL)
00151     {
00152       m_pLogFile = new CLogFile();
00153       if(m_pLogFile == NULL)
00154         return FALSE;
00155 
00156       char szFile[256];
00157       m_pPlugin->getLogFileName(szFile, sizeof(szFile));
00158 
00159       if(m_pPlugin->getMode() == NP_EMBED)
00160       {
00161         if(!m_pLogFile->create(szFile, FALSE))
00162         {
00163           char szMessage[512];
00164           wsprintf(szMessage, "File '%s'\n probably exists. Overwrite?", szFile);
00165           if(IDYES == m_pPlugin->messageBox(szMessage, "", MB_ICONQUESTION | MB_YESNO))
00166           {
00167             if(!m_pLogFile->create(szFile, TRUE))
00168             {
00169               m_pPlugin->messageBox("Cannot create file.", "", MB_ICONERROR | MB_OK);
00170               delete m_pLogFile;
00171               m_pLogFile = NULL;
00172               return FALSE;
00173             }
00174           }
00175           else
00176           {
00177             delete m_pLogFile;
00178             m_pLogFile = NULL;
00179             goto Frame;
00180           }
00181         }
00182       }
00183       else // NP_FULL
00184       {
00185         if(!m_pLogFile->create(szFile, TRUE))
00186         {
00187           delete m_pLogFile;
00188           m_pLogFile = NULL;
00189           return FALSE;
00190         }
00191       }
00192     }
00193 
00194     formatLogItem(plis, szOutput, m_szItemSeparator, TRUE);
00195     m_pLogFile->write(szOutput);
00196     m_pLogFile->flush();
00197   }
00198 
00199 Frame:
00200 
00201   // dump to frame
00202   if(m_bLogToFrame && !m_bBlockLogToFrame)
00203   {
00204     if(m_bShowImmediately)
00205     {
00206       BOOL dosstyle = (m_pPlugin && m_pPlugin->isStandAlone());
00207 
00208       int iLength = formatLogItem(plis, szOutput, "", dosstyle);
00209 
00210       // we should fix the output string if it contains symbols <html
00211       // If this is the case the browser will display the whole output
00212       // in HTML format while we still want it to be in plain text.
00213       // I do not know if this is a bug in the browser or not.
00214       FixUpOutputString(szOutput);
00215 
00216       if (m_pPlugin && m_pPlugin->isStandAlone())
00217       {
00218         m_pPlugin->outputToNativeWindow(szOutput);
00219       }
00220       else
00221       {
00222         if(m_pStream == NULL)
00223           NPN_NewStream(m_pPluginInstance, m_szStreamType, m_szTarget, &m_pStream);
00224 
00225         NPN_Write(m_pPluginInstance, m_pStream, iLength, (void *)szOutput);
00226       }
00227       delete plis;
00228     }
00229     else
00230       m_pLog->add(plis);
00231   }
00232 
00233   return TRUE;
00234 }
00235 
00236 void CLogger::setShowImmediatelyFlag(BOOL bFlagState)
00237 {
00238   m_bShowImmediately = bFlagState;
00239 }
00240 
00241 BOOL CLogger::getShowImmediatelyFlag()
00242 {
00243   return m_bShowImmediately;
00244 }
00245 
00246 void CLogger::setLogToFileFlag(BOOL bFlagState)
00247 {
00248   m_bLogToFile = bFlagState;
00249 }
00250 
00251 BOOL CLogger::getLogToFileFlag()
00252 {
00253   return m_bLogToFile;
00254 }
00255 
00256 void CLogger::setLogToFrameFlag(BOOL bFlagState)
00257 {
00258   m_bLogToFrame = bFlagState;
00259 }
00260 
00261 BOOL CLogger::getLogToFrameFlag()
00262 {
00263   return m_bLogToFrame;
00264 }
00265 
00266 int CLogger::getStringDataWrap()
00267 {
00268   return m_iStringDataWrap;
00269 }
00270 
00271 void CLogger::clearLog()
00272 {
00273   if(m_pLog != NULL)
00274     delete m_pLog;
00275 
00276   m_pLog = new CLogItemList();
00277 }
00278 
00279 void CLogger::clearTarget()
00280 {
00281   if (m_pPlugin && m_pPlugin->isStandAlone())
00282   {
00283     m_pPlugin->outputToNativeWindow("");
00284   }
00285   else
00286   {
00287     if(m_pStream != NULL)
00288       NPN_DestroyStream(m_pPluginInstance, m_pStream, NPRES_DONE);
00289 
00290     NPN_NewStream(m_pPluginInstance, m_szStreamType, m_szTarget, &m_pStream);
00291     NPN_Write(m_pPluginInstance, m_pStream, 1, (void *)"\n");
00292 
00293     if(!m_bShowImmediately)
00294     {
00295       NPN_DestroyStream(m_pPluginInstance, m_pStream, NPRES_DONE);
00296       m_pStream = NULL;
00297     }
00298   }
00299 }
00300 
00301 void CLogger::resetStartTime()
00302 {
00303   m_dwStartTime = XP_GetTickCount();
00304 }
00305 
00306 void CLogger::dumpLogToTarget()
00307 {
00308   if(m_pLog == NULL)
00309     return;
00310 
00311   static char szOutput[1024];
00312 
00313   if (m_pPlugin && m_pPlugin->isStandAlone())
00314   {
00315     for(LogItemListElement * plile = m_pLog->m_pFirst; plile != NULL; plile = plile->pNext)
00316     {
00317       formatLogItem(plile->plis, szOutput, "", TRUE);
00318       m_pPlugin->outputToNativeWindow(szOutput);
00319     }
00320   }
00321   else
00322   {
00323     BOOL bTemporaryStream = ((m_pStream == NULL) && !getShowImmediatelyFlag());
00324 
00325     if(m_pStream == NULL)
00326       NPN_NewStream(m_pPluginInstance, m_szStreamType, m_szTarget, &m_pStream);
00327 
00328     for(LogItemListElement * plile = m_pLog->m_pFirst; plile != NULL; plile = plile->pNext)
00329     {
00330       int iLength = formatLogItem(plile->plis, szOutput, "");
00331       NPN_Write(m_pPluginInstance, m_pStream, iLength, (void *)szOutput);
00332     }
00333 
00334     if(bTemporaryStream)
00335     {
00336       NPN_DestroyStream(m_pPluginInstance, m_pStream, NPRES_DONE);
00337       m_pStream = NULL;
00338     }
00339   }
00340 }
00341 
00342 void CLogger::closeLogToFile()
00343 {
00344   if(m_pLogFile != NULL)
00345   {
00346     delete m_pLogFile;
00347     m_pLogFile = NULL;
00348   }
00349 }
00350 
00351 void CLogger::blockDumpToFile(BOOL bBlock)
00352 {
00353   m_bBlockLogToFile = bBlock;
00354 }
00355 
00356 void CLogger::blockDumpToFrame(BOOL bBlock)
00357 {
00358   m_bBlockLogToFrame = bBlock;
00359 }
00360 
00361 void CLogger::markStale()
00362 {
00363   m_bStale = TRUE;
00364 }
00365 
00366 BOOL CLogger::isStale()
00367 {
00368   return m_bStale;
00369 }