📄 _pidbutl.cpp
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/DB/_PIDBUtl.cpp,v $
* $Date: 2003/05/13 18:41:56 $
*
Description:
\*____________________________________________________________________________*/
//$SourceTop:$
#include <assert.h>
#include <stdio.h>
#include "_PIDBUtl.h"
#include "_PIDB.h"
#include "PIStrStr.h"
#include "PIConfDB.h"
#define D { cerr << __FILE__ << ": " << __LINE__ << endl; };
/*____________________________________________________________________________*\
*
Description:
Static data
\*____________________________________________________________________________*/
static int iLogSink; /* static data is NULL initialized, default==DB */
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
_PIDB *_PIDBUtil::GetRoot( _PIDB *pDB )
{
if ( !pDB )
{ return 0; };
_PIDB *pTmp = pDB;
while( pTmp->GetParent() )
{
pTmp = pTmp->GetParent();
};
return pTmp;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
_PIDB *_PIDBUtil::GetStdTree( _PIDB *pDB, const char *pKey )
{
_PIDB *pRoot = GetRoot( pDB );
if ( !pRoot )
{ return 0; };
return (_PIDB *)pRoot->Lookup( PIDBTYPE_TREE, pKey, 0 );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int _PIDBUtil::AddLogMessage( _PIDB *pDB, PIConfigDB *pConfig, int iType,
const char *pFormat, va_list ap )
{
assert( pDB && pFormat );
if ( !pDB || !pFormat )
{ return PIAPI_EINVAL; };
PIOStrStream os;
const char *pInfo = 0;
if ( pConfig )
{
pInfo = (const char *)pConfig->Lookup(PIDBTYPE_STRING,
PIDBKEY_NOTE, 0 );
};
/* ---
Allocate a buffer for the message and write it in.
NOTE even though this buffer is large, there is a real potential
for an overwrite to occur. At least we can check that the format
string does not exceed the length of the buffer.
--- */
enum { BUF_SIZE=4095 };
char szBuf[BUF_SIZE+1];
const char *pMsg = ap ? 0 : pFormat;
if ( !pMsg )
{
if ( strlen( pFormat )<=BUF_SIZE )
{ vsprintf( szBuf, pFormat, ap ); }
else
{ return PIAPI_ERROR; };
pMsg = szBuf;
};
/* --- write out the message --- */
if ( pInfo )
{
os << pMsg << endl << "In object starting at " << pInfo;
}
else
{
os << pMsg;
};
os << ends;
switch( iLogSink )
{
case PILOG_STDOUT:
cout << os.str() << endl;
return PIAPI_COMPLETED;
case PILOG_STDERR:
cerr << os.str() << endl;
return PIAPI_COMPLETED;
case PILOG_DB:
default:;
/* --- continue --- */
};
/* --- find the diagnostics tree --- */
_PIDB *pRoot = pDB->GetRoot();
_PIDB *pDiag = (_PIDB *)pRoot->Lookup( PIDBTYPE_TREE, PIDBKEY_DIAGNOSTICS,
0 );
if ( !pDiag )
{
pDiag = PI_NEW( PIDBTree( pRoot, PIDBKEY_DIAGNOSTICS ) );
};
assert( pDiag );
if ( !pDiag )
{ return PIAPI_EXHAUSTED; };
pDiag->Add(
PIDBTYPE_STRING,
iType == PILOG_ERROR ? PIDBKEY_ERROR : PIDBKEY_WARNING,
(void *)os.str(), 0 );
return PIAPI_COMPLETED;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int _PIDBUtil::WriteDiagnostics( _PIDB *pDB, int iFlags, ostream &os )
{
/* --- find the diagnostics tree --- */
assert( pDB );
_PIDB *pRoot = pDB->GetRoot();
assert( pRoot );
_PIDB *pTree = (_PIDB *)pRoot->Lookup( PIDBTYPE_TREE, PIDBKEY_DIAGNOSTICS,
0 );
if ( !pTree )
{ return PIAPI_ERROR; };
/* --- iterate over the tree printing out messages --- */
assert( pTree );
_PIDBIterator *pIter = 0;
if ( iFlags & PILOG_WARNING )
{ pIter = pTree->GetIterator( PIDBTYPE_STRING, PIDBKEY_WARNING, 0 ); };
if ( pIter )
{
if ( pIter->AtValidElement() )
{
os << "Warnings:-" << endl;
for( ; pIter->AtValidElement(); pIter->Next() )
{
const char *pS = (const char *)pIter->Current( 0 );
if ( pS )
{ os << pS << endl; };
};
};
};
PI_DELETE( pIter );
pIter = 0;
if ( iFlags & PILOG_ERROR )
{ pIter = pTree->GetIterator( PIDBTYPE_STRING, PIDBKEY_ERROR, 0 ); };
if ( pIter )
{
if ( pIter->AtValidElement() )
{
os << "Errors:-" << endl;
for( ; pIter->AtValidElement(); pIter->Next() )
{
const char *pS = (const char *)pIter->Current( 0 );
if ( pS )
{ os << pS << endl; };
};
};
};
PI_DELETE( pIter );
pIter = 0;
return PIAPI_COMPLETED;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int _PIDBUtil::NewSink( int iType, const char *pParam )
{
assert( iType>=PILOG_DB );
assert( iType<=PILOG_STDERR );
(void)pParam; /* not implemented */
if ( iType==PILOG_FILE )
{ return PIAPI_NOTSUPPORTED; };
iLogSink = iType;
return PIAPI_COMPLETED;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -