1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include <string>
39
40#include "xp.h"
41
42#include "logger.h"
43#include "profile.h"
44#include "plugload.h"
45
46Logger::Logger() :
47  bMutedAll(FALSE),
48  bOnTop(TRUE),
49  bToWindow(TRUE),
50  bToConsole(FALSE),
51  bToFile(FALSE),
52  bSPALID(FALSE)
53{
54  if(0 != GetPluginsDir(szFile, sizeof(szFile)))
55  {
56    strcat(szFile, DIR_SEPARATOR);
57    strcat(szFile, DEFAULT_LOG_FILE_NAME);
58  }
59  else
60    szFile[0] = '\0';
61
62  for(int i = 0; i < sizeof(bMutedCalls)/sizeof(BOOL); i++)
63    bMutedCalls[i] = FALSE;
64
65  bMutedCalls[action_npn_mem_alloc] = TRUE;
66  bMutedCalls[action_npn_mem_free]  = TRUE;
67  bMutedCalls[action_npn_mem_flush] = TRUE;
68}
69
70Logger::~Logger()
71{
72}
73
74BOOL Logger::init()
75{
76  if(bToFile)
77    filer.create(szFile, TRUE);
78
79  return TRUE;
80}
81
82void Logger::shut()
83{
84  filer.close();
85}
86
87#define MAX_OUTPUT_SIZE 8192
88
89void Logger::logNS_NP_GetEntryPoints()
90{
91  char szLog[] = "NP_GetEntryPoints by Netscape\r\n";
92
93  if(bToConsole)
94    printf("%s", szLog);
95
96  if(bToFile)
97    filer.write(szLog);
98
99  if(bToWindow)
100    dumpStringToMainWindow(szLog);
101}
102
103void Logger::logNS_NP_Initialize()
104{
105  char szLog[] = "NP_Initialize by Netscape\r\n";
106
107  if(bToConsole)
108    printf("%s", szLog);
109
110  if(bToFile)
111    filer.write(szLog);
112
113  if(bToWindow)
114    dumpStringToMainWindow(szLog);
115}
116
117void Logger::logNS_NP_Shutdown()
118{
119  char szLog[] = "NP_Shutdown by Netscape\r\n";
120
121  if(bToConsole)
122    printf("%s", szLog);
123
124  if(bToFile)
125    filer.write(szLog);
126
127  if(bToWindow)
128    dumpStringToMainWindow(szLog);
129}
130
131void Logger::logSPY_NP_GetEntryPoints(NPPluginFuncs * pNPPFuncs)
132{
133  char szLog[80] = "NP_GetEntryPoints by NPSpy\r\n";
134
135  if(bToConsole)
136    printf("%s", szLog);
137
138  if(bToFile)
139    filer.write(szLog);
140
141  if(bToWindow)
142    dumpStringToMainWindow(szLog);
143
144  if(!pNPPFuncs)
145    return;
146
147  char szLog1[80],szLog2[80],szLog3[80],szLog4[80],szLog5[80],szLog6[80],szLog7[80],
148       szLog8[80],szLog9[80],szLog10[80],szLog11[80],szLog12[80],szLog13[80],szLog14[80],
149       szLog15[80],szLog16[80],szLog17[80],szLog18[80],szLog19[80],szLog20[80];
150
151  sprintf(szLog1, "\r\n");
152  sprintf(szLog2, "     Plugin entry point table\r\n");
153  sprintf(szLog3, "     ========================\r\n");
154
155  if(pNPPFuncs->size)
156    sprintf(szLog4, "    size          = %i\r\n", pNPPFuncs->size);
157  else
158    sprintf(szLog4, "    size          = not set!\r\n");
159
160  if(pNPPFuncs->version)
161    sprintf(szLog5, "    version       = %i\r\n", pNPPFuncs->version);
162  else
163    sprintf(szLog5, "    version       = not set!\r\n");
164
165  if(pNPPFuncs->newp)
166    sprintf(szLog6, "    newp          = %#08lx\r\n", pNPPFuncs->newp);
167  else
168    sprintf(szLog6, "    newp          = not set!\r\n");
169
170  if(pNPPFuncs->destroy)
171    sprintf(szLog7, "    destroy       = %#08lx\r\n", pNPPFuncs->destroy);
172  else
173    sprintf(szLog7, "    destroy       = not set!\r\n");
174
175  if(pNPPFuncs->setwindow)
176    sprintf(szLog8, "    setwindow     = %#08lx\r\n", pNPPFuncs->setwindow);
177  else
178    sprintf(szLog8, "    setwindow     = not set!\r\n");
179
180  if(pNPPFuncs->newstream)
181    sprintf(szLog9, "    newstream     = %#08lx\r\n", pNPPFuncs->newstream);
182  else
183    sprintf(szLog9, "    newstream     = not set!\r\n");
184
185  if(pNPPFuncs->destroystream)
186    sprintf(szLog10, "    destroystream = %#08lx\r\n", pNPPFuncs->destroystream);
187  else
188    sprintf(szLog10, "    destroystream = not set!\r\n");
189
190  if(pNPPFuncs->asfile)
191    sprintf(szLog11, "    asfile        = %#08lx\r\n", pNPPFuncs->asfile);
192  else
193    sprintf(szLog11, "    asfile        = not set!\r\n");
194
195  if(pNPPFuncs->writeready)
196    sprintf(szLog12, "    writeready    = %#08lx\r\n", pNPPFuncs->writeready);
197  else
198    sprintf(szLog12, "    writeready    = not set!\r\n");
199
200  if(pNPPFuncs->write)
201    sprintf(szLog13, "    write         = %#08lx\r\n", pNPPFuncs->write);
202  else
203    sprintf(szLog13, "    write         = not set!\r\n");
204
205  if(pNPPFuncs->print)
206    sprintf(szLog14, "    print         = %#08lx\r\n", pNPPFuncs->print);
207  else
208    sprintf(szLog14, "    print         = not set!\r\n");
209
210  if(pNPPFuncs->event)
211    sprintf(szLog15, "    event         = %#08lx\r\n", pNPPFuncs->event);
212  else
213    sprintf(szLog15, "    event         = not set!\r\n");
214
215  if(pNPPFuncs->urlnotify)
216    sprintf(szLog16, "    urlnotify     = %#08lx\r\n", pNPPFuncs->urlnotify);
217  else
218    sprintf(szLog16, "    urlnotify     = not set!\r\n");
219
220  if(pNPPFuncs->javaClass)
221    sprintf(szLog17, "    javaClass     = %#08lx\r\n", pNPPFuncs->javaClass);
222  else
223    sprintf(szLog17, "    javaClass     = not set!\r\n");
224
225  if(pNPPFuncs->getvalue)
226    sprintf(szLog18, "    getvalue      = %#08lx\r\n", pNPPFuncs->getvalue);
227  else
228    sprintf(szLog18, "    getvalue      = not set!\r\n");
229
230  if(pNPPFuncs->setvalue)
231    sprintf(szLog19, "    setvalue      = %#08lx\r\n", pNPPFuncs->setvalue);
232  else
233    sprintf(szLog19, "    setvalue      = not set!\r\n");
234
235  sprintf(szLog20, "\r\n");
236
237  if(bToConsole)
238  {
239    printf("%s", szLog1); printf("%s", szLog2); printf("%s", szLog3); printf("%s", szLog4);
240    printf("%s", szLog5); printf("%s", szLog6); printf("%s", szLog7); printf("%s", szLog8);
241    printf("%s", szLog9); printf("%s", szLog10); printf("%s", szLog11); printf("%s", szLog12);
242    printf("%s", szLog13); printf("%s", szLog14); printf("%s", szLog15); printf("%s", szLog16);
243    printf("%s", szLog17); printf("%s", szLog18); printf("%s", szLog19); printf("%s", szLog20);
244  }
245
246  if(bToFile)
247  {
248    filer.write(szLog1); filer.write(szLog2); filer.write(szLog3); filer.write(szLog4);
249    filer.write(szLog5); filer.write(szLog6); filer.write(szLog7); filer.write(szLog8);
250    filer.write(szLog9); filer.write(szLog10); filer.write(szLog11); filer.write(szLog12);
251    filer.write(szLog13); filer.write(szLog14); filer.write(szLog15); filer.write(szLog16);
252    filer.write(szLog17); filer.write(szLog18); filer.write(szLog19); filer.write(szLog20);
253  }
254
255  if(bToWindow)
256  {
257    dumpStringToMainWindow(szLog1); dumpStringToMainWindow(szLog2);
258    dumpStringToMainWindow(szLog3); dumpStringToMainWindow(szLog4);
259    dumpStringToMainWindow(szLog5); dumpStringToMainWindow(szLog6);
260    dumpStringToMainWindow(szLog7); dumpStringToMainWindow(szLog8);
261    dumpStringToMainWindow(szLog9); dumpStringToMainWindow(szLog10);
262    dumpStringToMainWindow(szLog11); dumpStringToMainWindow(szLog12);
263    dumpStringToMainWindow(szLog13); dumpStringToMainWindow(szLog14);
264    dumpStringToMainWindow(szLog15); dumpStringToMainWindow(szLog16);
265    dumpStringToMainWindow(szLog17); dumpStringToMainWindow(szLog18);
266    dumpStringToMainWindow(szLog19); dumpStringToMainWindow(szLog20);
267  }
268}
269
270void Logger::logSPY_NP_Initialize()
271{
272  char szLog[] = "NP_Initialize by NPSpy\r\n";
273
274  if(bToConsole)
275    printf("%s", szLog);
276
277  if(bToFile)
278    filer.write(szLog);
279
280  if(bToWindow)
281    dumpStringToMainWindow(szLog);
282}
283
284void Logger::logSPY_NP_Shutdown(char * mimetype)
285{
286  char szLog[512] = "NP_Shutdown by NPSpy\r\n";
287  if(mimetype)
288  {
289    strcat(szLog, " for \"");
290    strcat(szLog, mimetype);
291    strcat(szLog, "\"\r\n");
292  }
293
294  if(bToConsole)
295    printf("%s", szLog);
296
297  if(bToFile)
298    filer.write(szLog);
299
300  if(bToWindow)
301    dumpStringToMainWindow(szLog);
302}
303
304void Logger::logCall(NPAPI_Action action, DWORD dw1, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6, DWORD dw7)
305{
306  if(isMuted(action))
307    return;
308
309  std::string log;
310
311  LogItemStruct * lis = makeLogItemStruct(action, dw1, dw2, dw3, dw4, dw5, dw6, dw7);
312  formatLogItem(lis, &log, TRUE);
313  freeLogItemStruct(lis);
314
315  if(bToConsole)
316    printf("%s", log.c_str());
317
318  if(bToFile)
319    filer.write(log);
320
321  if(bToWindow)
322    dumpStringToMainWindow(log);
323}
324
325void Logger::logMessage(const char *msg)
326{
327  if(bToConsole)
328    printf("%s", msg);
329
330  if(bToFile)
331    filer.write((char *)msg);
332
333  if(bToWindow)
334    dumpStringToMainWindow((char *)msg);
335}
336
337void Logger::logReturn(NPAPI_Action action, DWORD dwRet)
338{
339    if (isMuted(action))
340        return;
341
342    char msg[512];
343    sprintf(msg, "---Return: %d\r\n", dwRet);
344    logMessage(msg);
345}
346
347void Logger::setOnTop(BOOL ontop)
348{
349  bOnTop = ontop;
350}
351
352void Logger::setToFile(BOOL tofile, char * filename)
353{
354  if(!filename || !*filename || (strlen(filename) > _MAX_PATH))
355  {
356    bToFile = FALSE;
357    return;
358  }
359
360  //don't screw up the file on false call
361  BOOL samefile = (_stricmp(szFile, filename) == 0);
362  BOOL sameaction = (bToFile == tofile);
363
364  if(sameaction)
365  {
366    if(samefile)
367      return;
368
369    strcpy(szFile, filename);
370
371    if(bToFile)
372    {
373      filer.close();
374      filer.create(szFile, TRUE);
375    }
376  }
377
378  if(!sameaction)
379  {
380    bToFile = tofile;
381
382    if(!samefile)
383      strcpy(szFile, filename);
384
385    if(bToFile)
386      filer.create(szFile, TRUE);
387    else
388      filer.close();
389  }
390}
391
392BOOL Logger::isMuted(NPAPI_Action action)
393{
394  if(bMutedAll)
395    return TRUE;
396
397  if(action >= TOTAL_NUMBER_OF_API_CALLS)
398  {
399    assert(0);
400    return FALSE;
401  }
402
403  return bMutedCalls[action];
404}
405
406BOOL * Logger::getMutedCalls()
407{
408  return &bMutedCalls[0];
409}
410
411void Logger::setMutedCalls(BOOL * mutedcalls)
412{
413  for(int i = 0; i < sizeof(bMutedCalls)/sizeof(BOOL); i++)
414    bMutedCalls[i] = mutedcalls[i];
415}
416