An Introduction to Log4cpp

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;}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值