An Introduction to Log4cpp
来源:互联网 发布:蛤的意义知乎 编辑:程序博客网 时间:2024/05/17 20:32
Log4cpp 是 Log4J 的 C++ 移植版本,开放源代码并且完全免费。与 Log4J 能够跨平台一样,Log4cpp 也致力于写出跨平台的 C++ 程序。Log4cpp 主要是用于 C++ 程序中写 log 文件,与此同时,Log4cpp 中有很多有用的类库,对于写跨平台 C++ 程序的人来说,可以直接拿来用,或者作为自己写跨平台类的参考。
Log4cpp 中的跨平台类库有明显的 Java 痕迹,比如 Class、Object 、Loader、Locale 等类。 Log4cpp 中的类都可以根据类名 new 出一个 instance,其实现的方式和 MFC 如出一辙:通过 C++ 强大的宏来实现。
Log4cpp 中的跨平台类库主要有:
信号类:Condition(broadcast,signal,wait),CriticalSection(lock,unlock),WaitAccess,Event(set,reset,wait),Mutex(lock,unlock),Semaphore(wait,tryWait,post)
网络类:InetAddress,Socket,ServerSocket,DatagramSocket,SocketInputStream,SocketOutputStream
日期类:DateFormat,DateTimeDateFormat,System(currentTimeMillis)
文件类:FileWatchdog(doOnChange)
内存操作类:基于引用计数机制的智能指针 ObjectPtrT
字符串操作类:StrictMath,StringHelper(toUpperCase,toLowerCase,trim,equalsIgnoreCase,endsWith,format),StringTokenizer
线程类:Thread(start,run,join)
使用以上的类不用考虑 thread handle, event handle, socket handle 之类的 handle 问题,所有这些文件已经被封装了。很好用,对不对?
不足之处在于没有 GUI 类。ANSI C++ 中对于目录等文件系统的处理功能较弱,这里面也没有目录处理类。另外Socket 的 read(void * buf, size_t len) 不能设置 timeout,并且如果读取数据个数小于 len 那么 read 函数将一直堵塞,不太好用,很可惜。实际的使用上面,可以考虑做一个 Socket 子类,重写 read() 函数。
以下是一个示例程序,在 VC6 下编译运行通过,代码中有中文全角空格。
#include "stdafx.h"#include#include using namespace log4cxx;using namespace log4cxx::helpers;#include #include using namespace std;class MyOutputer{private: Mutex m_outputLock;public: void output(const char * msg){ m_outputLock.lock(); cout << msg << endl; m_outputLock.unlock(); }};class MyThread : public Thread{private: bool m_running; MyOutputer m_out;public: MyThread(){m_running = false; } virtual ~MyThread(){} virtual void run(){ m_running = true; //循环十次,执行十秒 for(int i =0; i < 10 && m_running; i++){ m_out.output("MyThread running..."); Thread::sleep(1000); } } void stop(){ m_running = false; }};typedef ObjectPtrT MyThreadPtr;int main(int argc, char* argv[]){ MyOutputer out; out.output("main begin..."); vector threadList; int count = 5, i=0; for(i =0; i< count; i++){ MyThread *pThread = new MyThread(); threadList.push_back(pThread); } out.output("main start all threads..."); for(i =0; i< count; i++){ MyThread *pThread = threadList[i]; Thread::sleep(300); pThread->start(); } out.output("main sleep 4 seconds...");
//等 4 秒,停止所有线程 Thread::sleep(4000); out.output("main stop all threads..."); for(i =0; i< count; i++){ MyThread *pThread = threadList[i]; pThread->stop(); } out.output("main wait all threads..."); //等待所有线程中止
for(i =0; i< count; i++){ MyThread *pThread = threadList[i]; pThread->join(); } threadList.clear(); //所有线程对象被启动销毁 out.output("main end"); return 0;}
- An Introduction to Log4cpp
- An Introduction to Struts
- An introduction to LaTeX2e
- An Introduction To Ajax
- An introduction to SOA
- An introduction to Microcode
- An Introduction to LDAP
- An Introduction To SQLite
- An Introduction to Libaio
- An Introduction to LDAP
- An Introduction to GCC
- An Introduction to OpenCL
- An Introduction to ANYDATA
- An Introduction to NFC
- An Introduction to GCC
- An Introduction to ANYDATA
- An introduction to JSON
- AN INTRODUCTION TO MOOS
- 梅花引.荆溪阻雪
- 快速启动里添加桌面
- 关于LINUX中的中断处理
- 只要理想和良心在心中:俞敏洪北大开学典礼的发言
- centos5启动和停止那些服务
- An Introduction to Log4cpp
- 深入浅出 Linux设备驱动中断处理介绍
- VC中_T、_TEXT和TEXT宏的作用(来自互联网)
- Objective-c sort array int方法
- FW:Use Log4XX for Logging
- 小议传统分层与新式分层,抑或与DDD分层
- 关于称量球问题的证明
- Answer Wisely
- ubuntu常用软件