winlin

add srs_log_file to write log to file. change to 0.9.27

@@ -17,7 +17,11 @@ chunk_size 60000; @@ -17,7 +17,11 @@ chunk_size 60000;
17 # the logs dir. 17 # the logs dir.
18 # if enabled ffmpeg, each stracoding stream will create a log file. 18 # if enabled ffmpeg, each stracoding stream will create a log file.
19 # default: ./objs/logs 19 # default: ./objs/logs
20 -log_dir ./objs/logs; 20 +ff_log_dir ./objs/logs;
  21 +# the log file for srs.
  22 +# if not specified, disable log to file, only print to console.
  23 +# if speicfied, write log to file and print to console.
  24 +srs_log_file ./objs/srs.log;
21 # the max connections. 25 # the max connections.
22 # if exceed the max connections, server will drop the new connection. 26 # if exceed the max connections, server will drop the new connection.
23 # default: 2000 27 # default: 2000
@@ -2,5 +2,6 @@ @@ -2,5 +2,6 @@
2 # @see full.conf for detail config. 2 # @see full.conf for detail config.
3 3
4 listen 1935; 4 listen 1935;
  5 +srs_log_file ./objs/srs.log;
5 vhost __defaultVhost__ { 6 vhost __defaultVhost__ {
6 } 7 }
@@ -129,10 +129,10 @@ echo -e " | ${SrsGprofSummaryColor}gprof -b ./objs/srs gmon.out > gprof. @@ -129,10 +129,10 @@ echo -e " | ${SrsGprofSummaryColor}gprof -b ./objs/srs gmon.out > gprof.
129 echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}" 129 echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}"
130 echo -e " |${SrsResearchSummaryColor}research: ./objs/research, api server, players, ts info.\${BLACK}" 130 echo -e " |${SrsResearchSummaryColor}research: ./objs/research, api server, players, ts info.\${BLACK}"
131 echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}" 131 echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}"
132 -echo -e " |${SrsUtestSummaryColor}utest: ./objs/srs_utest, the utest for srs\${BLACK}"  
133 -echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}"  
134 echo -e " |${SrsBWTCSummaryColor}bandwidth: ./objs/bandwidth, the bandwidth test client\${BLACK}" 132 echo -e " |${SrsBWTCSummaryColor}bandwidth: ./objs/bandwidth, the bandwidth test client\${BLACK}"
135 echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}" 133 echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}"
  134 +echo -e " |${SrsUtestSummaryColor}utest: ./objs/srs_utest, the utest for srs\${BLACK}"
  135 +echo -e " \${BLACK}+------------------------------------------------------------------------------------\${BLACK}"
136 echo -e " |${SrsLibrtmpSummaryColor}librtmp @see: https://github.com/winlinvip/simple-rtmp-server/wiki/SrsLibrtmp\${BLACK}" 136 echo -e " |${SrsLibrtmpSummaryColor}librtmp @see: https://github.com/winlinvip/simple-rtmp-server/wiki/SrsLibrtmp\${BLACK}"
137 echo -e " |${SrsLibrtmpSummaryColor}librtmp: ./objs/include, ./objs/lib, the srs-librtmp library\${BLACK}" 137 echo -e " |${SrsLibrtmpSummaryColor}librtmp: ./objs/include, ./objs/lib, the srs-librtmp library\${BLACK}"
138 echo -e " | ${SrsLibrtmpSummaryColor}simple handshake: publish/play stream with simple handshake to server\${BLACK}" 138 echo -e " | ${SrsLibrtmpSummaryColor}simple handshake: publish/play stream with simple handshake to server\${BLACK}"
@@ -16,6 +16,7 @@ ROOT="./" @@ -16,6 +16,7 @@ ROOT="./"
16 APP="./objs/srs" 16 APP="./objs/srs"
17 CONFIG="./conf/srs.conf" 17 CONFIG="./conf/srs.conf"
18 DEFAULT_PID_FILE='./objs/srs.pid' 18 DEFAULT_PID_FILE='./objs/srs.pid'
  19 +DEFAULT_LOG_FILE='./objs/srs.log'
