forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# look for Boost | ||
if(DEFINED ENV{BOOST_ROOT}) | ||
set(Boost_NO_SYSTEM_PATHS ON) | ||
endif() | ||
set(Boost_REALPATH ON) | ||
find_package(Boost COMPONENTS program_options REQUIRED) | ||
include_directories(${Boost_INCLUDE_DIR}) | ||
|
||
set(base_src | ||
base/config.cpp | ||
base/debug.cpp | ||
) | ||
|
||
add_library(base_static_lib STATIC ${base_src}) | ||
target_link_libraries(base_static_lib ${Boost_LIBRARIES}) | ||
add_library(base_shared_lib SHARED ${base_src}) | ||
target_link_libraries(base_shared_lib ${Boost_LIBRARIES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// | ||
// Created by liu on 2016/12/19. | ||
// | ||
|
||
#include "config.h" | ||
#include "iostream" | ||
#include <string> | ||
#include <boost/program_options.hpp> | ||
#include <fstream> | ||
|
||
using namespace std; | ||
|
||
const boost::program_options::variables_map &base::config::getConf() const { | ||
return conf; | ||
} | ||
|
||
string base::config::toString(string outerSep, string innerSep) { | ||
map<string, pair<Type, void*> >::iterator iter; | ||
std::ostringstream stream; | ||
for (iter = confMap.begin(); iter != confMap.end(); iter++) { | ||
stream << outerSep << iter->first << innerSep; | ||
switch (iter->second.first) { | ||
case (INT): stream << *((int*)(iter->second.second)); break; | ||
case (UNSIGNED): stream << *((unsigned*)(iter->second.second)); break; | ||
case (FLOAT): stream << *((float*)(iter->second.second)); break; | ||
case (STRING): stream << *((string*)(iter->second.second)); break; | ||
case (BOOL): stream << *((bool*)(iter->second.second)); break; | ||
} | ||
|
||
} | ||
return stream.str(); | ||
} | ||
|
||
|
||
void base::config::init(int argc, char **argv) { | ||
/* 当传入 @configFile 时要正确解析 */ | ||
/* 覆盖策略: | ||
* 前面定义的覆盖后面的 // 命令行必须在 配置文件之前 | ||
* --a b @config(其中a=c) // a最终为b | ||
* @config(其中a=b) @config(其中a=c) // a最终为b | ||
* */ | ||
try { | ||
|
||
if (argc >=2) { | ||
if (argv[1][0] == '@' && argc == 2) { // @config | ||
init(argv[1] + 1); | ||
return; | ||
} else if (argv[1][0] == '@') { // @config1 @config2 | ||
ifstream f(argv[1] + 1); if (!f) { cerr << "config file '" << (char *)(argv[1] + 1) << "' not found!"; exit(1);} | ||
po::store(po::parse_config_file(f,optionDescription), conf); | ||
f.close(); | ||
init(argc - 1, argv + 1); | ||
return; | ||
} else { | ||
int firstConfigFile = 1; | ||
for (; firstConfigFile < argc && argv[firstConfigFile][0] != '@' ; firstConfigFile += 2); | ||
if (firstConfigFile >= argc) {// --a b | ||
po::store(po::parse_command_line(argc, (const char *const *) argv, optionDescription), conf); | ||
} else { // --a b @config | ||
po::store(po::parse_command_line(firstConfigFile, (const char *const *) argv, optionDescription), conf); | ||
init(argc - firstConfigFile + 1, argv + firstConfigFile - 1); | ||
return; | ||
} | ||
} | ||
} else { | ||
po::store(po::parse_command_line(argc, (const char *const *) argv, optionDescription), conf); | ||
} | ||
if (conf.count("help")) { | ||
cerr << optionDescription << endl; | ||
exit(1); | ||
} | ||
po::notify(conf); | ||
extractBool(); | ||
} catch ( boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::required_option> > & e) { | ||
cerr << endl << "[error] " << e.get_option_name() << " must be set!" << endl << endl; | ||
cerr << optionDescription << endl; | ||
exit(1); | ||
} catch (boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::unknown_option> > & e) { | ||
cerr << endl << "[error] unrecognised option '" << e.get_option_name() << "'." << endl << endl; | ||
cerr << optionDescription << endl; | ||
exit(1); | ||
} | ||
|
||
} | ||
|
||
void base::config::init(string configFile) { | ||
try { | ||
ifstream f(configFile); | ||
if (!f) { cerr << "config file '" << configFile << "' not found!"; exit(1);} | ||
po::store(po::parse_config_file(f,optionDescription), conf); | ||
if (conf.count("help")) { | ||
cerr << optionDescription << endl; | ||
exit(1); | ||
} | ||
po::notify(conf); | ||
extractBool(); | ||
f.close(); | ||
} catch ( boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::required_option> > & e) { | ||
cerr << endl << "[error] " << e.get_option_name() << " must be set!" << endl << endl; | ||
cerr << optionDescription << endl; | ||
exit(1); | ||
} catch (boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::unknown_option> > & e) { | ||
cerr << endl << "[error] unrecognised option '" << e.get_option_name() << "'." << endl << endl; | ||
cerr << optionDescription << endl; | ||
exit(1); | ||
} | ||
} | ||
|
||
void base::config::extractBool() { | ||
map<string, pair<Type, void*> >::iterator iter; | ||
for (iter = confMap.begin(); iter != confMap.end(); iter++) { | ||
if (iter->second.first == BOOL) { | ||
*(bool*)(iter->second.second) = (bool) conf.count(iter->first); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// Created by liu on 2016/12/19. | ||
// | ||
|
||
#ifndef PROJECT_CONFIG_H | ||
#define PROJECT_CONFIG_H | ||
|
||
#include <boost/program_options.hpp> | ||
#include <boost/any.hpp> | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
using namespace std; | ||
using boost::any_cast; | ||
|
||
namespace po = boost::program_options; | ||
namespace base { | ||
|
||
class config { | ||
public: | ||
enum Type{ | ||
INT, | ||
UNSIGNED, | ||
FLOAT, | ||
STRING, | ||
BOOL, | ||
}; | ||
po::variables_map conf; | ||
map<string, pair<Type, void*> > confMap; | ||
po::options_description optionDescription; | ||
po::options_description_easy_init addOpt; | ||
|
||
public: | ||
config(string confName = "Configuration"): | ||
optionDescription(confName), | ||
addOpt(optionDescription.add_options()) | ||
{ | ||
addOpt = addOpt("help,h", "Help"); | ||
}; | ||
|
||
string toString(string outerSep = "_", string innerSep = "_"); | ||
|
||
template <class T> | ||
void registerConf(const char * name, base::config::Type type, T& arg, const char * comment); | ||
template <class T> | ||
void registerConf(const char * name, base::config::Type type, T& arg, const char * comment, T def); | ||
|
||
void extractBool(); | ||
|
||
const boost::program_options::variables_map &getConf() const; | ||
virtual void init(int argc, char * argv[]); | ||
virtual void init(string configFile); | ||
}; | ||
} | ||
|
||
template <class T> | ||
void base::config::registerConf(const char * name, base::config::Type type, T& arg, const char * comment) { | ||
confMap[name] = make_pair(type, &arg); | ||
if (type == BOOL) { | ||
addOpt = addOpt(name, comment); | ||
} else { | ||
addOpt = addOpt(name, po::value<T>(&arg)->required(), comment); | ||
} | ||
} | ||
|
||
template <class T> | ||
void base::config::registerConf(const char * name, base::config::Type type, T& arg, const char * comment, T def) { | ||
confMap[name] = make_pair(type, &arg); | ||
addOpt = addOpt(name, po::value<T>(&arg)->default_value(def), comment); | ||
} | ||
|
||
#endif //PROJECT_CONFIG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Created by liu on 2016/12/19. | ||
// | ||
|
||
#include "debug.h" | ||
|
||
base::Debug::Debug(string modelName):modelName(modelName) { | ||
if (enabledModels.count(modelName) || enabledModels.count("*")) { | ||
disable = false; | ||
} | ||
} | ||
|
||
void base::Debug::init(base::DebugConfig config) { | ||
logLevel = config.logLevel; | ||
vector<string> enModelsVec; | ||
boost::split(enModelsVec, config.enabledModels, boost::is_any_of(",")); | ||
enabledModels.clear(); | ||
for (auto i :enModelsVec) { | ||
enabledModels.insert(i); | ||
} | ||
} | ||
|
||
int base::Debug::logLevel = (int)LogLevel::info; | ||
char* base::Debug::tmpBuffer = new char[4096]; | ||
set<string> base::Debug::enabledModels; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// Created by liu on 2016/12/19. | ||
// | ||
|
||
#ifndef PROJECT_DEBUG_H | ||
#define PROJECT_DEBUG_H | ||
|
||
#include "stdarg.h" | ||
#include <string> | ||
#include <map> | ||
#include <vector> | ||
#include <set> | ||
#include <iostream> | ||
#include "config.h" | ||
#include <boost/format.hpp> | ||
#include <boost/tokenizer.hpp> | ||
#include <boost/algorithm/string.hpp> | ||
using namespace std; | ||
|
||
namespace base { | ||
enum LogLevel { | ||
info = 0, debug, war, err | ||
}; | ||
|
||
class DebugConfig: virtual public base::config { | ||
public: | ||
int logLevel; | ||
string enabledModels; | ||
DebugConfig(string confName = "Configuration"):base::config(confName) { | ||
registerConf<int> ("loglevel" , INT , logLevel , " 0 = err, war, debug, info",0); | ||
registerConf<string> ("debugModels" , STRING , enabledModels , "debuginfo enabled Models name list","*"); | ||
} | ||
}; | ||
|
||
class Debug { | ||
static set<string> enabledModels; | ||
static int logLevel; | ||
static char* tmpBuffer; | ||
public: | ||
string modelName; | ||
bool disable = true; | ||
|
||
static void init(DebugConfig config); | ||
|
||
Debug(string modelName); | ||
|
||
inline void debug(string msg, ...) { | ||
va_list ap; va_start(ap, msg); vsprintf(tmpBuffer, msg.c_str(), ap); va_end(ap); | ||
if (!disable && logLevel <= LogLevel::debug) | ||
printAtTime(" \x1b[;32mDEBUG\x1b[0m " + modelName + "\t \x1b[;32m" + tmpBuffer + "\x1b[0m"); | ||
} | ||
|
||
inline void info(string msg, ...) { | ||
va_list ap; va_start(ap, msg); vsprintf(tmpBuffer, msg.c_str(), ap); va_end(ap); | ||
if (!disable && logLevel <= LogLevel::info) | ||
printAtTime(" \x1b[;34mINFO\x1b[0m " + modelName + "\t \x1b[;34m" + tmpBuffer + "\x1b[0m"); | ||
} | ||
|
||
inline void warning(string msg, ...) { | ||
va_list ap; va_start(ap, msg); vsprintf(tmpBuffer, msg.c_str(), ap); va_end(ap); | ||
if (!disable && logLevel <= LogLevel::war) | ||
printAtTime(" \x1b[;33mWARNING\x1b[0m " + modelName + "\t \x1b[;33m" + tmpBuffer + "\x1b[0m"); | ||
} | ||
|
||
inline void error(string msg, ...) { | ||
va_list ap; va_start(ap, msg); vsprintf(tmpBuffer, msg.c_str(), ap); va_end(ap); | ||
if (!disable && logLevel <= LogLevel::err) | ||
printAtTime(" \x1b[;31mERROR\x1b[0m " + modelName + "\t \x1b[;31m" + tmpBuffer + "\x1b[0m"); | ||
} | ||
|
||
inline void printAtTime(string str) { | ||
print('[' + getTime() + ']' + str); | ||
} | ||
|
||
virtual inline void print(string str) { | ||
cout << str << endl; | ||
} | ||
|
||
static inline string getTime(const char daySep = '/', const char secSep = ':') { | ||
string formatString; | ||
formatString += "%Y"; formatString += daySep; formatString += "%m"; formatString += daySep; | ||
formatString += "%d-%H"; | ||
formatString += secSep; formatString += "%M"; formatString += secSep; formatString += "%S"; | ||
time_t t; | ||
time(&t); | ||
char buffer [2048]; | ||
strftime (buffer, sizeof(buffer), formatString.c_str(), localtime(&t)); | ||
return string(buffer); | ||
} | ||
}; | ||
} | ||
|
||
#endif //PROJECT_DEBUG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Created by liu on 2017/1/5. | ||
// | ||
|
||
#ifndef PROJECT_PROCESS_H | ||
#define PROJECT_PROCESS_H | ||
|
||
#include "config.h" | ||
namespace base { | ||
template <class ConfigClass> | ||
class Process { | ||
public: | ||
ConfigClass & config; | ||
int returnNum = 0; | ||
|
||
Process(ConfigClass &config): config(config) {} | ||
|
||
virtual void main() = 0; | ||
|
||
static ConfigClass createConfig() { | ||
return ConfigClass(); | ||
} | ||
}; | ||
} | ||
|
||
|
||
|
||
#endif //PROJECT_PROCESS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Created by liu on 2017/1/5. | ||
// | ||
|
||
#ifndef PROJECT_PROCESSLOADER_H | ||
#define PROJECT_PROCESSLOADER_H | ||
|
||
#include "config.h" | ||
#include "debug.h" | ||
|
||
namespace base{ | ||
class ProcessLoader { | ||
int argc; | ||
char ** argv; | ||
Debug * debug; | ||
|
||
public: | ||
ProcessLoader(int argc, char * argv[]):argc(argc), argv(argv) { } | ||
|
||
template <class RunClass> | ||
int runProcess() { | ||
auto c = RunClass::createConfig(); | ||
c.init(argc, argv); | ||
|
||
base::Debug::init(c); | ||
debug = new base::Debug("main"); | ||
debug->debug(c.toString("\n", " -> ")); | ||
|
||
RunClass processRunner(c); | ||
processRunner.main(); | ||
return processRunner.returnNum; | ||
} | ||
}; | ||
} | ||
|
||
|
||
|
||
#endif //PROJECT_PROCESSLOADER_H |
Oops, something went wrong.