2010-12-15 39 views
0

C++宏包裹在函數指針投入向量

#define L_(lvl) BOOST_LOG_USE_LOG_IF_LEVEL(g_l(), g_log_level(), lvl) 

是否有可能包裝成一個boost ::功能<>對象這一個宏,可能如下(從升壓日誌庫)或一個函數指針,並將其粘貼到這樣的項目的隊列或向量中?

如果是這樣,我該怎麼做?

我想寫一個機制,我有1線程寫入日誌文件的所有日誌記錄,任何工作線程都需要能夠登錄到它,但依賴於活動日誌級別, d只想記錄配置爲記錄的內容。所以我會爲任何線程創建一個隊列,將日誌消息放在隊列中,然後使用專用線程將它們寫入各自的文件,以便在不需要大量日誌記錄時減慢我的程序速度。我想使用boost :: log庫,所以我必須使用這個宏,因爲這寫入到封面下的文件。

lvl是您傳遞給此函數的日誌級別。它的用途是:L_(debug) << "some log text";

回答

1

如果你有一個固定的日誌記錄級別的,你可以只調用一個函數中,宏觀和使用一個指針函數:

void log() { 
    L_(3); 
} 

void anotherlog() { 
    L_(13); 
} 

您可以輕鬆擁有,在一個向量的然後調用他們都:

typedef void (*LogFunction)(); 
std::vector<LogFunction> logFunctions; 
logFunctions.push_back(log); 
logFunctions.push_back(anotherLog); 

std::vector<LogFunction>::iterator it, end = logFunctions.end(); 
for (it = logFunctions.begin(); it != end; ++it) 
    (*it)(); 
+0

以及這個L_宏對它的日誌消息採用的流<<操作符呢?我應該使用一個函數並重載那個運算符<<? – 2010-12-15 14:29:40

0

不,你不能把一個宏放在靠近函數指針的地方。甚至不需要像這樣的宏,你可以使用面向對象的函數。

+0

我可以但它只是我正在使用的庫的這一部分。 – 2010-12-15 14:15:25

0
void Log(int level) 
{ 
    L_(level); 
} 

typedef boost::function1<void, int> LogFunction; 

std::vector<LogFunction> LogFunctionVector; 

LogFunction L3 = Log(3); 
LogFunction L5 = Log(5); 

LogFunctionVector.push_back(L3); 
LogFunctionVector.push_back(L5); 
1
struct LogFunction 
{ 
    LogFunction(int lvl) :lvl_(lvl) {}  

    TYPE operator()() { return L_(lvl_); } 
    // Where "TYPE" is whatever type L_ returns 
private: 
    int lvl_; 
}; 
1

是否有一個理由,爲什麼你不能使用專用線WR的加速實施ITER?

typedef logger_format_write< default_, default_, writer::threading::on_dedicated_thread > logger_type;