19 20
20 ######################################################################## 21 ########################################################################
21 # utility functions 22 # utility functions
@@ -65,10 +66,17 @@ start() { @@ -65,10 +66,17 @@ start() {
65 66
66 # not exists, start server 67 # not exists, start server
67 ok_msg "Starting SRS..." 68 ok_msg "Starting SRS..."
  69 +
  70 + # get log file
  71 + srs_log_file=`cat ${ROOT}/${CONFIG} |grep '^srs_log_file'| awk '{print $2}'| awk -F ';' '{print $1}'`
  72 +
68 # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000" 73 # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
69 - # TODO: FIXME: write log to, for instance, the same dir of log.  
70 # TODO: FIXME: support deamon, without nohup. 74 # TODO: FIXME: support deamon, without nohup.
71 - (cd ${ROOT}; nohup ${APP} -c ${CONFIG} >/dev/null 2>&1 &) 75 + if [[ -z $srs_log_file ]]; then
  76 + (cd ${ROOT}; nohup ${APP} -c ${CONFIG} >/dev/null 2>&1 &)
  77 + else
  78 + (cd ${ROOT}; nohup ${APP} -c ${CONFIG} >> $srs_log_file 2>&1 &)
  79 + fi
72 80
73 # check again after start server 81 # check again after start server
74 sleep 0.5 82 sleep 0.5
@@ -684,6 +684,11 @@ int SrsConfig::parse_file(const char* filename) @@ -684,6 +684,11 @@ int SrsConfig::parse_file(const char* filename)
684 // TODO: check http. 684 // TODO: check http.
685 // TODO: check pid. 685 // TODO: check pid.
686 686
  687 + std::string filename = this->get_srs_log_file();
  688 + if (!filename.empty()) {
  689 + srs_trace("open log file %s and write to", filename.c_str());
  690 + }
  691 +
687 return ret; 692 return ret;
688 } 693 }
689 694
@@ -1253,11 +1258,11 @@ string SrsConfig::get_engine_output(SrsConfDirective* engine) @@ -1253,11 +1258,11 @@ string SrsConfig::get_engine_output(SrsConfDirective* engine)
1253 return conf->arg0(); 1258 return conf->arg0();
1254 } 1259 }
1255 1260
1256 -string SrsConfig::get_log_dir() 1261 +string SrsConfig::get_ffmpeg_log_dir()
1257 { 1262 {
1258 srs_assert(root); 1263 srs_assert(root);
1259 1264
1260 - SrsConfDirective* conf = root->get("log_dir"); 1265 + SrsConfDirective* conf = root->get("ff_log_dir");
1261 if (!conf || conf->arg0().empty()) { 1266 if (!conf || conf->arg0().empty()) {
1262 return "./objs/logs"; 1267 return "./objs/logs";
1263 } 1268 }
@@ -1265,6 +1270,18 @@ string SrsConfig::get_log_dir() @@ -1265,6 +1270,18 @@ string SrsConfig::get_log_dir()
1265 return conf->arg0(); 1270 return conf->arg0();
1266 } 1271 }
1267 1272
  1273 +string SrsConfig::get_srs_log_file()
  1274 +{
  1275 + srs_assert(root);
  1276 +
  1277 + SrsConfDirective* conf = root->get("srs_log_file");
  1278 + if (!conf || conf->arg0().empty()) {
  1279 + return "";
  1280 + }
  1281 +
  1282 + return conf->arg0();
  1283 +}
  1284 +
