1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

add log level in conf. change to 0.9.45

This commit is contained in:
winlin 2014-04-03 18:32:51 +08:00
parent 4984631cd6
commit 3f13726544
12 changed files with 103 additions and 43 deletions

View file

@ -26,6 +26,10 @@ ff_log_dir ./objs/logs;
# if file, write log to file. requires srs_log_file if log to file. # if file, write log to file. requires srs_log_file if log to file.
# default: file. # default: file.
srs_log_tank file; srs_log_tank file;
# the log level, for all log tanks.
# can be: verbose, info, trace, warn, error
# defualt: trace
srs_log_level trace;
# when srs_log_tank is file, specifies the log file. # when srs_log_tank is file, specifies the log file.
# default: ./objs/srs.log # default: ./objs/srs.log
srs_log_file ./objs/srs.log; srs_log_file ./objs/srs.log;

View file

@ -1288,6 +1288,18 @@ string SrsConfig::get_srs_log_file()
return conf->arg0(); return conf->arg0();
} }
string SrsConfig::get_srs_log_level()
{
srs_assert(root);
SrsConfDirective* conf = root->get("srs_log_level");
if (!conf || conf->arg0().empty()) {
return "trace";
}
return conf->arg0();
}
bool SrsConfig::get_srs_log_tank_file() bool SrsConfig::get_srs_log_tank_file()
{ {
srs_assert(root); srs_assert(root);

View file

@ -149,6 +149,7 @@ public:
virtual std::string get_engine_output(SrsConfDirective* engine); virtual std::string get_engine_output(SrsConfDirective* engine);
virtual std::string get_ffmpeg_log_dir(); virtual std::string get_ffmpeg_log_dir();
virtual bool get_srs_log_tank_file(); virtual bool get_srs_log_tank_file();
virtual std::string get_srs_log_level();
virtual std::string get_srs_log_file(); virtual std::string get_srs_log_file();
virtual bool get_deamon(); virtual bool get_deamon();
virtual int get_max_connections(); virtual int get_max_connections();

View file

@ -587,8 +587,8 @@ int SrsHlsMuxer::segment_open(int64_t segment_start_dts)
srs_error("open hls muxer failed. ret=%d", ret); srs_error("open hls muxer failed. ret=%d", ret);
return ret; return ret;
} }
srs_info("open HLS muxer success. vhost=%s, path=%s, tmp=%s", srs_info("open HLS muxer success. path=%s, tmp=%s",
vhost.c_str(), current->full_path.c_str(), tmp_file.c_str()); current->full_path.c_str(), tmp_file.c_str());
return ret; return ret;
} }

View file

@ -670,7 +670,7 @@ int SrsHttpUri::initialize(std::string _url)
srs_info("parse url %s success", purl); srs_info("parse url %s success", purl);
query = get_uri_field(url, &hp_u, UF_QUERY); query = get_uri_field(url, &hp_u, UF_QUERY);
srs_info("parse query %s success", query); srs_info("parse query %s success", query.c_str());
return ret; return ret;
} }

View file

