winlin

add log tank config, default to console.

... ... @@ -18,10 +18,11 @@ chunk_size 60000;
# if enabled ffmpeg, each stracoding stream will create a log file.
# default: ./objs/logs
ff_log_dir ./objs/logs;
# the log file for srs.
# if not specified or empty, disable log to file, only print to console.
# if speicfied, write log to file and print to console.
# default: empty.
# the log tank, console or file.
# if console, print log to console.
# if file, write log to file. requires srs_log_file if log to file.
# default: console.
srs_log_tank console;
srs_log_file ./objs/srs.log;
# the max connections.
# if exceed the max connections, server will drop the new connection.
... ...
... ... @@ -2,6 +2,7 @@
# @see full.conf for detail config.
listen 1935;
srs_log_tank file;
srs_log_file ./objs/srs.log;
vhost __defaultVhost__ {
}
... ...
... ... @@ -27,11 +27,11 @@ YELLOW="\\e[33m"
BLACK="\\e[0m"
POS="\\e[60G"
ok_msg(){
ok_msg() {
echo -e "${1}${POS}${BLACK}[${GREEN} OK ${BLACK}]"
}
failed_msg(){
failed_msg() {
echo -e "${1}${POS}${BLACK}[${RED}FAILED${BLACK}]"
}
... ... @@ -43,14 +43,14 @@ failed_msg(){
# @set variable $error_msg if error.
# @set variable $srs_pid_file to pid file.
load_process_info() {
# get pid file
srs_pid_file=`cat ${ROOT}/${CONFIG} |grep ^pid|awk '{print $2}'|awk -F ';' '{print $1}'`
if [[ -z $srs_pid_file ]]; then srs_pid_file=${DEFAULT_PID_FILE}; fi
# get abs path
srs_pid_dir=`dirname $srs_pid_file`
srs_pid_file=`(cd ${ROOT}; cd $srs_pid_dir; pwd)`/`basename $srs_pid_file`
if [[ -f $srs_pid_file ]]; then
srs_pid=`cat $srs_pid_file 2>/dev/null`
else
srs_pid=`cat ${ROOT}/$srs_pid_file 2>/dev/null`
fi
srs_pid=`cat $srs_pid_file 2>/dev/null`
ret=$?; if [[ 0 -ne $ret ]]; then error_msg="file $srs_pid_file does not exists"; return 1; fi
ps -p ${srs_pid} >/dev/null 2>/dev/null
... ... @@ -69,21 +69,24 @@ start() {
# get log file
srs_log_file=`cat ${ROOT}/${CONFIG} |grep '^srs_log_file'| awk '{print $2}'| awk -F ';' '{print $1}'`
if [[ -z $srs_log_file ]]; then srs_log_file=${DEFAULT_LOG_FILE}; fi
# get abs path
srs_log_dir=`dirname $srs_log_file`
srs_log_file=`(cd ${ROOT}; cd $srs_log_dir; pwd)`/`basename $srs_log_file`
# TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
if [[ -z $srs_log_file ]]; then
(cd ${ROOT}; ${APP} -c ${CONFIG} >/dev/null 2>&1)
(cd ${ROOT}; ${ROOT}/${APP} -c ${CONFIG} >/dev/null 2>&1)
else
(cd ${ROOT}; ${APP} -c ${CONFIG} >> $srs_log_file 2>&1)
(cd ${ROOT}; ${ROOT}/${APP} -c ${CONFIG} >> $srs_log_file 2>&1)
fi
# check again after start server
sleep 1
for ((i = 0; i < 5; i++)); do
# sleep a little while, for srs may start then crash.
sleep 0.1
load_process_info
ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "SRS start failed"; return $ret; fi
ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "SRS start failed, see $srs_log_file"; return $ret; fi
done
# check whether started.
... ...
... ... @@ -308,7 +308,7 @@ int SrsConfDirective::read_token(SrsFileBuffer* buffer, std::vector<string>& arg
srs_error("line %d: unexpected end of file, expecting ; or \"}\"", buffer->line);
return ERROR_SYSTEM_CONFIG_INVALID;
}
srs_trace("end of file. ret=%d", ret);
srs_trace("config parsed EOF");
return ret;
}
... ... @@ -684,7 +684,13 @@ int SrsConfig::parse_file(const char* filename)
// TODO: check http.
// TODO: check pid.
// check log
std::string log_filename = this->get_srs_log_file();
if (this->get_srs_log_tank_file() && log_filename.empty()) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("must specifies the file to write log to. ret=%d", ret);
return ret;
}
if (!log_filename.empty()) {
srs_trace("log file is %s", log_filename.c_str());
}
... ... @@ -1282,6 +1288,18 @@ string SrsConfig::get_srs_log_file()
return conf->arg0();
}
bool SrsConfig::get_srs_log_tank_file()
{
srs_assert(root);
SrsConfDirective* conf = root->get("srs_log_tank");
if (conf && conf->arg0() == "file") {
return true;
}
return false;
}
bool SrsConfig::get_deamon()
{
srs_assert(root);
... ...
... ... @@ -148,6 +148,7 @@ public:
virtual void get_engine_aparams(SrsConfDirective* engine, std::vector<std::string>& aparams);
virtual std::string get_engine_output(SrsConfDirective* engine);
virtual std::string get_ffmpeg_log_dir();
virtual bool get_srs_log_tank_file();
virtual std::string get_srs_log_file();
virtual bool get_deamon();
virtual int get_max_connections();
... ...
... ... @@ -42,7 +42,7 @@ SrsThreadContext::~SrsThreadContext()
void SrsThreadContext::generate_id()
{
static int id = 1;
static int id = 100;
cache[st_thread_self()] = id++;
}
... ... @@ -230,20 +230,22 @@ void SrsFastLog::write_log(char *str_log, int size, int _level)
log_data[size++] = LOG_TAIL;
log_data[size++] = 0;
// if is error msg, then print color msg.
// \033[31m : red text code in shell
// \033[32m : green text code in shell
// \033[33m : yellow text code in shell
// \033[0m : normal text code
if (_level <= SrsLogLevel::Trace) {
printf("%s", str_log);
} else if (_level == SrsLogLevel::Warn) {
printf("\033[33m%s\033[0m", str_log);
} else{
printf("\033[31m%s\033[0m", str_log);
if (fd < 0 || !_srs_config->get_srs_log_tank_file()) {
// if is error msg, then print color msg.
// \033[31m : red text code in shell
// \033[32m : green text code in shell
// \033[33m : yellow text code in shell
// \033[0m : normal text code
if (_level <= SrsLogLevel::Trace) {
printf("%s", str_log);
} else if (_level == SrsLogLevel::Warn) {
printf("\033[33m%s\033[0m", str_log);
} else{
printf("\033[31m%s\033[0m", str_log);
}
}
// open log file.
// open log file. if specified
if (!_srs_config->get_srs_log_file().empty() && fd < 0) {
std::string filename = _srs_config->get_srs_log_file();
... ... @@ -256,8 +258,9 @@ void SrsFastLog::write_log(char *str_log, int size, int _level)
);
}
}
// write log to file.
if (fd > 0) {
if (fd > 0 && _srs_config->get_srs_log_tank_file()) {
::write(fd, str_log, size);
}
}
... ...
... ... @@ -179,26 +179,6 @@ SrsServer::~SrsServer()
int SrsServer::initialize()
{
int ret = ERROR_SUCCESS;
// use linux epoll.
if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) {
ret = ERROR_ST_SET_EPOLL;
srs_error("st_set_eventsys use linux epoll failed. ret=%d", ret);
return ret;
}
srs_verbose("st_set_eventsys use linux epoll success");
if(st_init() != 0){
ret = ERROR_ST_INITIALIZE;
srs_error("st_init failed. ret=%d", ret);
return ret;
}
srs_verbose("st_init success");
// set current log id.
_srs_context->generate_id();
srs_info("log set id success");
return ret;
}
... ... @@ -277,6 +257,32 @@ int SrsServer::acquire_pid_file()
return ret;
}
int SrsServer::initialize_st()
{
int ret = ERROR_SUCCESS;
// use linux epoll.
if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) {
ret = ERROR_ST_SET_EPOLL;
srs_error("st_set_eventsys use linux epoll failed. ret=%d", ret);
return ret;
}
srs_verbose("st_set_eventsys use linux epoll success");
if(st_init() != 0){
ret = ERROR_ST_INITIALIZE;
srs_error("st_init failed. ret=%d", ret);
return ret;
}
srs_verbose("st_init success");
// set current log id.
_srs_context->generate_id();
srs_info("log set id success");
return ret;
}
int SrsServer::listen()
{
int ret = ERROR_SUCCESS;
... ...
... ... @@ -80,6 +80,7 @@ public:
public:
virtual int initialize();
virtual int acquire_pid_file();
virtual int initialize_st();
virtual int listen();
virtual int cycle();
virtual void remove(SrsConnection* conn);
... ...
... ... @@ -60,18 +60,8 @@ int run_master()
signal(SIGNAL_RELOAD, handler);
signal(SIGTERM, handler);
signal(SIGINT, handler);
srs_trace("uname: "SRS_UNAME);
srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");
srs_trace("configure: "SRS_CONFIGURE);
if ((ret = _srs_server->initialize()) != ERROR_SUCCESS) {
return ret;
}
// TODO: create log dir in _srs_config->get_log_dir()
if ((ret = _srs_server->acquire_pid_file()) != ERROR_SUCCESS) {
if ((ret = _srs_server->initialize_st()) != ERROR_SUCCESS) {
return ret;
}
... ... @@ -137,6 +127,8 @@ int main(int argc, char** argv)
{
int ret = ERROR_SUCCESS;
srs_trace("srs(simple-rtmp-server)");
// TODO: support both little and big endian.
srs_assert(srs_is_little_endian());
... ... @@ -160,6 +152,18 @@ int main(int argc, char** argv)
if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) {
return ret;
}
srs_trace("uname: "SRS_UNAME);
srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");
srs_trace("configure: "SRS_CONFIGURE);
if ((ret = _srs_server->initialize()) != ERROR_SUCCESS) {
return ret;
}
if ((ret = _srs_server->acquire_pid_file()) != ERROR_SUCCESS) {
return ret;
}
return run();
}
... ...