1268 int SrsConfig::get_max_connections() 1285 int SrsConfig::get_max_connections()
1269 { 1286 {
1270 srs_assert(root); 1287 srs_assert(root);
@@ -147,7 +147,8 @@ public: @@ -147,7 +147,8 @@ public:
147 virtual int get_engine_achannels(SrsConfDirective* engine); 147 virtual int get_engine_achannels(SrsConfDirective* engine);
148 virtual void get_engine_aparams(SrsConfDirective* engine, std::vector<std::string>& aparams); 148 virtual void get_engine_aparams(SrsConfDirective* engine, std::vector<std::string>& aparams);
149 virtual std::string get_engine_output(SrsConfDirective* engine); 149 virtual std::string get_engine_output(SrsConfDirective* engine);
150 - virtual std::string get_log_dir(); 150 + virtual std::string get_ffmpeg_log_dir();
  151 + virtual std::string get_srs_log_file();
151 virtual int get_max_connections(); 152 virtual int get_max_connections();
152 virtual bool get_gop_cache(std::string vhost); 153 virtual bool get_gop_cache(std::string vhost);
153 virtual double get_queue_length(std::string vhost); 154 virtual double get_queue_length(std::string vhost);
@@ -118,7 +118,7 @@ int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine) @@ -118,7 +118,7 @@ int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine)
118 output = srs_replace(output, "[engine]", engine->arg0()); 118 output = srs_replace(output, "[engine]", engine->arg0());
119 119
120 // write ffmpeg info to log file. 120 // write ffmpeg info to log file.
121 - log_file = _srs_config->get_log_dir(); 121 + log_file = _srs_config->get_ffmpeg_log_dir();
122 log_file += "/"; 122 log_file += "/";
123 log_file += "encoder"; 123 log_file += "encoder";
124 log_file += "-"; 124 log_file += "-";
@@ -26,6 +26,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -26,6 +26,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include <stdarg.h> 26 #include <stdarg.h>
27 #include <sys/time.h> 27 #include <sys/time.h>
28 28
  29 +#include <sys/types.h>
  30 +#include <sys/stat.h>
  31 +#include <fcntl.h>
  32 +
  33 +#include <srs_app_config.hpp>
  34 +
29 SrsThreadContext::SrsThreadContext() 35 SrsThreadContext::SrsThreadContext()
30 { 36 {
31 } 37 }
@@ -57,11 +63,18 @@ SrsFastLog::SrsFastLog() @@ -57,11 +63,18 @@ SrsFastLog::SrsFastLog()
57 { 63 {
58 level = SrsLogLevel::Trace; 64 level = SrsLogLevel::Trace;
59 log_data = new char[LOG_MAX_SIZE]; 65 log_data = new char[LOG_MAX_SIZE];
  66 +
  67 + fd = -1;
60 } 68 }
61 69
62 SrsFastLog::~SrsFastLog() 70 SrsFastLog::~SrsFastLog()
63 { 71 {
64 srs_freepa(log_data); 72 srs_freepa(log_data);
  73 +
  74 + if (fd > 0) {
  75 + ::close(fd);
  76 + fd = -1;
  77 + }
65 } 78 }
66 79
67 void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...) 80 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) @@ -229,4 +242,24 @@ void SrsFastLog::write_log(char *str_log, int size, int _level)
229 } else{ 242 } else{
230 printf("\033[31m%s\033[0m", str_log); 243 printf("\033[31m%s\033[0m", str_log);
231 } 244 }
  245 +
  246 + // if specified log file, write log to it.
  247 + if (!_srs_config->get_srs_log_file().empty()) {
  248 + if (fd < 0) {
  249 + std::string filename = _srs_config->get_srs_log_file();
  250 +
  251 + fd = ::open(filename.c_str(), O_RDWR | O_APPEND);
  252 +
  253 + if(fd == -1 && errno == ENOENT) {
  254 + fd = open(filename.c_str(),
  255 + O_RDWR | O_CREAT | O_TRUNC,
  256 + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
  257 + );
  258 + }
  259 + }
  260 +
  261 + if (fd > 0) {
  262 + ::write(fd, str_log, size);
  263 + }
  264 + }
232 } 265 }
@@ -80,6 +80,8 @@ private: @@ -80,6 +80,8 @@ private:
80 // defined in SrsLogLevel. 80 // defined in SrsLogLevel.
81 int level; 81 int level;
82 char* log_data; 82 char* log_data;
  83 + // log to file if specified srs_log_file
  84 + int fd;
83 public: 85 public:
84 SrsFastLog(); 86 SrsFastLog();
85 virtual ~SrsFastLog(); 87 virtual ~SrsFastLog();
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "26" 34 +#define VERSION_REVISION "27"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "srs" 37 #define RTMP_SIG_SRS_KEY "srs"