1

我正在实现用于存储日志消息的示例log4cplus代码RollingFileAppender。我的问题是我是否需要通过调用析构函数来显式释放以下行分配的内存:

SharedAppenderPtr myAppender1(new RollingFileAppender(LOG4CPLUS_TEXT("myTTCCLayoutLogFile.log"),
                                                               3 * 1024 * 1024,    // Max backup size
                                                               3));                // Max index

整个代码是:

#include "samplefileloggerheader.h"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Entry point for the program
//
int main()
{
    //
    // Local variables definition
    //
    int                     initLoop    = 0;    // Initialization variable for
                                                // the loop
    int                     LOOP_COUNT  = 100000;
    std::auto_ptr<Layout>   myLayout1;

    //
    // Function logic begins here
    //
    for (;;)
    {
        log4cplus::initialize();

        //
        // LogLog class is used to output log statements from within the
        // log4cplus package.
        // setInternalDebugging() Allows to enable/disable log4cplus
        // internal logging
        //
        helpers::LogLog::getLogLog()->setInternalDebugging( true );

        //
        // Instantiating a file appender that will append events in the
        // TTCCLayout() layout
        //
        SharedAppenderPtr myAppender1(new RollingFileAppender(LOG4CPLUS_TEXT("myTTCCLayoutLogFile.log"),
                                                               3 * 1024 * 1024,    // Max backup size
                                                               3));                // Max index
        myAppender1->setName(LOG4CPLUS_TEXT("sampleAppender1"));

        //
        // Instantiating a layout object
        //
        myLayout1 = std::auto_ptr<Layout>(new log4cplus::TTCCLayout());

        //
        // Attaching the layout object with the appender object
        //
        myAppender1->setLayout(myLayout1);

        //
        // Getting the root logger
        //
        Logger::getRoot().addAppender(myAppender1);

        //
        // Creating instances of logger
        //
        Logger  root        = Logger::getRoot();
        Logger  test        = Logger::getInstance(LOG4CPLUS_TEXT("test"));
        Logger  subTest     = Logger::getInstance(LOG4CPLUS_TEXT("test.subTest"));

        //
        // Writing the logs to the file
        //
        for (initLoop; initLoop < LOOP_COUNT; ++initLoop)
        {
            NDCContextCreator _context(LOG4CPLUS_TEXT("for()"));
            LOG4CPLUS_DEBUG(subTest, "Loop count index is : " << initLoop);
        } // for

        //
        // Final break statement
        //
        break;
    } // for

   return 0;
} // main()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

还有一个问题,我需要照顾multi-threading还是log4clpus为我做?

4

1 回答 1

2

我不知道log4clplus库,但变量类型 ( SharedAppenderPtr) 的名称表明它shared_ptr应该负责为您释放资源。

于 2013-12-10T07:47:52.437 回答