multilog.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:10k
源码类别:

Symbian

开发平台:

C/C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: RCSL 1.0/RPSL 1.0
  3.  *
  4.  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
  5.  *
  6.  * The contents of this file, and the files included with this file, are
  7.  * subject to the current version of the RealNetworks Public Source License
  8.  * Version 1.0 (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the RealNetworks Community Source License Version 1.0
  11.  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
  12.  * in which case the RCSL will apply. You may also obtain the license terms
  13.  * directly from RealNetworks.  You may not use this file except in
  14.  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
  15.  * applicable to this file, the RCSL.  Please see the applicable RPSL or
  16.  * RCSL for the rights, obligations and limitations governing use of the
  17.  * contents of the file.
  18.  *
  19.  * This file is part of the Helix DNA Technology. RealNetworks is the
  20.  * developer of the Original Code and owns the copyrights in the portions
  21.  * it created.
  22.  *
  23.  * This file, and the files included with this file, is distributed and made
  24.  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  25.  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  26.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
  27.  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  28.  *
  29.  * Technology Compatibility Kit Test Suite(s) Location:
  30.  *    http://www.helixcommunity.org/content/tck
  31.  *
  32.  * Contributor(s):
  33.  *
  34.  * ***** END LICENSE BLOCK ***** */
  35. // system
  36. #include "hlxclib/stdio.h"
  37. #include "hlxclib/stdlib.h"
  38. #include "hlxclib/stdarg.h"
  39. #include "hlxclib/string.h"
  40. // include
  41. #include "hxtypes.h"
  42. #include "hxcom.h"
  43. #include "hxerror.h"
  44. #include "hxprefs.h"
  45. #include "ihxpckts.h"
  46. #include "hxstrutl.h"
  47. #include "hxprefutil.h"
  48. // pndebug
  49. #include "debugout.h"
  50. #include "hxassert.h"
  51. #include "multilog.h"
  52. #ifdef _OPENWAVE
  53. #include <op_fs.h>
  54. #endif /* _OPENWAVE */
  55. #define MULTILOG_BUFFER_SIZE  1024
  56. void MultiLog(BOOL               bOutputToFile,
  57.               BOOL               bOutputToDebugger,
  58.               BOOL               bOutputToCoreDebug,
  59.               const char*        pszFileName,
  60.               IHXErrorMessages* pErrorMessages,
  61.               UINT32             ulCoreDebugUserCode,
  62.               const char*        pszRegKey,
  63.               const char*        pszMsg,
  64.               va_list            varargs)
  65. {
  66.     if (bOutputToFile || bOutputToDebugger || bOutputToCoreDebug)
  67.     {
  68.         // Check if the string contains variable arguments.
  69.         // We also have to allow for the literal '%' percent
  70.         // character which would be "%%".
  71.         //
  72.         // Check for any '%' characters
  73.         char* pPct = (char*)strchr(pszMsg, '%');
  74.         // Make sure that this isn't a "%%".
  75.         while(pPct && *(pPct + 1) == '%')
  76.         {
  77.             pPct = (char*)strchr((pPct + 2), '%');
  78.         }
  79.         // Create the log message
  80.         char szDbgStr[MULTILOG_BUFFER_SIZE]; /* Flawfinder: ignore */
  81.         if(pPct)
  82.         {
  83.             vsnprintf(szDbgStr, sizeof(szDbgStr), pszMsg, varargs);
  84.         }
  85.         else
  86.         {
  87.             // There are no substitutions to be done,
  88.             // so we can just strcpy the string
  89.             SafeStrCpy(szDbgStr, pszMsg, MULTILOG_BUFFER_SIZE);
  90.         }
  91.         // If a file one of the outputs?
  92.         if (bOutputToFile)
  93.         {
  94.             // Do we have a filename?
  95.             if (pszFileName)
  96.             {
  97.                 // Open the file
  98. #ifdef _OPENWAVE
  99.                 OpFsFd fd = OpFsOpen(pszFileName,
  100.                                      kOpFsFlagWrOnly | kOpFsFlagCreate,
  101.                                      0);
  102.                 if (fd != kOpFsErrAny)
  103.                 {
  104.                     OpFsSeek(fd, 0, kOpFsSeekEnd);
  105.                     OpFsWrite(fd, szDbgStr, strlen(szDbgStr));
  106.                     OpFsClose(fd);
  107.                 }
  108. #else
  109.                 FILE* fp = fopen(pszFileName, "a+");
  110.                 if (fp)
  111.                 {
  112.                     fprintf(fp, "%s", szDbgStr);
  113.                     fflush(fp);
  114.                     fclose(fp);
  115.                 }
  116. #endif /* _OPENWAVE */
  117.             }
  118.         }
  119.         // Is debugger window one of the outputs?
  120.         if (bOutputToDebugger)
  121.         {
  122.             HXOutputDebugString(szDbgStr);
  123.         }
  124.         // Is the Core Debug window one of the targets?
  125.         if (bOutputToCoreDebug)
  126.         {
  127.             // Do we have an IHXErrorMessages pointer?
  128.             if (pErrorMessages)
  129.             {
  130.                 // Initially we say that we will output to core debug
  131.                 BOOL bDoOutput = TRUE;
  132.                 // Do we have a regkey string?
  133.                 if (pszRegKey)
  134.                 {
  135.                     // By putting a regKey string, the user is saying
  136.                     // that they only want to see this be output to
  137.                     // core debug if the regKey is set to "1". So now
  138.                     // the default is that we will NOT output.
  139.                     bDoOutput = FALSE;
  140.                     // QI IHXErrorMessages for IHXPreferences
  141.                     IHXPreferences* pPref = NULL;
  142.                     pErrorMessages->QueryInterface(IID_IHXPreferences, (void**) &pPref);
  143.                     if (pPref)
  144.                     {
  145. ReadPrefBOOL(pPref, pszRegKey, bDoOutput);
  146.     }
  147.                     HX_RELEASE(pPref);
  148.                 }
  149.                 // Now, after potentially checking the regKey,
  150.                 // are we *still* supposed to output?
  151.                 if (bDoOutput)
  152.                 {
  153.                     // The core debug window prints out a funky
  154.                     // character when it sees 'n'. However, it
  155.                     // seems to be fine when it sees "rn". So
  156.                     // here we go through and replace any lone
  157.                     // 'n' with "rn", unless 'n' is the last
  158.                     // character in the string.
  159.                     char* pLF = (char*)strchr(szDbgStr, 'n');
  160.                     if (pLF)
  161.                     {
  162.                         // We have at least one 'n' character. Is
  163.                         // the first 'n' character the last one
  164.                         // in the string?
  165.                         if (*(pLF + 1) == '')
  166.                         {
  167.                             // We have a 'n' character, but there's only
  168.                             // one and it's the last character in the string.
  169.                             // Therefore, we can just NULL it out
  170.                             *pLF = '';
  171.                         }
  172.                         else
  173.                         {
  174.                             // No, we have a 'n' and it's not at the
  175.                             // end of the string. So we must go through
  176.                             // the string and replace 'n' with "rn".
  177.                             char szTmpStr[MULTILOG_BUFFER_SIZE];
  178.                             char* pSrc = &szDbgStr[0];
  179.                             char* pDst = &szTmpStr[0];
  180.                             while ((pSrc - szDbgStr) < MULTILOG_BUFFER_SIZE && *pSrc && (pDst - szTmpStr) < MULTILOG_BUFFER_SIZE)
  181.                             {
  182.                                 if (*pSrc == 'n')
  183.                                 {
  184.                                     // Is this the last character in
  185.                                     // the string? If so, we don't copy it
  186.                                     if (*(pSrc + 1) != '')
  187.                                     {
  188.                                         // Is the character before it
  189.                                         // already a 'r"? If so, then
  190.                                         // we don't need to do the replacement
  191.                                         if (*(pSrc - 1) != 'r')
  192.                                         {
  193.                                             // Replace the 'n' with a "rn"
  194.                                             *pDst++ = 'r';
  195.                                             if ((pDst - szTmpStr) < MULTILOG_BUFFER_SIZE) *pDst++ = *pSrc++;
  196.                                         }
  197.                                         else
  198.                                         {
  199.                                             *pDst++ = *pSrc++;
  200.                                         }
  201.                                     }
  202.                                     else
  203.                                     {
  204.                                         // Advance the source
  205.                                         pSrc++;
  206.                                     }
  207.                                 }
  208.                                 else
  209.                                 {
  210.                                     // This character is not the 'n',
  211.                                     // so just copy it.
  212.                                     *pDst++ = *pSrc++;
  213.                                 }
  214.                             }
  215.                             // Copy NULL at end
  216.                             if ((pDst - szTmpStr) < MULTILOG_BUFFER_SIZE) *pDst++ = '';
  217.                             // Now copy the temp string over the old one
  218.                             SafeStrCpy(szDbgStr, szTmpStr, MULTILOG_BUFFER_SIZE);
  219.                         }
  220.                     }
  221.                     // Now we can finally call IHXErrorMessages::Report
  222.                     pErrorMessages->Report(HXLOG_DEBUG,         // severity
  223.                                            HXR_OK,              // RMA Code
  224.                                            ulCoreDebugUserCode, // user code
  225.                                            szDbgStr,            // string
  226.                                            NULL);               // More Info URL
  227.                 }
  228.             }
  229.         }
  230.     }
  231. }