Show page source of Sample_testFixedContextCategory.cpp #80675

[[PageNavi(NavigationList)]]

==== サンプル6 testFixedContextCategory.cpp ====
 [http://log4cpp-jp.sourceforge.jp/api/classlog4cpp_1_1FixedContextCategory.html log4cpp::FixedContextCategory]を使ったサンプルです。
このクラスはlog4cpp::Categoryのサブクラスな訳ですが、何が違うかと言うと、NDC(ネスト化診断コンテキスト)の部分が固定文字列に置き換えられるということです。

===== 関連ファイル =====
 * testFixedContextCategory.cpp

===== ファイルの中身 =====
testFixedContextCategory.cpp[[BR]]
{{{ code cpp
#include <stdio.h>
#include <iostream>
#include "log4cpp/FixedContextCategory.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/NDC.hh"

int main(int argc, char** argv) {    
    log4cpp::Appender* appender = 
        new log4cpp::OstreamAppender("default", &std::cout);

    log4cpp::Layout* layout = new log4cpp::BasicLayout();
    appender->setLayout(layout);

    log4cpp::Category& root = log4cpp::Category::getRoot();
    root.addAppender(appender);
       root.setPriority(log4cpp::Priority::ERROR);
    
    log4cpp::FixedContextCategory sub1(std::string("sub1"), std::string("context1"));

    log4cpp::FixedContextCategory sub1_2(std::string("sub1"), std::string("context1_2"));

    log4cpp::FixedContextCategory sub2(std::string("sub1.sub2"), std::string("context2"));

    std::cout << " root priority = " << root.getPriority() << std::endl;
    std::cout << " sub1 priority = " << sub1.getPriority() << std::endl;
    std::cout << " sub2 priority = " << sub2.getPriority() << std::endl;
    
    root.error("root error");
    root.warn("root warn");
    sub1.error("sub1 error");
    sub1.warn("sub1 warn");
    sub1_2.error("sub1 error");
    sub1_2.warn("sub1 warn");
    sub2.error("sub2 error");
    sub2.warn("sub2 warn");
    
    log4cpp::Category::getInstance(std::string("sub1")).
               setPriority(log4cpp::Priority::INFO);

    std::cout << " root priority = " << root.getPriority() << std::endl;
    std::cout << " sub1 priority = " << sub1.getPriority() << std::endl;
    std::cout << " sub2 priority = " << sub2.getPriority() << std::endl;
   
    std::cout << "priority info" << std::endl;
    root.error("root error");
    root.warn("root warn");
    sub1.error("sub1 error");
    sub1.warn("sub1 warn");
    sub2.error("sub2 error");
    sub2.warn("sub2 warn");
    sub2.error("%s %s %d", "test", "vform", 123);
    sub2.warnStream() << "streamed warn";

    sub2 << log4cpp::Priority::WARN << "warn2.." << "..warn3..value=" << 0 
         << log4cpp::eol << "..warn4";

    log4cpp::Category::shutdown();

    return 0;
}
}}}

===== 実行結果例 =====
 testFixedContextCategory.cppをビルドし、実行した結果です。実行環境はLinux(Ubuntu 11.0)です。 

stdout(標準出力):[[BR]]
{{{
 root priority = 300
 sub1 priority = 800
 sub2 priority = 800
1336866872 ERROR  : root error
1336866872 ERROR sub1 context1: sub1 error
1336866872 ERROR sub1 context1_2: sub1 error
1336866872 ERROR sub1.sub2 context2: sub2 error
 root priority = 300
 sub1 priority = 800
 sub2 priority = 800
priority info
1336866872 ERROR  : root error
1336866872 ERROR sub1 context1: sub1 error
1336866872 WARN sub1 context1: sub1 warn
1336866872 ERROR sub1.sub2 context2: sub2 error
1336866872 WARN sub1.sub2 context2: sub2 warn
1336866872 ERROR sub1.sub2 context2: test vform 123
1336866872 WARN sub1.sub2 context2: streamed warn
1336866872 WARN sub1.sub2 context2: warn2....warn3..value=0
1336866872 WARN sub1.sub2 context2: ..warn4
}}}

[[PageNavi(NavigationList)]]