• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            遨游Code Sky

            共享今天Code Sky, 共創(chuàng)明天Code Sky!

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              12 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks
                   Log4cpp 是 Log4J 的 C++ 移植版本,開放源代碼并且完全免費。與 Log4J 能夠跨平臺一樣,Log4cpp 也致力于寫出跨平臺的 C++ 程序。Log4cpp 主要是用于 C++ 程序中寫 log 文件,與此同時,Log4cpp 中有很多有用的類庫,對于寫跨平臺 C++ 程序的人來說,可以直接拿來用,或者作為自己寫跨平臺類的參考。

            Log4cpp 中的跨平臺類庫有明顯的 Java 痕跡,比如 Class、Object 、Loader、Locale 等類。 Log4cpp 中的類都可以根據(jù)類名 new 出一個 instance,其實現(xiàn)的方式和 MFC 如出一轍:通過 C++ 強大的宏來實現(xiàn)。

            Log4cpp 中的跨平臺類庫主要有:

            信號類:Condition(broadcast,signal,wait),CriticalSection(lock,unlock),WaitAccess,Event(set,reset,wait),Mutex(lock,unlock),Semaphore(wait,tryWait,post)

            網(wǎng)絡類:InetAddress,Socket,ServerSocket,DatagramSocket,SocketInputStream,SocketOutputStream

            日期類:DateFormat,DateTimeDateFormat,System(currentTimeMillis)

            文件類:FileWatchdog(doOnChange)

            內存操作類:基于引用計數(shù)機制的智能指針 ObjectPtrT

            字符串操作類:StrictMath,StringHelper(toUpperCase,toLowerCase,trim,equalsIgnoreCase,endsWith,format),StringTokenizer

            線程類:Thread(start,run,join)

            使用以上的類不用考慮 thread handle, event handle, socket handle 之類的 handle 問題,所有這些文件已經(jīng)被封裝了。很好用,對不對?

            不足之處在于沒有 GUI 類。ANSI C++ 中對于目錄等文件系統(tǒng)的處理功能較弱,這里面也沒有目錄處理類。另外 Socket 的 read(void * buf, size_t len) 不能設置 timeout,并且如果讀取數(shù)據(jù)個數(shù)小于 len 那么 read 函數(shù)將一直堵塞,不太好用,很可惜。實際的使用上面,可以考慮做一個 Socket 子類,重寫 read() 函數(shù)。

            以下是一個示例程序,在 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;
                 //循環(huán)十次,執(zhí)行十秒
                 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;
            }

             

            posted on 2007-09-24 17:26 劉遠清 閱讀(408) 評論(0)  編輯 收藏 引用
            久久久久亚洲AV无码麻豆| 久久免费高清视频| 久久国产成人亚洲精品影院| 狠狠人妻久久久久久综合| 日本加勒比久久精品| 久久香蕉超碰97国产精品 | 狠狠色噜噜色狠狠狠综合久久| 少妇内射兰兰久久| 亚洲欧美日韩久久精品| 日本精品久久久久中文字幕8 | 中文字幕日本人妻久久久免费| 日本欧美久久久久免费播放网| 国产精品99久久久久久猫咪| 国产精品久久久久天天影视| 热99RE久久精品这里都是精品免费| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 99热成人精品热久久669| 久久嫩草影院免费看夜色| 天天久久狠狠色综合| 久久精品国产亚洲AV久| 久久国内免费视频| 久久伊人五月天论坛| 久久人妻少妇嫩草AV无码专区| 亚洲欧洲精品成人久久曰影片| 国产精品伦理久久久久久| 久久久国产乱子伦精品作者| 亚洲国产精品成人AV无码久久综合影院 | 国产精品无码久久综合| 亚洲午夜无码久久久久| 久久久WWW成人| 久久人人爽人人爽人人片AV不 | av国内精品久久久久影院 | 精品久久久久久无码人妻热| 成人国内精品久久久久影院| 久久国产精品成人片免费| 嫩草伊人久久精品少妇AV| 久久久久亚洲av成人网人人软件 | 国产精品免费久久久久电影网| 色综合久久综精品| 久久精品国产亚洲Aⅴ蜜臀色欲| 91亚洲国产成人久久精品网址|