@ -61,10 +61,12 @@ int SrsThreadContext::get_id()
SrsFastLog::SrsFastLog() SrsFastLog::SrsFastLog()
{ {
level = SrsLogLevel::Trace; _level = SrsLogLevel::Trace;
log_data = new char[LOG_MAX_SIZE]; log_data = new char[LOG_MAX_SIZE];
fd = -1; fd = -1;
// TODO: support reload.
} }
SrsFastLog::~SrsFastLog() SrsFastLog::~SrsFastLog()
@ -77,9 +79,19 @@ SrsFastLog::~SrsFastLog()
} }
} }
int SrsFastLog::level()
{
return _level;
}
void SrsFastLog::set_level(int level)
{
_level = level;
}
void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...) void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevel::Verbose) { if (_level > SrsLogLevel::Verbose) {
return; return;
} }
@ -94,12 +106,12 @@ void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
write_log(log_data, size, SrsLogLevel::Verbose); write_log(fd, log_data, size, SrsLogLevel::Verbose);
} }
void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...) void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevel::Info) { if (_level > SrsLogLevel::Info) {
return; return;
} }
@ -114,12 +126,12 @@ void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
write_log(log_data, size, SrsLogLevel::Info); write_log(fd, log_data, size, SrsLogLevel::Info);
} }
void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...) void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevel::Trace) { if (_level > SrsLogLevel::Trace) {
return; return;
} }
@ -134,12 +146,12 @@ void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
write_log(log_data, size, SrsLogLevel::Trace); write_log(fd, log_data, size, SrsLogLevel::Trace);
} }
void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...) void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevel::Warn) { if (_level > SrsLogLevel::Warn) {
return; return;
} }
@ -154,12 +166,12 @@ void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
write_log(log_data, size, SrsLogLevel::Warn); write_log(fd, log_data, size, SrsLogLevel::Warn);
} }
void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...) void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevel::Error) { if (_level > SrsLogLevel::Error) {
return; return;
} }
@ -177,7 +189,7 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
// add strerror() to error msg. // add strerror() to error msg.
size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno)); size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno));
write_log(log_data, size, SrsLogLevel::Error); write_log(fd, log_data, size, SrsLogLevel::Error);
} }
bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size) bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size)
@ -219,7 +231,7 @@ bool SrsFastLog::generate_header(const char* tag, int context_id, const char* le
return true; return true;
} }
void SrsFastLog::write_log(char *str_log, int size, int _level) void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
{ {
// ensure the tail and EOF of string // ensure the tail and EOF of string
// LOG_TAIL_SIZE for the TAIL char. // LOG_TAIL_SIZE for the TAIL char.
@ -227,8 +239,8 @@ void SrsFastLog::write_log(char *str_log, int size, int _level)
size = srs_min(LOG_MAX_SIZE - 1 - LOG_TAIL_SIZE, size); size = srs_min(LOG_MAX_SIZE - 1 - LOG_TAIL_SIZE, size);
// add some to the end of char. // add some to the end of char.
log_data[size++] = LOG_TAIL; str_log[size++] = LOG_TAIL;
log_data[size++] = 0; str_log[size++] = 0;
if (fd < 0 || !_srs_config->get_srs_log_tank_file()) { if (fd < 0 || !_srs_config->get_srs_log_tank_file()) {
// if is error msg, then print color msg. // if is error msg, then print color msg.
@ -236,9 +248,9 @@ void SrsFastLog::write_log(char *str_log, int size, int _level)
// \033[32m : green text code in shell // \033[32m : green text code in shell
// \033[33m : yellow text code in shell // \033[33m : yellow text code in shell
// \033[0m : normal text code // \033[0m : normal text code
if (_level <= SrsLogLevel::Trace) { if (level <= SrsLogLevel::Trace) {
printf("%s", str_log); printf("%s", str_log);
} else if (_level == SrsLogLevel::Warn) { } else if (level == SrsLogLevel::Warn) {
printf("\033[33m%s\033[0m", str_log); printf("\033[33m%s\033[0m", str_log);
} else{ } else{
printf("\033[31m%s\033[0m", str_log); printf("\033[31m%s\033[0m", str_log);

View file

@ -54,23 +54,6 @@ public:
virtual int get_id(); virtual int get_id();
}; };
/**
* the log level, for example:
* if specified Debug level, all level messages will be logged.
* if specified Warn level, only Warn/Error/Fatal level messages will be logged.
*/
class SrsLogLevel
{
public:
// only used for very verbose debug, generally,
// we compile without this level for high performance.
static const int Verbose = 0x01;
static const int Info = 0x02;
static const int Trace = 0x03;
static const int Warn = 0x04;
static const int Error = 0x05;
};
/** /**
* we use memory/disk cache and donot flush when write log. * we use memory/disk cache and donot flush when write log.
*/ */
@ -78,7 +61,7 @@ class SrsFastLog : public ISrsLog
{ {
private: private:
// defined in SrsLogLevel. // defined in SrsLogLevel.
int level; int _level;
char* log_data; char* log_data;
// log to file if specified srs_log_file // log to file if specified srs_log_file
int fd; int fd;
@ -86,6 +69,8 @@ public:
SrsFastLog(); SrsFastLog();
virtual ~SrsFastLog(); virtual ~SrsFastLog();
public: public:
virtual int level();
virtual void set_level(int level);
virtual void verbose(const char* tag, int context_id, const char* fmt, ...); virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
virtual void info(const char* tag, int context_id, const char* fmt, ...); virtual void info(const char* tag, int context_id, const char* fmt, ...);
virtual void trace(const char* tag, int context_id, const char* fmt, ...); virtual void trace(const char* tag, int context_id, const char* fmt, ...);
@ -93,7 +78,7 @@ public:
virtual void error(const char* tag, int context_id, const char* fmt, ...); virtual void error(const char* tag, int context_id, const char* fmt, ...);
private: private:
virtual bool generate_header(const char* tag, int context_id, const char* level_name, int* header_size); virtual bool generate_header(const char* tag, int context_id, const char* level_name, int* header_size);
virtual void write_log(char* str_log, int size, int _level); static void write_log(int& fd, char* str_log, int size, int level);
}; };
#endif #endif

View file

@ -149,8 +149,6 @@ int SrsListener::cycle()
return ret; return ret;
} }
srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
return ret; return ret;
} }
@ -493,13 +491,15 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
// directly enqueue, the cycle thread will remove the client. // directly enqueue, the cycle thread will remove the client.
conns.push_back(conn); conns.push_back(conn);
srs_verbose("add conn from port %d to vector. conns=%d", port, (int)conns.size()); srs_verbose("add conn to vector.");
// cycle will start process thread and when finished remove the client. // cycle will start process thread and when finished remove the client.
if ((ret = conn->start()) != ERROR_SUCCESS) { if ((ret = conn->start()) != ERROR_SUCCESS) {
return ret; return ret;
} }
srs_verbose("conn start finished. ret=%d", ret); srs_verbose("conn started success .");
srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
return ret; return ret;
} }

