multilog.cpp
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:11k
源码类别:

Symbian

开发平台:

Visual C++

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