diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 06fdfdb9d..19224ec04 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -17,7 +17,11 @@ chunk_size 60000; # the logs dir. # if enabled ffmpeg, each stracoding stream will create a log file. # default: ./objs/logs -log_dir ./objs/logs; +ff_log_dir ./objs/logs; +# the log file for srs. +# if not specified, disable log to file, only print to console. +# if speicfied, write log to file and print to console. +srs_log_file ./objs/srs.log; # the max connections. # if exceed the max connections, server will drop the new connection. # default: 2000 diff --git a/trunk/conf/srs.conf b/trunk/conf/srs.conf index 0e4140af3..d316884ae 100644 --- a/trunk/conf/srs.conf +++ b/trunk/conf/srs.conf @@ -2,5 +2,6 @@ # @see full.conf for detail config. listen 1935; +srs_log_file ./objs/srs.log; vhost __defaultVhost__ { } diff --git a/trunk/configure b/trunk/configure index 9e7b7c8f4..61f170b3d 100755 --- a/trunk/configure +++ b/trunk/configure @@ -129,10 +129,10 @@ echo -e " | ${SrsGprofSummaryColor}gprof -b ./objs/srs gmon.out > gprof. echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}" echo -e " |${SrsResearchSummaryColor}research: ./objs/research, api server, players, ts info.\${BLACK}" echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}" -echo -e " |${SrsUtestSummaryColor}utest: ./objs/srs_utest, the utest for srs\${BLACK}" -echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}" echo -e " |${SrsBWTCSummaryColor}bandwidth: ./objs/bandwidth, the bandwidth test client\${BLACK}" echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}" +echo -e " |${SrsUtestSummaryColor}utest: ./objs/srs_utest, the utest for srs\${BLACK}" +echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}" echo -e " |${SrsLibrtmpSummaryColor}librtmp @see: https://github.com/winlinvip/simple-rtmp-server/wiki/SrsLibrtmp\${BLACK}" echo -e " |${SrsLibrtmpSummaryColor}librtmp: ./objs/include, ./objs/lib, the srs-librtmp library\${BLACK}" echo -e " | ${SrsLibrtmpSummaryColor}simple handshake: publish/play stream with simple handshake to server\${BLACK}" diff --git a/trunk/etc/init.d/srs b/trunk/etc/init.d/srs index 9b7bf849c..d1b5fd139 100755 --- a/trunk/etc/init.d/srs +++ b/trunk/etc/init.d/srs @@ -16,6 +16,7 @@ ROOT="./" APP="./objs/srs" CONFIG="./conf/srs.conf" DEFAULT_PID_FILE='./objs/srs.pid' +DEFAULT_LOG_FILE='./objs/srs.log' ######################################################################## # utility functions @@ -65,10 +66,17 @@ start() { # not exists, start server ok_msg "Starting SRS..." + + # get log file + srs_log_file=`cat ${ROOT}/${CONFIG} |grep '^srs_log_file'| awk '{print $2}'| awk -F ';' '{print $1}'` + # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000" - # TODO: FIXME: write log to, for instance, the same dir of log. # TODO: FIXME: support deamon, without nohup. - (cd ${ROOT}; nohup ${APP} -c ${CONFIG} >/dev/null 2>&1 &) + if [[ -z $srs_log_file ]]; then + (cd ${ROOT}; nohup ${APP} -c ${CONFIG} >/dev/null 2>&1 &) + else + (cd ${ROOT}; nohup ${APP} -c ${CONFIG} >> $srs_log_file 2>&1 &) + fi # check again after start server sleep 0.5 diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 5d677b147..353195c4e 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -684,6 +684,11 @@ int SrsConfig::parse_file(const char* filename) // TODO: check http. // TODO: check pid. + std::string filename = this->get_srs_log_file(); + if (!filename.empty()) { + srs_trace("open log file %s and write to", filename.c_str()); + } + return ret; } @@ -1253,11 +1258,11 @@ string SrsConfig::get_engine_output(SrsConfDirective* engine) return conf->arg0(); } -string SrsConfig::get_log_dir() +string SrsConfig::get_ffmpeg_log_dir() { srs_assert(root); - SrsConfDirective* conf = root->get("log_dir"); + SrsConfDirective* conf = root->get("ff_log_dir"); if (!conf || conf->arg0().empty()) { return "./objs/logs"; } @@ -1265,6 +1270,18 @@ string SrsConfig::get_log_dir() return conf->arg0(); } +string SrsConfig::get_srs_log_file() +{ + srs_assert(root); + + SrsConfDirective* conf = root->get("srs_log_file"); + if (!conf || conf->arg0().empty()) { + return ""; + } + + return conf->arg0(); +} + int SrsConfig::get_max_connections() { srs_assert(root); diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index ae5699d1b..85c244fcf 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -147,7 +147,8 @@ public: virtual int get_engine_achannels(SrsConfDirective* engine); virtual void get_engine_aparams(SrsConfDirective* engine, std::vector& aparams); virtual std::string get_engine_output(SrsConfDirective* engine); - virtual std::string get_log_dir(); + virtual std::string get_ffmpeg_log_dir(); + virtual std::string get_srs_log_file(); virtual int get_max_connections(); virtual bool get_gop_cache(std::string vhost); virtual double get_queue_length(std::string vhost); diff --git a/trunk/src/app/srs_app_encoder.cpp b/trunk/src/app/srs_app_encoder.cpp index 3fbc79787..f418e02a3 100644 --- a/trunk/src/app/srs_app_encoder.cpp +++ b/trunk/src/app/srs_app_encoder.cpp @@ -118,7 +118,7 @@ int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine) output = srs_replace(output, "[engine]", engine->arg0()); // write ffmpeg info to log file. - log_file = _srs_config->get_log_dir(); + log_file = _srs_config->get_ffmpeg_log_dir(); log_file += "/"; log_file += "encoder"; log_file += "-"; diff --git a/trunk/src/app/srs_app_log.cpp b/trunk/src/app/srs_app_log.cpp index 2c877e52a..a327171eb 100644 --- a/trunk/src/app/srs_app_log.cpp +++ b/trunk/src/app/srs_app_log.cpp @@ -26,6 +26,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include +#include +#include +#include + +#include + SrsThreadContext::SrsThreadContext() { } @@ -57,11 +63,18 @@ SrsFastLog::SrsFastLog() { level = SrsLogLevel::Trace; log_data = new char[LOG_MAX_SIZE]; + + fd = -1; } SrsFastLog::~SrsFastLog() { srs_freepa(log_data); + + if (fd > 0) { + ::close(fd); + fd = -1; + } } void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...) @@ -229,4 +242,24 @@ void SrsFastLog::write_log(char *str_log, int size, int _level) } else{ printf("\033[31m%s\033[0m", str_log); } + + // if specified log file, write log to it. + if (!_srs_config->get_srs_log_file().empty()) { + if (fd < 0) { + std::string filename = _srs_config->get_srs_log_file(); + + fd = ::open(filename.c_str(), O_RDWR | O_APPEND); + + if(fd == -1 && errno == ENOENT) { + fd = open(filename.c_str(), + O_RDWR | O_CREAT | O_TRUNC, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH + ); + } + } + + if (fd > 0) { + ::write(fd, str_log, size); + } + } } diff --git a/trunk/src/app/srs_app_log.hpp b/trunk/src/app/srs_app_log.hpp index 4967368aa..8200e04c0 100644 --- a/trunk/src/app/srs_app_log.hpp +++ b/trunk/src/app/srs_app_log.hpp @@ -80,6 +80,8 @@ private: // defined in SrsLogLevel. int level; char* log_data; + // log to file if specified srs_log_file + int fd; public: SrsFastLog(); virtual ~SrsFastLog(); diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 5993fd7fe..5a1e1f844 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR "0" #define VERSION_MINOR "9" -#define VERSION_REVISION "26" +#define VERSION_REVISION "27" #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION // server info. #define RTMP_SIG_SRS_KEY "srs"