View file

@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// current release version // current release version
#define VERSION_MAJOR "0" #define VERSION_MAJOR "0"
#define VERSION_MINOR "9" #define VERSION_MINOR "9"
#define VERSION_REVISION "44" #define VERSION_REVISION "45"
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
// server info. // server info.
#define RTMP_SIG_SRS_KEY "srs" #define RTMP_SIG_SRS_KEY "srs"

View file

@ -31,6 +31,15 @@ ISrsLog::~ISrsLog()
{ {
} }
int ISrsLog::level()
{
return SrsLogLevel::Trace;
}
void ISrsLog::set_level(int /*level*/)
{
}
void ISrsLog::verbose(const char* /*tag*/, int /*context_id*/, const char* /*fmt*/, ...) void ISrsLog::verbose(const char* /*tag*/, int /*context_id*/, const char* /*fmt*/, ...)
{ {
} }

View file

@ -35,6 +35,23 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
/**
* the log level, for example:
* if specified Debug level, all level messages will be logged.
* if specified Warn level, only Warn/Error/Fatal level messages will be logged.
*/
class SrsLogLevel
{
public:
// only used for very verbose debug, generally,
// we compile without this level for high performance.
static const int Verbose = 0x01;
static const int Info = 0x02;
static const int Trace = 0x03;
static const int Warn = 0x04;
static const int Error = 0x05;
};
/** /**
* the log interface provides method to write log. * the log interface provides method to write log.
* but we provides some macro, which enable us to disable the log when compile. * but we provides some macro, which enable us to disable the log when compile.
@ -46,6 +63,11 @@ public:
ISrsLog(); ISrsLog();
virtual ~ISrsLog(); virtual ~ISrsLog();
public: public:
/**
* defined in SrsLogLevel.
*/
virtual int level();
virtual void set_level(int level);
/** /**
* log for verbose, very verbose information. * log for verbose, very verbose information.
*/ */

View file

@ -157,6 +157,21 @@ int main(int argc, char** argv)
return ret; return ret;
} }
// config parsed, initialize log.
if ("verbose" == _srs_config->get_srs_log_level()) {
_srs_log->set_level(SrsLogLevel::Verbose);
} else if ("info" == _srs_config->get_srs_log_level()) {
_srs_log->set_level(SrsLogLevel::Info);
} else if ("trace" == _srs_config->get_srs_log_level()) {
_srs_log->set_level(SrsLogLevel::Trace);
} else if ("warn" == _srs_config->get_srs_log_level()) {
_srs_log->set_level(SrsLogLevel::Warn);
} else if ("error" == _srs_config->get_srs_log_level()) {
_srs_log->set_level(SrsLogLevel::Error);
} else {
_srs_log->set_level(SrsLogLevel::Trace);
}
srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION); srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION);
srs_trace("uname: "SRS_UNAME); srs_trace("uname: "SRS_UNAME);
srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian"); srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");