Log4cpp 中的跨平台类库- -
来源:互联网 发布:使用usb进行网络共享 编辑:程序博客网 时间:2024/05/16 07:09
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
int main(int argc, char* argv[])
{
MyOutputer out;
out.output("main begin...");
vector
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项目)
- Things to Remember: Accustoming Yourself to C++
- 将多个视图添加到单个文档
- 如何动态控制IFrame的长和宽
- 有100万种方法使你丢掉一个工作日,却没有一个方法能弥补回来
- 身在HK
- Log4cpp 中的跨平台类库- -
- 吉祥三宝
- 第二章 游戏体系结构
- 搬家了……
- OpenGL函数中文说明二:glGenTextures(GLsizei n, GLuint *textures)函数说明
- W32单个实例
- jordan119太可恶
- 小窗终曲说策划(有空就看看吧,可能对设计想法有所帮助,不仅仅是游戏方面
- 我的问题 真的无法的 大虾帮忙解决一下