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 
00040 #include "logger.h"
00041 #include "profile.h"
00042 #include "plugload.h"
00043 
00044 Logger::Logger() :
00045   bMutedAll(FALSE),
00046   bOnTop(TRUE),
00047   bToWindow(TRUE),
00048   bToConsole(FALSE),
00049   bToFile(FALSE),
00050   bSPALID(FALSE)
00051 {
00052   if(0 != GetPluginsDir(szFile, strlen(szFile)))
00053   {
00054     strcat(szFile, DIR_SEPARATOR);
00055     strcat(szFile, DEFAULT_LOG_FILE_NAME);
00056   }
00057   else
00058     szFile[0] = '\0';
00059 
00060   for(int i = 0; i < sizeof(bMutedCalls)/sizeof(BOOL); i++)
00061     bMutedCalls[i] = FALSE;
00062   
00063   bMutedCalls[action_npn_mem_alloc] = TRUE;
00064   bMutedCalls[action_npn_mem_free]  = TRUE;
00065   bMutedCalls[action_npn_mem_flush] = TRUE;
00066 }
00067 
00068 Logger::~Logger()
00069 {
00070 }
00071 
00072 BOOL Logger::init()
00073 {
00074   if(bToFile)
00075     filer.create(szFile, TRUE);
00076 
00077   return TRUE;
00078 }
00079 
00080 void Logger::shut()
00081 {
00082   filer.close();
00083 }
00084 
00085 #define MAX_OUTPUT_SIZE 8192
00086 
00087 void Logger::logNS_NP_GetEntryPoints()
00088 {
00089   char szLog[] = "NP_GetEntryPoints by Netscape\r\n";
00090 
00091   if(bToConsole)
00092     printf("%s", szLog);
00093   
00094   if(bToFile)
00095     filer.write(szLog);
00096 
00097   if(bToWindow)
00098     dumpStringToMainWindow(szLog);
00099 }
00100 
00101 void Logger::logNS_NP_Initialize()
00102 {
00103   char szLog[] = "NP_Initialize by Netscape\r\n";
00104 
00105   if(bToConsole)
00106     printf("%s", szLog);
00107   
00108   if(bToFile)
00109     filer.write(szLog);
00110 
00111   if(bToWindow)
00112     dumpStringToMainWindow(szLog);
00113 }
00114 
00115 void Logger::logNS_NP_Shutdown()
00116 {
00117   char szLog[] = "NP_Shutdown by Netscape\r\n";
00118 
00119   if(bToConsole)
00120     printf("%s", szLog);
00121   
00122   if(bToFile)
00123     filer.write(szLog);
00124 
00125   if(bToWindow)
00126     dumpStringToMainWindow(szLog);
00127 }
00128 
00129 void Logger::logSPY_NP_GetEntryPoints(NPPluginFuncs * pNPPFuncs)
00130 {
00131   char szLog[80] = "NP_GetEntryPoints by NPSpy\r\n";
00132 
00133   if(bToConsole)
00134     printf("%s", szLog);
00135   
00136   if(bToFile)
00137     filer.write(szLog);
00138 
00139   if(bToWindow)
00140     dumpStringToMainWindow(szLog);
00141 
00142   if(!pNPPFuncs)
00143     return;
00144 
00145   char szLog1[80],szLog2[80],szLog3[80],szLog4[80],szLog5[80],szLog6[80],szLog7[80],
00146        szLog8[80],szLog9[80],szLog10[80],szLog11[80],szLog12[80],szLog13[80],szLog14[80],
00147        szLog15[80],szLog16[80],szLog17[80],szLog18[80],szLog19[80],szLog20[80];
00148 
00149   sprintf(szLog1, "\r\n");
00150   sprintf(szLog2, "     Plugin entry point table\r\n");
00151   sprintf(szLog3, "     ========================\r\n");
00152 
00153   if(pNPPFuncs->size)
00154     sprintf(szLog4, "    size          = %i\r\n", pNPPFuncs->size);
00155   else
00156     sprintf(szLog4, "    size          = not set!\r\n");
00157 
00158   if(pNPPFuncs->version)
00159     sprintf(szLog5, "    version       = %i\r\n", pNPPFuncs->version);
00160   else
00161     sprintf(szLog5, "    version       = not set!\r\n");
00162   
00163   if(pNPPFuncs->newp)
00164     sprintf(szLog6, "    newp          = %#08lx\r\n", pNPPFuncs->newp);
00165   else
00166     sprintf(szLog6, "    newp          = not set!\r\n");
00167   
00168   if(pNPPFuncs->destroy)
00169     sprintf(szLog7, "    destroy       = %#08lx\r\n", pNPPFuncs->destroy);
00170   else
00171     sprintf(szLog7, "    destroy       = not set!\r\n");
00172   
00173   if(pNPPFuncs->setwindow)
00174     sprintf(szLog8, "    setwindow     = %#08lx\r\n", pNPPFuncs->setwindow);
00175   else
00176     sprintf(szLog8, "    setwindow     = not set!\r\n");
00177   
00178   if(pNPPFuncs->newstream)
00179     sprintf(szLog9, "    newstream     = %#08lx\r\n", pNPPFuncs->newstream);
00180   else
00181     sprintf(szLog9, "    newstream     = not set!\r\n");
00182   
00183   if(pNPPFuncs->destroystream)
00184     sprintf(szLog10, "    destroystream = %#08lx\r\n", pNPPFuncs->destroystream);
00185   else
00186     sprintf(szLog10, "    destroystream = not set!\r\n");
00187   
00188   if(pNPPFuncs->asfile)
00189     sprintf(szLog11, "    asfile        = %#08lx\r\n", pNPPFuncs->asfile);
00190   else
00191     sprintf(szLog11, "    asfile        = not set!\r\n");
00192   
00193   if(pNPPFuncs->writeready)
00194     sprintf(szLog12, "    writeready    = %#08lx\r\n", pNPPFuncs->writeready);
00195   else
00196     sprintf(szLog12, "    writeready    = not set!\r\n");
00197   
00198   if(pNPPFuncs->write)
00199     sprintf(szLog13, "    write         = %#08lx\r\n", pNPPFuncs->write);
00200   else
00201     sprintf(szLog13, "    write         = not set!\r\n");
00202   
00203   if(pNPPFuncs->print)
00204     sprintf(szLog14, "    print         = %#08lx\r\n", pNPPFuncs->print);
00205   else
00206     sprintf(szLog14, "    print         = not set!\r\n");
00207   
00208   if(pNPPFuncs->event)
00209     sprintf(szLog15, "    event         = %#08lx\r\n", pNPPFuncs->event);
00210   else
00211     sprintf(szLog15, "    event         = not set!\r\n");
00212   
00213   if(pNPPFuncs->urlnotify)
00214     sprintf(szLog16, "    urlnotify     = %#08lx\r\n", pNPPFuncs->urlnotify);
00215   else
00216     sprintf(szLog16, "    urlnotify     = not set!\r\n");
00217   
00218   if(pNPPFuncs->javaClass)
00219     sprintf(szLog17, "    javaClass     = %#08lx\r\n", pNPPFuncs->javaClass);
00220   else
00221     sprintf(szLog17, "    javaClass     = not set!\r\n");
00222   
00223   if(pNPPFuncs->getvalue)
00224     sprintf(szLog18, "    getvalue      = %#08lx\r\n", pNPPFuncs->getvalue);
00225   else
00226     sprintf(szLog18, "    getvalue      = not set!\r\n");
00227   
00228   if(pNPPFuncs->setvalue)
00229     sprintf(szLog19, "    setvalue      = %#08lx\r\n", pNPPFuncs->setvalue);
00230   else
00231     sprintf(szLog19, "    setvalue      = not set!\r\n");
00232 
00233   sprintf(szLog20, "\r\n");
00234 
00235   if(bToConsole)
00236   {
00237     printf("%s", szLog1); printf("%s", szLog2); printf("%s", szLog3); printf("%s", szLog4);
00238     printf("%s", szLog5); printf("%s", szLog6); printf("%s", szLog7); printf("%s", szLog8);
00239     printf("%s", szLog9); printf("%s", szLog10); printf("%s", szLog11); printf("%s", szLog12);
00240     printf("%s", szLog13); printf("%s", szLog14); printf("%s", szLog15); printf("%s", szLog16);
00241     printf("%s", szLog17); printf("%s", szLog18); printf("%s", szLog19); printf("%s", szLog20);
00242   }
00243   
00244   if(bToFile)
00245   {
00246     filer.write(szLog1); filer.write(szLog2); filer.write(szLog3); filer.write(szLog4);
00247     filer.write(szLog5); filer.write(szLog6); filer.write(szLog7); filer.write(szLog8);
00248     filer.write(szLog9); filer.write(szLog10); filer.write(szLog11); filer.write(szLog12);
00249     filer.write(szLog13); filer.write(szLog14); filer.write(szLog15); filer.write(szLog16);
00250     filer.write(szLog17); filer.write(szLog18); filer.write(szLog19); filer.write(szLog20);
00251   }
00252   
00253   if(bToWindow)
00254   {
00255     dumpStringToMainWindow(szLog1); dumpStringToMainWindow(szLog2);
00256     dumpStringToMainWindow(szLog3); dumpStringToMainWindow(szLog4);
00257     dumpStringToMainWindow(szLog5); dumpStringToMainWindow(szLog6);
00258     dumpStringToMainWindow(szLog7); dumpStringToMainWindow(szLog8);
00259     dumpStringToMainWindow(szLog9); dumpStringToMainWindow(szLog10);
00260     dumpStringToMainWindow(szLog11); dumpStringToMainWindow(szLog12);
00261     dumpStringToMainWindow(szLog13); dumpStringToMainWindow(szLog14);
00262     dumpStringToMainWindow(szLog15); dumpStringToMainWindow(szLog16);
00263     dumpStringToMainWindow(szLog17); dumpStringToMainWindow(szLog18);
00264     dumpStringToMainWindow(szLog19); dumpStringToMainWindow(szLog20);
00265   }
00266 }
00267 
00268 void Logger::logSPY_NP_Initialize()
00269 {
00270   char szLog[] = "NP_Initialize by NPSpy\r\n";
00271 
00272   if(bToConsole)
00273     printf("%s", szLog);
00274   
00275   if(bToFile)
00276     filer.write(szLog);
00277 
00278   if(bToWindow)
00279     dumpStringToMainWindow(szLog);
00280 }
00281 
00282 void Logger::logSPY_NP_Shutdown(char * mimetype)
00283 {
00284   char szLog[512] = "NP_Shutdown by NPSpy\r\n";
00285   if(mimetype)
00286   {
00287     strcat(szLog, " for \"");
00288     strcat(szLog, mimetype);
00289     strcat(szLog, "\"");
00290   }
00291 
00292   if(bToConsole)
00293     printf("%s", szLog);
00294   
00295   if(bToFile)
00296     filer.write(szLog);
00297 
00298   if(bToWindow)
00299     dumpStringToMainWindow(szLog);
00300 }
00301 
00302 void Logger::logCall(NPAPI_Action action, DWORD dw1, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6, DWORD dw7)
00303 {
00304   if(isMuted(action))
00305     return;
00306 
00307   static char szLog[512];
00308 
00309   LogItemStruct * lis = makeLogItemStruct(action, dw1, dw2, dw3, dw4, dw5, dw6, dw7);
00310   int iLength = formatLogItem(lis, szLog, TRUE);
00311   freeLogItemStruct(lis);
00312 
00313   if(bToConsole)
00314     printf("%s", szLog);
00315   
00316   if(bToFile)
00317     filer.write(szLog);
00318 
00319   if(bToWindow)
00320     dumpStringToMainWindow(szLog);
00321 }
00322 
00323 void Logger::logReturn(DWORD dwRet)
00324 {
00325 }
00326 
00327 void Logger::setOnTop(BOOL ontop)
00328 {
00329   bOnTop = ontop;
00330 }
00331 
00332 void Logger::setToFile(BOOL tofile, char * filename)
00333 {
00334   if(!filename || !*filename || (strlen(filename) > _MAX_PATH))
00335   {
00336     bToFile = FALSE;
00337     return;
00338   }
00339 
00340   //don't screw up the file on false call
00341   BOOL samefile = (stricmp(szFile, filename) == 0);
00342   BOOL sameaction = (bToFile == tofile);
00343   
00344   if(sameaction)
00345   {
00346     if(samefile)
00347       return;
00348 
00349     strcpy(szFile, filename);
00350 
00351     if(bToFile)
00352     {
00353       filer.close();
00354       filer.create(szFile, TRUE);
00355     }
00356   }
00357 
00358   if(!sameaction)
00359   {
00360     bToFile = tofile;
00361 
00362     if(!samefile)
00363       strcpy(szFile, filename);
00364 
00365     if(bToFile)
00366       filer.create(szFile, TRUE);
00367     else
00368       filer.close();
00369   }
00370 }
00371 
00372 BOOL Logger::isMuted(NPAPI_Action action)
00373 {
00374   if(bMutedAll)
00375     return TRUE;
00376 
00377   if(action >= TOTAL_NUMBER_OF_API_CALLS)
00378   {
00379     assert(0);
00380     return FALSE;
00381   }
00382 
00383   return bMutedCalls[action];
00384 }
00385 
00386 BOOL * Logger::getMutedCalls()
00387 {
00388   return &bMutedCalls[0];
00389 }
00390 
00391 void Logger::setMutedCalls(BOOL * mutedcalls)
00392 {
00393   for(int i = 0; i < sizeof(bMutedCalls)/sizeof(BOOL); i++)
00394     bMutedCalls[i] = mutedcalls[i];
00395 }