winlin

add log level in conf. change to 0.9.45

... ... @@ -26,6 +26,10 @@ ff_log_dir ./objs/logs;
# if file, write log to file. requires srs_log_file if log to file.
# default: 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.
# default: ./objs/srs.log
srs_log_file ./objs/srs.log;
... ...
... ... @@ -1288,6 +1288,18 @@ string SrsConfig::get_srs_log_file()
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()
{
srs_assert(root);
... ...
... ... @@ -149,6 +149,7 @@ public:
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_level();
virtual std::string get_srs_log_file();
virtual bool get_deamon();
virtual int get_max_connections();
... ...
... ... @@ -587,8 +587,8 @@ int SrsHlsMuxer::segment_open(int64_t segment_start_dts)
srs_error("open hls muxer failed. ret=%d", ret);
return ret;
}
srs_info("open HLS muxer success. vhost=%s, path=%s, tmp=%s",
vhost.c_str(), current->full_path.c_str(), tmp_file.c_str());
srs_info("open HLS muxer success. path=%s, tmp=%s",
current->full_path.c_str(), tmp_file.c_str());
return ret;
}
... ...
... ... @@ -670,7 +670,7 @@ int SrsHttpUri::initialize(std::string _url)
srs_info("parse url %s success", purl);
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;
}
... ...
... ... @@ -61,10 +61,12 @@ int SrsThreadContext::get_id()
SrsFastLog::SrsFastLog()
{
level = SrsLogLevel::Trace;
_level = SrsLogLevel::Trace;
log_data = new char[LOG_MAX_SIZE];
fd = -1;
// TODO: support reload.
}
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, ...)
{
if (level > SrsLogLevel::Verbose) {
if (_level > SrsLogLevel::Verbose) {
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);
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, ...)
{
if (level > SrsLogLevel::Info) {
if (_level > SrsLogLevel::Info) {
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);
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, ...)
{
if (level > SrsLogLevel::Trace) {
if (_level > SrsLogLevel::Trace) {
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);
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, ...)
{
if (level > SrsLogLevel::Warn) {
if (_level > SrsLogLevel::Warn) {
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);
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, ...)
{
if (level > SrsLogLevel::Error) {
if (_level > SrsLogLevel::Error) {
return;
}
... ... @@ -177,7 +189,7 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
// add strerror() to error msg.
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)
... ... @@ -219,7 +231,7 @@ bool SrsFastLog::generate_header(const char* tag, int context_id, const char* le
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
// 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);
// add some to the end of char.
log_data[size++] = LOG_TAIL;
log_data[size++] = 0;
str_log[size++] = LOG_TAIL;
str_log[size++] = 0;
if (fd < 0 || !_srs_config->get_srs_log_tank_file()) {
// 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[33m : yellow text code in shell
// \033[0m : normal text code
if (_level <= SrsLogLevel::Trace) {
if (level <= SrsLogLevel::Trace) {
printf("%s", str_log);
} else if (_level == SrsLogLevel::Warn) {
} else if (level == SrsLogLevel::Warn) {
printf("\033[33m%s\033[0m", str_log);
} else{
printf("\033[31m%s\033[0m", str_log);
... ...
... ... @@ -55,30 +55,13 @@ public:
};
/**
* 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.
*/
class SrsFastLog : public ISrsLog
{
private:
// defined in SrsLogLevel.
int level;
int _level;
char* log_data;
// log to file if specified srs_log_file
int fd;
... ... @@ -86,6 +69,8 @@ public:
SrsFastLog();
virtual ~SrsFastLog();
public:
virtual int level();
virtual void set_level(int level);
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 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, ...);
private:
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
... ...
... ... @@ -149,8 +149,6 @@ int SrsListener::cycle()
return ret;
}
srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), 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.
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.
if ((ret = conn->start()) != ERROR_SUCCESS) {
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;
}
... ...
... ... @@ -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 "44"
#define VERSION_REVISION "45"
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
// server info.
#define RTMP_SIG_SRS_KEY "srs"
... ...
... ... @@ -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*/, ...)
{
}
... ...
... ... @@ -36,6 +36,23 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#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.
* but we provides some macro, which enable us to disable the log when compile.
* @see also SmtDebug/SmtTrace/SmtWarn/SmtError which is corresponding to Debug/Trace/Warn/Fatal.
... ... @@ -47,6 +64,11 @@ public:
virtual ~ISrsLog();
public:
/**
* defined in SrsLogLevel.
*/
virtual int level();
virtual void set_level(int level);
/**
* log for verbose, very verbose information.
*/
virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
... ...
... ... @@ -157,6 +157,21 @@ int main(int argc, char** argv)
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("uname: "SRS_UNAME);
srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");
... ...