Log模塊見雛形了,之因此最開始寫Log模塊,主要是考慮先寫好了這個模塊,之後在開發其餘模塊時,能夠調用Log模塊很容易的輸出Log信息,方便開發過程當中調試程序。Log模塊大體以下:服務器
在程序中調用Log模塊輸出日誌信息,Log模塊會根據Log等級判斷是否是要把log信息輸出到各個監聽器,Log模塊從配置模塊讀取配置信息,來設置最低輸出的log等級和監聽器。併發
各個監聽器分別把信息輸出到控制檯,Log服務器,Log文件,系統日誌等。高併發
使用這個模塊仍是很是簡單的,示例代碼:spa
- #include "god/log.h"
-
- using namespace std;
- using namespace G;
-
- static Logger::ptr g_loggera = Log::find("ModuleA");
- static Logger::ptr g_loggerb = Log::find("ModuleB");
-
- int main(int argc, char *argv[]) {
- GOD_LOG_FATAL(g_loggera) << "Fatal,libgod";
- GOD_LOG_FATAL(g_loggerb) << "Fatal,嘿嘿";
-
- GOD_LOG_ERROR(g_loggera) << "Error,libgod";
- GOD_LOG_ERROR(g_loggerb) << "Error,嘿嘿";
-
- GOD_LOG_WARNING(g_loggera) << "Warning,libgod";
- GOD_LOG_WARNING(g_loggerb) << "Warning,嘿嘿";
-
- GOD_LOG_INFO(g_loggera) << "Info,libgod";
- GOD_LOG_INFO(g_loggerb) << "Info,嘿嘿";
-
- GOD_LOG_TRACE(g_loggera) << "Trace,libgod";
- GOD_LOG_TRACE(g_loggerb) << "Trace,嘿嘿";
-
- GOD_LOG_DEBUG(g_loggera) << "Debug,libgod";
- GOD_LOG_DEBUG(g_loggerb) << "Debug,嘿嘿";
- return 0;
- }
運行結果:調試
- ./bin/log.exe
- 2012-Nov-16 16:04:57.425740 FATAL ModuleA src/log.cpp:10 Fatal,libgod
- 2012-Nov-16 16:04:57.426163 FATAL ModuleB src/log.cpp:11 Fatal,嘿嘿
- 2012-Nov-16 16:04:57.426231 ERROR ModuleA src/log.cpp:13 Error,libgod
- 2012-Nov-16 16:04:57.426390 ERROR ModuleB src/log.cpp:14 Error,嘿嘿
- 2012-Nov-16 16:04:57.426460 WARNING ModuleA src/log.cpp:16 Warning,libgod
- 2012-Nov-16 16:04:57.426516 WARNING ModuleB src/log.cpp:17 Warning,嘿嘿
- 2012-Nov-16 16:04:57.426571 INFO ModuleA src/log.cpp:19 Info,libgod
- 2012-Nov-16 16:04:57.426626 INFO ModuleB src/log.cpp:20 Info,嘿嘿