log4cpp Android JNI开发移植

本文介绍了如何将log4cpp库移植到Android平台,通过JNI技术进行调用,详细解析了关键代码和下载链接。

简介

log4cpp是个基于LGPL的开源项目,是基于优秀的日志处理跟踪项目Java语言的log4j移植过来的。log4j介绍的文档很多,在java领域使用的也比较广泛,而这个功能强大的库对国内的C++语言开发人员却使用的不多。本文介绍了将log4cpp移植到Android平台,进行NDK开发。
log4cpp的优点:
提供应用程序运行上下文,方便跟踪调试;
可扩展的、多种方式记录日志,包括命令行、文件、回卷文件、内存、syslog服务器、Win事件日志等;
可以动态控制日志记录级别,在效率和功能中进行调整;
所有配置可以通过配置文件进行动态调整;
多语言支持,包括Java(log4j),C++(log4cpp、log4cplus),C(log4c),python(log4p)等;

主要代码

主要通过创建 AndroidAppender继承LayoutAppender来实现Android NDK的log打印
/*

 * AndroidAppender.cpp

 *

 * Copyright 2002, the Log4cpp project.

 *

 * See the COPYING file for the terms of usage and distribution.

 */





#include "PortabilityImpl.hh"



#include <android/log.h>

#include <stdio.h>

 

#include <log4cpp/Category.hh>

#include <log4cpp/AndroidAppender.hh>

#include <log4cpp/FactoryParams.hh>

#include <log4cpp/Priority.hh>



namespace log4cpp {



    AndroidAppender::AndroidAppender(const std::string& name) : 

            LayoutAppender(name) {

    }

    

    AndroidAppender::~AndroidAppender() {

        close();

    }



    void AndroidAppender::close() {

    }



    void AndroidAppender::_append(const LoggingEvent& event) {

        const char* tag = (event.categoryName).c_str();

        const char* message = (event.message).c_str();



        switch(event.priority){

            case Priority::PriorityLevel::NOTICE:

                 __android_log_write(ANDROID_LOG_VERBOSE, tag, message);

                 break;

            case Priority::PriorityLevel::DEBUG:

                 __android_log_write(ANDROID_LOG_DEBUG, tag, message);

                 break;

            case Priority::PriorityLevel::INFO:

                 __android_log_write(ANDROID_LOG_INFO, tag, message);

                 break;

            case Priority::PriorityLevel::WARN:

                 __android_log_write(ANDROID_LOG_WARN, tag, message);

                 break;

            case Priority::PriorityLevel::ERROR:

                 __android_log_write(ANDROID_LOG_ERROR, tag, message);

                 break;

        }



    }

   

}

测试代码如下
#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/AndroidAppender.hh>
#include <log4cpp/SimpleLayout.hh>
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "abc", __VA_ARGS__)
extern "C" {

/*
 * Class:     com_example_testlog4cpp_MainActivity
 * Method:    testNativeLog
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_example_testlog4cpp_MainActivity_testNativeLog
  (JNIEnv *, jobject){

    log4cpp::Appender *appender = new log4cpp::AndroidAppender("Appender");
    log4cpp::Layout *layout = new log4cpp::SimpleLayout();
    //log4cpp::Layout *layout = new log4cpp::BasicLayout();
    log4cpp::Category& category = log4cpp::Category::getInstance("abc");

    appender->setLayout(layout);
    category.setAppender(appender);
//    category.setPriority(log4cpp::Priority::INFO);

    /*The actual logging*/
    category.info("This is for tracing the flow");
    category.notice("This is to notify certain events");
    category.warn("This is to generate certain warnings");
}

}


代码下载

http://download.csdn.net/detail/xiaowang0924/9456922
http://download.csdn.net/detail/xiaowang0924/9456926
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值