📄 tracer.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================//// Author: Sushma Fernandes (sushma_fernandes@hp.com)//// Modified By: Jenny Yu (jenny_yu@hp.com)// Carol Ann Krug Graves, Hewlett-Packard Company// (carolann_graves@hp.com)// Amit K Arora, IBM (amita@in.ibm.com) for PEP#101// David Dillard, VERITAS Software Corp.// (david.dillard@veritas.com)////%/////////////////////////////////////////////////////////////////////////////#include <fstream>#include <cstring>#include <Pegasus/Common/System.h>#include <Pegasus/Common/Tracer.h>#include <Pegasus/Common/AutoPtr.h>PEGASUS_USING_STD;PEGASUS_USING_PEGASUS;// If Windows platform then set the EOF_CHAR to 2#if defined(PEGASUS_OS_TYPE_WINDOWS) #define EOF_CHAR 2#else #define EOF_CHAR 1#endif// Trace files for test purposes// Will be created in the $(PEGASUS_TMP) directory, or if not set,// in the current directoryCString FILE1;CString FILE2;CString FILE3;CString FILE4;// // Reads the last trace message from a given trace file and compares the // given string with the string read from file//// return 0 if the strings match// return 1 if the strings do not match//Uint32 compare(const char* fileName, const char* compareStr){ Uint32 count=0; Uint32 retCode=0; fstream file; Uint32 size= static_cast<Uint32>(strlen(compareStr)); AutoArrayPtr<char> readStr(new char[size+EOF_CHAR+1]); file.open(fileName,fstream::in); if (!file.good()) { return 1; } file.seekg((Sint32) -(static_cast<Sint32>(size)+EOF_CHAR),fstream::end); memset(readStr.get(), 0, (size+EOF_CHAR+1)*sizeof(char)); file.read(readStr.get(),size+EOF_CHAR); (readStr.get())[size]='\0'; retCode=strcmp(compareStr,readStr.get()); /* Diagnostic to determnine string differences if (!retCode) cout << "Compare Error: compareStr= \n\"" << compareStr << "\". readStr= \n\"" << readStr.get() << "\"" << endl; */ file.close(); return retCode;}//// Description:// Trace properties file, level and component are not set// Should not log a trace message//// Type:// Negative //// return 0 if the test passed// return 1 if the test failed//Uint32 test1(){ const char* METHOD_NAME = "test1"; PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s %d", "This message should not appear value=",123); PEG_METHOD_EXIT(); return(compare(FILE1,""));}//// Description:// Trace properties level and component are not set// Should not log a trace message//// Type:// Negative //// return 0 if the test passed// return 1 if the test failed//Uint32 test2(){ const char* METHOD_NAME = "test2"; Tracer::setTraceFile(FILE1); PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s %d", "This message should not appear value=",123); return(compare(FILE1,"This message should not appear value=123"));}//// Description:// Trace properties component is not set// Should not log a trace message//// Type:// Negative //// return 0 if the test passed// return 1 if the test failed//Uint32 test3(){ const char* METHOD_NAME = "test3"; Tracer::setTraceLevel(Tracer::LEVEL1); PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s", "This message should not appear"); return(compare(FILE1,"This message should not appear"));}//// Description:// Trace properties file, level and component are set// should log a trace message//// Type:// Positive //// return 0 if the test passed// return 1 if the test failed//Uint32 test4(){ const char* METHOD_NAME = "test4"; Tracer::setTraceComponents("Config"); PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); return(compare(FILE1,"Entering method test4"));}//// Description:// Trace component is set to an invalid component// should not log a trace message//// Type:// Negative //// return 0 if the test passed// return 1 if the test failed//Uint32 test5(){ const char* METHOD_NAME = "test5"; Tracer::setTraceComponents("Wrong Component Name"); PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); PEG_METHOD_EXIT(); return(compare(FILE1,"Entering method test4"));}//// Description:// Trace level is set to LEVEL 2 and logs a LEVEL 4 message // should not log a trace message//// Type:// Negative //// return 0 if the test passed// return 1 if the test failed//Uint32 test6(){ const char* METHOD_NAME = "test6"; Tracer::setTraceComponents("Config"); Tracer::setTraceLevel(Tracer::LEVEL2); Tracer::trace(TRC_CONFIG,Tracer::LEVEL2,"%s %s", "Test Message for Level2 in",METHOD_NAME); Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s %s", "Test Message for Level2 in",METHOD_NAME); Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL4,"%s", "This Message should not appear"); return(compare(FILE1,"Test Message for Level2 in test6"));}//// Description:// Trace level is set to an invalid level// should not log a trace message//// Type:// Negative //// return 0 if the test passed// return 1 if the test failed//Uint32 test7(){ const char* METHOD_NAME = "test7"; Tracer::setTraceLevel(100); PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); PEG_METHOD_EXIT(); return(compare(FILE1,"Test Message for Level2 in test6"));}//// Description:// Trace level is set to LEVEL1 for a non entry/exit message// should not log a trace message, should log an error//// Type:// Negative //// return 0 if the test passed// return 1 if the test failed//Uint32 test8(){ const char* METHOD_NAME = "test8"; Tracer::setTraceLevel(Tracer::LEVEL1); Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL1,"%s", "Test Message for Level4"); return(compare(FILE1,"Test Message for Level2 in test6"));}//// Description:// Changes the trace file to FILE2 //// Type:// Positive //// return 0 if the test passed// return 1 if the test failed//Uint32 test9(){ const char* METHOD_NAME = "test9"; Tracer::setTraceLevel(Tracer::LEVEL3); Tracer::setTraceFile(FILE2); PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); Tracer::trace(TRC_CONFIG,Tracer::LEVEL3,"%s %s", "Test Message for Level3 in",METHOD_NAME); return(compare(FILE2,"Test Message for Level3 in test9"));}//// Description:// Passes invalid component in the trace call// should not log a trace message//// Type:// Negative //// return 0 if the test passed// return 1 if the test failed//// This test not required with change t0// use and test macros only.Uint32 test10(){ const char* METHOD_NAME = "test10"; Tracer::setTraceComponents("ALL"); PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); PEG_METHOD_EXIT(); return(compare(FILE2,"Test Message for Level3 in test9"));}//// Description:// Implements trace call for Tracer::Level1// should log a trace message//// Type:// Positive //// return 0 if the test passed// return 1 if the test failed//Uint32 test11(){ const char* METHOD_NAME = "test11"; Tracer::setTraceComponents("ALL"); Tracer::setTraceLevel(Tracer::LEVEL4); PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); return(compare(FILE2,"Entering method test11"));}//// Description:// Implements trace call for Tracer::Level1// should log a trace message//// Type:// Positive //// return 0 if the test passed// return 1 if the test failed//Uint32 test12(){ const char* METHOD_NAME = "test12"; Tracer::setTraceComponents("ALL"); Tracer::setTraceLevel(Tracer::LEVEL4); PEG_METHOD_ENTER(TRC_CONFIG,METHOD_NAME); PEG_METHOD_EXIT(); return(compare(FILE2,"Exiting method test12"));}//// Description:// Implements trace call for Tracer::Level2// should log a trace message//// Type:// Positive //// return 0 if the test passed// return 1 if the test failed//Uint32 test13(){ const char* METHOD_NAME = "test13"; Tracer::setTraceComponents("ALL"); Tracer::setTraceLevel(Tracer::LEVEL4); Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s %s", "Test Message for Level2 in",METHOD_NAME); return(compare(FILE2,"Test Message for Level2 in test13"));}//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -