winlin

add log tank config, default to console.

@@ -18,10 +18,11 @@ chunk_size 60000; @@ -18,10 +18,11 @@ chunk_size 60000;
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 ff_log_dir ./objs/logs; 20 ff_log_dir ./objs/logs;
21 -# the log file for srs.  
22 -# if not specified or empty, disable log to file, only print to console.  
23 -# if speicfied, write log to file and print to console.  
24 -# default: empty. 21 +# the log tank, console or file.
  22 +# if console, print log to console.
  23 +# if file, write log to file. requires srs_log_file if log to file.
  24 +# default: console.
  25 +srs_log_tank console;
25 srs_log_file ./objs/srs.log; 26 srs_log_file ./objs/srs.log;
26 # the max connections. 27 # the max connections.
27 # if exceed the max connections, server will drop the new connection. 28 # if exceed the max connections, server will drop the new connection.
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
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_tank file;
5 srs_log_file ./objs/srs.log; 6 srs_log_file ./objs/srs.log;
6 vhost __defaultVhost__ { 7 vhost __defaultVhost__ {
7 } 8 }
@@ -27,11 +27,11 @@ YELLOW="\\e[33m" @@ -27,11 +27,11 @@ YELLOW="\\e[33m"
27 BLACK="\\e[0m" 27 BLACK="\\e[0m"
28 POS="\\e[60G" 28 POS="\\e[60G"
29 29
30 -ok_msg(){ 30 +ok_msg() {
31 echo -e "${1}${POS}${BLACK}[${GREEN} OK ${BLACK}]" 31 echo -e "${1}${POS}${BLACK}[${GREEN} OK ${BLACK}]"
32 } 32 }
33 33
34 -failed_msg(){ 34 +failed_msg() {
35 echo -e "${1}${POS}${BLACK}[${RED}FAILED${BLACK}]" 35 echo -e "${1}${POS}${BLACK}[${RED}FAILED${BLACK}]"
36 } 36 }
37 37
@@ -43,14 +43,14 @@ failed_msg(){ @@ -43,14 +43,14 @@ failed_msg(){
43 # @set variable $error_msg if error. 43 # @set variable $error_msg if error.
44 # @set variable $srs_pid_file to pid file. 44 # @set variable $srs_pid_file to pid file.
45 load_process_info() { 45 load_process_info() {
  46 + # get pid file
46 srs_pid_file=`cat ${ROOT}/${CONFIG} |grep ^pid|awk '{print $2}'|awk -F ';' '{print $1}'` 47 srs_pid_file=`cat ${ROOT}/${CONFIG} |grep ^pid|awk '{print $2}'|awk -F ';' '{print $1}'`
47 if [[ -z $srs_pid_file ]]; then srs_pid_file=${DEFAULT_PID_FILE}; fi 48 if [[ -z $srs_pid_file ]]; then srs_pid_file=${DEFAULT_PID_FILE}; fi
  49 + # get abs path
  50 + srs_pid_dir=`dirname $srs_pid_file`
  51 + srs_pid_file=`(cd ${ROOT}; cd $srs_pid_dir; pwd)`/`basename $srs_pid_file`
48 52
49 - if [[ -f $srs_pid_file ]]; then  
50 - srs_pid=`cat $srs_pid_file 2>/dev/null`  
51 - else  
52 - srs_pid=`cat ${ROOT}/$srs_pid_file 2>/dev/null`  
53 - fi 53 + srs_pid=`cat $srs_pid_file 2>/dev/null`
54 ret=$?; if [[ 0 -ne $ret ]]; then error_msg="file $srs_pid_file does not exists"; return 1; fi 54 ret=$?; if [[ 0 -ne $ret ]]; then error_msg="file $srs_pid_file does not exists"; return 1; fi
55 55
56 ps -p ${srs_pid} >/dev/null 2>/dev/null 56 ps -p ${srs_pid} >/dev/null 2>/dev/null
@@ -69,21 +69,24 @@ start() { @@ -69,21 +69,24 @@ start() {
69 69
70 # get log file 70 # get log file
71 srs_log_file=`cat ${ROOT}/${CONFIG} |grep '^srs_log_file'| awk '{print $2}'| awk -F ';' '{print $1}'` 71 srs_log_file=`cat ${ROOT}/${CONFIG} |grep '^srs_log_file'| awk '{print $2}'| awk -F ';' '{print $1}'`
  72 + if [[ -z $srs_log_file ]]; then srs_log_file=${DEFAULT_LOG_FILE}; fi
  73 + # get abs path
  74 + srs_log_dir=`dirname $srs_log_file`
  75 + srs_log_file=`(cd ${ROOT}; cd $srs_log_dir; pwd)`/`basename $srs_log_file`
72 76
73 # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000" 77 # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
74 if [[ -z $srs_log_file ]]; then 78 if [[ -z $srs_log_file ]]; then
75 - (cd ${ROOT}; ${APP} -c ${CONFIG} >/dev/null 2>&1) 79 + (cd ${ROOT}; ${ROOT}/${APP} -c ${CONFIG} >/dev/null 2>&1)
76 else 80 else
77 - (cd ${ROOT}; ${APP} -c ${CONFIG} >> $srs_log_file 2>&1) 81 + (cd ${ROOT}; ${ROOT}/${APP} -c ${CONFIG} >> $srs_log_file 2>&1)
78 fi 82 fi
79 83
80 # check again after start server 84 # check again after start server
81 - sleep 1  
82 for ((i = 0; i < 5; i++)); do 85 for ((i = 0; i < 5; i++)); do
83 # sleep a little while, for srs may start then crash. 86 # sleep a little while, for srs may start then crash.
84 sleep 0.1 87 sleep 0.1
85 load_process_info 88 load_process_info
86 - ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "SRS start failed"; return $ret; fi 89 + ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "SRS start failed, see $srs_log_file"; return $ret; fi
87 done 90 done
88 91
89 # check whether started. 92 # check whether started.
@@ -308,7 +308,7 @@ int SrsConfDirective::read_token(SrsFileBuffer* buffer, std::vector<string>& arg @@ -308,7 +308,7 @@ int SrsConfDirective::read_token(SrsFileBuffer* buffer, std::vector<string>& arg
308 srs_error("line %d: unexpected end of file, expecting ; or \"}\"", buffer->line); 308 srs_error("line %d: unexpected end of file, expecting ; or \"}\"", buffer->line);
309 return ERROR_SYSTEM_CONFIG_INVALID; 309 return ERROR_SYSTEM_CONFIG_INVALID;
310 } 310 }
311 - srs_trace("end of file. ret=%d", ret); 311 + srs_trace("config parsed EOF");
312 312
313 return ret; 313 return ret;
314 } 314 }
@@ -684,7 +684,13 @@ int SrsConfig::parse_file(const char* filename) @@ -684,7 +684,13 @@ 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 + // check log
687 std::string log_filename = this->get_srs_log_file(); 688 std::string log_filename = this->get_srs_log_file();
  689 + if (this->get_srs_log_tank_file() && log_filename.empty()) {
  690 + ret = ERROR_SYSTEM_CONFIG_INVALID;
  691 + srs_error("must specifies the file to write log to. ret=%d", ret);
  692 + return ret;
  693 + }
688 if (!log_filename.empty()) { 694 if (!log_filename.empty()) {
689 srs_trace("log file is %s", log_filename.c_str()); 695 srs_trace("log file is %s", log_filename.c_str());
690 } 696 }
@@ -1282,6 +1288,18 @@ string SrsConfig::get_srs_log_file() @@ -1282,6 +1288,18 @@ string SrsConfig::get_srs_log_file()
1282 return conf->arg0(); 1288 return conf->arg0();
1283 } 1289 }
1284 1290
  1291 +bool SrsConfig::get_srs_log_tank_file()
  1292 +{
  1293 + srs_assert(root);
  1294 +
  1295 + SrsConfDirective* conf = root->get("srs_log_tank");
  1296 + if (conf && conf->arg0() == "file") {
  1297 + return true;
  1298 + }
  1299 +
  1300 + return false;
  1301 +}
  1302 +
1285 bool SrsConfig::get_deamon() 1303 bool SrsConfig::get_deamon()
1286 { 1304 {
1287 srs_assert(root); 1305 srs_assert(root);
@@ -148,6 +148,7 @@ public: @@ -148,6 +148,7 @@ public:
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_ffmpeg_log_dir(); 150 virtual std::string get_ffmpeg_log_dir();
  151 + virtual bool get_srs_log_tank_file();
151 virtual std::string get_srs_log_file(); 152 virtual std::string get_srs_log_file();
152 virtual bool get_deamon(); 153 virtual bool get_deamon();
153 virtual int get_max_connections(); 154 virtual int get_max_connections();
@@ -42,7 +42,7 @@ SrsThreadContext::~SrsThreadContext() @@ -42,7 +42,7 @@ SrsThreadContext::~SrsThreadContext()
42 42
43 void SrsThreadContext::generate_id() 43 void SrsThreadContext::generate_id()
44 { 44 {
45 - static int id = 1; 45 + static int id = 100;
46 cache[st_thread_self()] = id++; 46 cache[st_thread_self()] = id++;
47 } 47 }
48 48
@@ -230,20 +230,22 @@ void SrsFastLog::write_log(char *str_log, int size, int _level) @@ -230,20 +230,22 @@ void SrsFastLog::write_log(char *str_log, int size, int _level)
230 log_data[size++] = LOG_TAIL; 230 log_data[size++] = LOG_TAIL;
231 log_data[size++] = 0; 231 log_data[size++] = 0;
232 232
233 - // if is error msg, then print color msg.  
234 - // \033[31m : red text code in shell  
235 - // \033[32m : green text code in shell  
236 - // \033[33m : yellow text code in shell  
237 - // \033[0m : normal text code  
238 - if (_level <= SrsLogLevel::Trace) {  
239 - printf("%s", str_log);  
240 - } else if (_level == SrsLogLevel::Warn) {  
241 - printf("\033[33m%s\033[0m", str_log);  
242 - } else{  
243 - printf("\033[31m%s\033[0m", str_log); 233 + if (fd < 0 || !_srs_config->get_srs_log_tank_file()) {
  234 + // if is error msg, then print color msg.
  235 + // \033[31m : red text code in shell
  236 + // \033[32m : green text code in shell
  237 + // \033[33m : yellow text code in shell
  238 + // \033[0m : normal text code
  239 + if (_level <= SrsLogLevel::Trace) {
  240 + printf("%s", str_log);
  241 + } else if (_level == SrsLogLevel::Warn) {
  242 + printf("\033[33m%s\033[0m", str_log);
  243 + } else{
  244 + printf("\033[31m%s\033[0m", str_log);
  245 + }
244 } 246 }
245 -  
246 - // open log file. 247 +
  248 + // open log file. if specified
247 if (!_srs_config->get_srs_log_file().empty() && fd < 0) { 249 if (!_srs_config->get_srs_log_file().empty() && fd < 0) {
248 std::string filename = _srs_config->get_srs_log_file(); 250 std::string filename = _srs_config->get_srs_log_file();
249 251
@@ -256,8 +258,9 @@ void SrsFastLog::write_log(char *str_log, int size, int _level) @@ -256,8 +258,9 @@ void SrsFastLog::write_log(char *str_log, int size, int _level)
256 ); 258 );
257 } 259 }
258 } 260 }
  261 +
259 // write log to file. 262 // write log to file.
260 - if (fd > 0) { 263 + if (fd > 0 && _srs_config->get_srs_log_tank_file()) {
261 ::write(fd, str_log, size); 264 ::write(fd, str_log, size);
262 } 265 }
263 } 266 }
@@ -179,26 +179,6 @@ SrsServer::~SrsServer() @@ -179,26 +179,6 @@ SrsServer::~SrsServer()
179 int SrsServer::initialize() 179 int SrsServer::initialize()
180 { 180 {
181 int ret = ERROR_SUCCESS; 181 int ret = ERROR_SUCCESS;
182 -  
183 - // use linux epoll.  
184 - if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) {  
185 - ret = ERROR_ST_SET_EPOLL;  
186 - srs_error("st_set_eventsys use linux epoll failed. ret=%d", ret);  
187 - return ret;  
188 - }  
189 - srs_verbose("st_set_eventsys use linux epoll success");  
190 -  
191 - if(st_init() != 0){  
192 - ret = ERROR_ST_INITIALIZE;  
193 - srs_error("st_init failed. ret=%d", ret);  
194 - return ret;  
195 - }  
196 - srs_verbose("st_init success");  
197 -  
198 - // set current log id.  
199 - _srs_context->generate_id();  
200 - srs_info("log set id success");  
201 -  
202 return ret; 182 return ret;
203 } 183 }
204 184
@@ -277,6 +257,32 @@ int SrsServer::acquire_pid_file() @@ -277,6 +257,32 @@ int SrsServer::acquire_pid_file()
277 return ret; 257 return ret;
278 } 258 }
279 259
  260 +int SrsServer::initialize_st()
  261 +{
  262 + int ret = ERROR_SUCCESS;
  263 +
  264 + // use linux epoll.
  265 + if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) {
  266 + ret = ERROR_ST_SET_EPOLL;
  267 + srs_error("st_set_eventsys use linux epoll failed. ret=%d", ret);
  268 + return ret;
  269 + }
  270 + srs_verbose("st_set_eventsys use linux epoll success");
  271 +
  272 + if(st_init() != 0){
  273 + ret = ERROR_ST_INITIALIZE;
  274 + srs_error("st_init failed. ret=%d", ret);
  275 + return ret;
  276 + }
  277 + srs_verbose("st_init success");
  278 +
  279 + // set current log id.
  280 + _srs_context->generate_id();
  281 + srs_info("log set id success");
  282 +
  283 + return ret;
  284 +}
  285 +
280 int SrsServer::listen() 286 int SrsServer::listen()
281 { 287 {
282 int ret = ERROR_SUCCESS; 288 int ret = ERROR_SUCCESS;
@@ -80,6 +80,7 @@ public: @@ -80,6 +80,7 @@ public:
80 public: 80 public:
81 virtual int initialize(); 81 virtual int initialize();
82 virtual int acquire_pid_file(); 82 virtual int acquire_pid_file();
  83 + virtual int initialize_st();
83 virtual int listen(); 84 virtual int listen();
84 virtual int cycle(); 85 virtual int cycle();
85 virtual void remove(SrsConnection* conn); 86 virtual void remove(SrsConnection* conn);
@@ -60,18 +60,8 @@ int run_master() @@ -60,18 +60,8 @@ int run_master()
60 signal(SIGNAL_RELOAD, handler); 60 signal(SIGNAL_RELOAD, handler);
61 signal(SIGTERM, handler); 61 signal(SIGTERM, handler);
62 signal(SIGINT, handler); 62 signal(SIGINT, handler);
63 -  
64 - srs_trace("uname: "SRS_UNAME);  
65 - srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");  
66 - srs_trace("configure: "SRS_CONFIGURE);  
67 -  
68 - if ((ret = _srs_server->initialize()) != ERROR_SUCCESS) {  
69 - return ret;  
70 - }  
71 -  
72 - // TODO: create log dir in _srs_config->get_log_dir()  
73 63
74 - if ((ret = _srs_server->acquire_pid_file()) != ERROR_SUCCESS) { 64 + if ((ret = _srs_server->initialize_st()) != ERROR_SUCCESS) {
75 return ret; 65 return ret;
76 } 66 }
77 67
@@ -137,6 +127,8 @@ int main(int argc, char** argv) @@ -137,6 +127,8 @@ int main(int argc, char** argv)
137 { 127 {
138 int ret = ERROR_SUCCESS; 128 int ret = ERROR_SUCCESS;
139 129
  130 + srs_trace("srs(simple-rtmp-server)");
  131 +
140 // TODO: support both little and big endian. 132 // TODO: support both little and big endian.
141 srs_assert(srs_is_little_endian()); 133 srs_assert(srs_is_little_endian());
142 134
@@ -160,6 +152,18 @@ int main(int argc, char** argv) @@ -160,6 +152,18 @@ int main(int argc, char** argv)
160 if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) { 152 if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) {
161 return ret; 153 return ret;
162 } 154 }
  155 +
  156 + srs_trace("uname: "SRS_UNAME);
  157 + srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");
  158 + srs_trace("configure: "SRS_CONFIGURE);
  159 +
  160 + if ((ret = _srs_server->initialize()) != ERROR_SUCCESS) {
  161 + return ret;
  162 + }
  163 +
  164 + if ((ret = _srs_server->acquire_pid_file()) != ERROR_SUCCESS) {
  165 + return ret;
  166 + }
163 167
164 return run(); 168 return run();
165 } 169 }