用plog时,想记录一些配置信息,加了一个新的LOG类型plog::config, 在日志中就可以搜索”CONFIG”来找配置信息的日志了.
顺便整理了一个使用plog的demo.
src_test_plog.zip
编译环境 : debian7.5.0 x64
#include <string>
#include <string.h>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include "helper/const/const_define.h"
#include "helper/cfg_manager/cfg_manager.h"
#include "helper/file/file_helper.h"
#include "helper/case/case.h"
#include "3rd/plog/Log.h"
using namespace std;
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
void main_init(int argc, char* argv[]);
void main_uninit();
// show this program info on UI or on log file
void show_prog_info(bool blog, bool bui);
void test_case();
int main(int argc, char* argv[]) {
main_init(argc, argv);
/**
# /etc/test_case/test_case.ini
[log_level]
enable_verbose=1
enable_debug=1
enable_info=1
enable_warning=1
enable_error=1
enable_fatal=1
*/
MYLOG_C << "this is new log type LOG_CONFIG, log config level information, always log";
MYLOG_V << "log verbose level information by switch /etc/test_case/test_case.ini log_level/enable_verbose";
MYLOG_D << "log debug level information by switch /etc/test_case/test_case.ini log_level/enable_debug";
MYLOG_I << "log info level information by switch /etc/test_case/test_case.ini log_level/enable_info";
MYLOG_W << "log warning level information by switch /etc/test_case/test_case.ini log_level/enable_warning";
MYLOG_E << "log error level information by switch /etc/test_case/test_case.ini log_level/enable_error";
MYLOG_F << "log fatal level information by switch /etc/test_case/test_case.ini log_level/enable_fatal";
main_uninit();
return 0;
}
void main_init(int argc, char* argv[])
{
char sz_buf[MAX_PATH] = {'\0'};
pthread_mutex_init(&g_mutex_cfg, NULL);
cfg_manager::setLocker(&g_mutex_cfg);
cfg_manager::get_instance();
sprintf(sz_buf, "mkdir -p %s", LOG_ROOT_DIR);
system(sz_buf);
sprintf(sz_buf, "mkdir -p %s", DATA_DIR);
system(sz_buf);
sprintf(sz_buf, "%s/%s%8.8ld%s", LOG_ROOT_DIR, LOG_FILE_NAME_PRE_FIX, (long)getpid(), LOG_FILE_NAME_POST_FIX);
printf("log file name : %s\n", sz_buf);
plog::init(plog::verbose, sz_buf, TEST_CASE_LOG_FILE_MAX_SIZE, TEST_CASE_LOG_FILE_MAX_CNT);
MYLOG_C << "log file : " << sz_buf;
load_cfg_file(true);
show_prog_info(true, true);
}
void main_uninit()
{
MYLOG_C << ">> main_uninit()";
cfg_manager::free_instance();
pthread_mutex_destroy(&g_mutex_cfg);
}
void show_prog_info(bool blog, bool bui)
{
// 要记录的常量信息,都定义在const_define.h中
if (blog) {
MYLOG_C << "[" << RELEASE_VERSION << "][" << PROG_NAME << "][" << PORG_VER << "][" << CODE_TIME << "]";
}
if (bui) {
printf("[%s][%s][%s][%s]\n", RELEASE_VERSION, PROG_NAME, PORG_VER, CODE_TIME);
}
}
void test_case()
{
// if have case to test, please write interface on "helper/case/case.h"
// write imp on case.cpp, then call case interface
case_empty();
}
root@debian750devmin:/home/lostspeed/dev# ./bin/test_plog
log file name : /var/log/test_case/test_case_00007172.log
[1.0.1.9_170721_0950][test_case][1.0.1.10][2017-07-21 09:50]
root@debian750devmin:/home/lostspeed/dev# more /var/log/test_case/test_case_00007172.log
2017-07-24 16:21:00.052 CONFIG [7172] [main_init@71] log file : /var/log/test_case/test_case_00007172.log
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@77] >> load_cfg_file()
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@99] parse ini ok : /etc/test_case/test_case.ini
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@112] cfg_manager::get_instance()->m_log_level.b_verbose = 1
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@125] cfg_manager::get_instance()->m_log_level.b_debug = 1
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@138] cfg_manager::get_instance()->m_log_level.b_info = 1
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@151] cfg_manager::get_instance()->m_log_level.b_warning = 1
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@164] cfg_manager::get_instance()->m_log_level.b_error = 1
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@177] cfg_manager::get_instance()->m_log_level.b_fatal = 1
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@187] log config change
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@191] LOG_VERBOSE enable
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@192] LOG_DEBUG enable
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@193] LOG_INFO enable
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@194] LOG_WARNING enable
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@195] LOG_ERROR enable
2017-07-24 16:21:00.052 CONFIG [7172] [load_cfg_file@196] LOG_FATAL enable
2017-07-24 16:21:00.052 CONFIG [7172] [show_prog_info@89] [1.0.1.9_170721_0950][test_case][1.0.1.10][2017-07-21 09:50]
2017-07-24 16:21:00.052 CONFIG [7172] [main@40] this is new log type LOG_CONFIG, log config level information, always log
2017-07-24 16:21:00.052 VERB [7172] [main@42] log verbose level information by switch /etc/test_case/test_case.ini log_level/enable_verbose
2017-07-24 16:21:00.052 DEBUG [7172] [main@43] log debug level information by switch /etc/test_case/test_case.ini log_level/enable_debug
2017-07-24 16:21:00.052 INFO [7172] [main@44] log info level information by switch /etc/test_case/test_case.ini log_level/enable_info
2017-07-24 16:21:00.052 WARN [7172] [main@45] log warning level information by switch /etc/test_case/test_case.ini log_level/enable_warning
2017-07-24 16:21:00.052 ERROR [7172] [main@46] log error level information by switch /etc/test_case/test_case.ini log_level/enable_error
2017-07-24 16:21:00.052 FATAL [7172] [main@47] log fatal level information by switch /etc/test_case/test_case.ini log_level/enable_fatal
2017-07-24 16:21:00.052 CONFIG [7172] [main_uninit@80] >> main_uninit()