使用RollingFileAppender时是否需要显式释放内存?

时间:2013-12-10 07:37:41

标签: c++ logging log4cplus

我正在实现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-threadinglog4clpus为我做这件事?

1 个答案:

答案 0 :(得分:2)

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