Log4cpp 中的跨平台类库
来源:互联网 发布:手机淘宝2017官方版 编辑:程序博客网 时间:2024/06/05 08:49
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 <log4cxx/helpers/thread.h>
#include <log4cxx/helpers/mutex.h>
using namespace log4cxx;
using namespace log4cxx::helpers;
#include <iostream>
#include <vector>
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<MyThread> MyThreadPtr;
int main(int argc, char* argv[])
{
MyOutputer out;
out.output("main begin...");
vector<MyThreadPtr> 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;
}
- Log4cpp 中的跨平台类库
- Log4cpp 中的跨平台类库- -
- Log4cpp 中的跨平台类库- -
- Log4cpp 中的跨平台类库
- log4cpp-c++日志库
- log4cpp库的使用
- log4cpp
- Log4cpp
- log4cpp
- Log4cpp
- log4cpp
- Log4cpp 库安装及使用
- log4cpp 在cef 项目中的应用
- Java跨平台中的平台
- log4cpp库的一个timestamp的bug
- log4cpp应用 -日志管理类接口
- log4Cpp学习(三)—ConsoleAppender类
- log4cpp在项目中的封装使用(VC项目)
- ReferenceEquals, == , Equals 比较
- 微视频与注意力经济
- TWebBrowser编程
- 实现自定义page基类时需要注意的地方
- [翻译] Effective C++, 3rd Edition, Item 51: 编写 new 和 delete 时要遵守惯例
- Log4cpp 中的跨平台类库
- osworkflow用户持久化
- 学习笔记 06-12-28
- 搜索能精确到一个片断,或者一个镜头?这正是Blinkx Search与众不同的地方
- EasyJF开源CMS开发团队重组,欢迎开源爱好者加入!
- 原形项目取得重大突破
- struts+hibernate分页[转贴]
- Google面试经历
- MYSQL修改密码的几种方式