mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
For #820, extract service module.
This commit is contained in:
parent
0a57a46016
commit
ff822b55cd
16 changed files with 510 additions and 213 deletions
|
@ -35,51 +35,6 @@
|
|||
#include <srs_app_utility.hpp>
|
||||
#include <srs_kernel_utility.hpp>
|
||||
|
||||
SrsThreadContext::SrsThreadContext()
|
||||
{
|
||||
}
|
||||
|
||||
SrsThreadContext::~SrsThreadContext()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsThreadContext::generate_id()
|
||||
{
|
||||
static int id = 100;
|
||||
|
||||
int gid = id++;
|
||||
cache[st_thread_self()] = gid;
|
||||
return gid;
|
||||
}
|
||||
|
||||
int SrsThreadContext::get_id()
|
||||
{
|
||||
return cache[st_thread_self()];
|
||||
}
|
||||
|
||||
int SrsThreadContext::set_id(int v)
|
||||
{
|
||||
st_thread_t self = st_thread_self();
|
||||
|
||||
int ov = 0;
|
||||
if (cache.find(self) != cache.end()) {
|
||||
ov = cache[self];
|
||||
}
|
||||
|
||||
cache[self] = v;
|
||||
|
||||
return ov;
|
||||
}
|
||||
|
||||
void SrsThreadContext::clear_cid()
|
||||
{
|
||||
st_thread_t self = st_thread_self();
|
||||
std::map<st_thread_t, int>::iterator it = cache.find(self);
|
||||
if (it != cache.end()) {
|
||||
cache.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
// the max size of a line of log.
|
||||
#define LOG_MAX_SIZE 4096
|
||||
|
||||
|
@ -90,7 +45,7 @@ void SrsThreadContext::clear_cid()
|
|||
|
||||
SrsFastLog::SrsFastLog()
|
||||
{
|
||||
_level = SrsLogLevel::Trace;
|
||||
level = SrsLogLevelTrace;
|
||||
log_data = new char[LOG_MAX_SIZE];
|
||||
|
||||
fd = -1;
|
||||
|
@ -120,7 +75,7 @@ int SrsFastLog::initialize()
|
|||
_srs_config->subscribe(this);
|
||||
|
||||
log_to_file_tank = _srs_config->get_log_tank_file();
|
||||
_level = srs_get_log_level(_srs_config->get_log_level());
|
||||
level = srs_get_log_level(_srs_config->get_log_level());
|
||||
utc = _srs_config->get_utc_time();
|
||||
}
|
||||
|
||||
|
@ -142,12 +97,12 @@ void SrsFastLog::reopen()
|
|||
|
||||
void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Verbose) {
|
||||
if (level > SrsLogLevelVerbose) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(false, tag, context_id, "Verb", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Verb", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -157,17 +112,17 @@ 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(fd, log_data, size, SrsLogLevel::Verbose);
|
||||
write_log(fd, log_data, size, SrsLogLevelVerbose);
|
||||
}
|
||||
|
||||
void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Info) {
|
||||
if (level > SrsLogLevelInfo) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(false, tag, context_id, "Debug", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Debug", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -177,17 +132,17 @@ 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(fd, log_data, size, SrsLogLevel::Info);
|
||||
write_log(fd, log_data, size, SrsLogLevelInfo);
|
||||
}
|
||||
|
||||
void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Trace) {
|
||||
if (level > SrsLogLevelTrace) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(false, tag, context_id, "Trace", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Trace", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -197,17 +152,17 @@ 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(fd, log_data, size, SrsLogLevel::Trace);
|
||||
write_log(fd, log_data, size, SrsLogLevelTrace);
|
||||
}
|
||||
|
||||
void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Warn) {
|
||||
if (level > SrsLogLevelWarn) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(true, tag, context_id, "Warn", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Warn", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -217,17 +172,17 @@ 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(fd, log_data, size, SrsLogLevel::Warn);
|
||||
write_log(fd, log_data, size, SrsLogLevelWarn);
|
||||
}
|
||||
|
||||
void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Error) {
|
||||
if (level > SrsLogLevelError) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(true, tag, context_id, "Error", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Error", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -242,7 +197,7 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
|
|||
size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno));
|
||||
}
|
||||
|
||||
write_log(fd, log_data, size, SrsLogLevel::Error);
|
||||
write_log(fd, log_data, size, SrsLogLevelError);
|
||||
}
|
||||
|
||||
int SrsFastLog::on_reload_utc_time()
|
||||
|
@ -287,7 +242,7 @@ int SrsFastLog::on_reload_log_level()
|
|||
return ret;
|
||||
}
|
||||
|
||||
_level = srs_get_log_level(_srs_config->get_log_level());
|
||||
level = srs_get_log_level(_srs_config->get_log_level());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -312,65 +267,6 @@ int SrsFastLog::on_reload_log_file()
|
|||
return ret;
|
||||
}
|
||||
|
||||
bool SrsFastLog::generate_header(bool error, const char* tag, int context_id, const char* level_name, int* header_size)
|
||||
{
|
||||
// clock time
|
||||
timeval tv;
|
||||
if (gettimeofday(&tv, NULL) == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// to calendar time
|
||||
struct tm* tm;
|
||||
if (utc) {
|
||||
if ((tm = gmtime(&tv.tv_sec)) == NULL) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ((tm = localtime(&tv.tv_sec)) == NULL) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// write log header
|
||||
int log_header_size = -1;
|
||||
|
||||
if (error) {
|
||||
if (tag) {
|
||||
log_header_size = snprintf(log_data, LOG_MAX_SIZE,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%d][%d] ",
|
||||
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
|
||||
level_name, tag, getpid(), context_id, errno);
|
||||
} else {
|
||||
log_header_size = snprintf(log_data, LOG_MAX_SIZE,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%d][%d] ",
|
||||
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
|
||||
level_name, getpid(), context_id, errno);
|
||||
}
|
||||
} else {
|
||||
if (tag) {
|
||||
log_header_size = snprintf(log_data, LOG_MAX_SIZE,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%d] ",
|
||||
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
|
||||
level_name, tag, getpid(), context_id);
|
||||
} else {
|
||||
log_header_size = snprintf(log_data, LOG_MAX_SIZE,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%d] ",
|
||||
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
|
||||
level_name, getpid(), context_id);
|
||||
}
|
||||
}
|
||||
|
||||
if (log_header_size == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// write the header size.
|
||||
*header_size = srs_min(LOG_MAX_SIZE - 1, log_header_size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
|
||||
{
|
||||
// ensure the tail and EOF of string
|
||||
|
@ -388,9 +284,9 @@ void SrsFastLog::write_log(int& fd, 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 <= SrsLogLevelTrace) {
|
||||
printf("%.*s", size, str_log);
|
||||
} else if (level == SrsLogLevel::Warn) {
|
||||
} else if (level == SrsLogLevelWarn) {
|
||||
printf("\033[33m%.*s\033[0m", size, str_log);
|
||||
} else{
|
||||
printf("\033[31m%.*s\033[0m", size, str_log);
|
||||
|
|
|
@ -26,33 +26,11 @@
|
|||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <srs_app_st.hpp>
|
||||
#include <srs_kernel_log.hpp>
|
||||
#include <srs_app_reload.hpp>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
/**
|
||||
* st thread context, get_id will get the st-thread id,
|
||||
* which identify the client.
|
||||
*/
|
||||
class SrsThreadContext : public ISrsThreadContext
|
||||
{
|
||||
private:
|
||||
std::map<st_thread_t, int> cache;
|
||||
public:
|
||||
SrsThreadContext();
|
||||
virtual ~SrsThreadContext();
|
||||
public:
|
||||
virtual int generate_id();
|
||||
virtual int get_id();
|
||||
virtual int set_id(int v);
|
||||
public:
|
||||
virtual void clear_cid();
|
||||
};
|
||||
#include <srs_app_reload.hpp>
|
||||
#include <srs_service_log.hpp>
|
||||
|
||||
/**
|
||||
* we use memory/disk cache and donot flush when write log.
|
||||
|
@ -61,10 +39,10 @@ public:
|
|||
*/
|
||||
class SrsFastLog : public ISrsLog, public ISrsReloadHandler
|
||||
{
|
||||
// for utest to override
|
||||
// for utest to override
|
||||
protected:
|
||||
// defined in SrsLogLevel.
|
||||
int _level;
|
||||
SrsLogLevel level;
|
||||
private:
|
||||
char* log_data;
|
||||
// log to file if specified srs_log_file
|
||||
|
@ -76,6 +54,7 @@ private:
|
|||
public:
|
||||
SrsFastLog();
|
||||
virtual ~SrsFastLog();
|
||||
// interface ISrsLog
|
||||
public:
|
||||
virtual int initialize();
|
||||
virtual void reopen();
|
||||
|
@ -91,7 +70,6 @@ public:
|
|||
virtual int on_reload_log_level();
|
||||
virtual int on_reload_log_file();
|
||||
private:
|
||||
virtual bool generate_header(bool error, const char* tag, int context_id, const char* level_name, int* header_size);
|
||||
virtual void write_log(int& fd, char* str_log, int size, int level);
|
||||
virtual void open_log_file();
|
||||
};
|
||||
|
|
|
@ -28,9 +28,7 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include <st.h>
|
||||
|
||||
#include <srs_app_st.hpp>
|
||||
#include <srs_service_st.hpp>
|
||||
#include <srs_protocol_io.hpp>
|
||||
|
||||
// the internal classes, user should never use it.
|
||||
|
|
|
@ -110,20 +110,20 @@ failed:
|
|||
return ret;
|
||||
}
|
||||
|
||||
int srs_get_log_level(string level)
|
||||
SrsLogLevel srs_get_log_level(string level)
|
||||
{
|
||||
if ("verbose" == level) {
|
||||
return SrsLogLevel::Verbose;
|
||||
return SrsLogLevelVerbose;
|
||||
} else if ("info" == level) {
|
||||
return SrsLogLevel::Info;
|
||||
return SrsLogLevelInfo;
|
||||
} else if ("trace" == level) {
|
||||
return SrsLogLevel::Trace;
|
||||
return SrsLogLevelTrace;
|
||||
} else if ("warn" == level) {
|
||||
return SrsLogLevel::Warn;
|
||||
return SrsLogLevelWarn;
|
||||
} else if ("error" == level) {
|
||||
return SrsLogLevel::Error;
|
||||
return SrsLogLevelError;
|
||||
} else {
|
||||
return SrsLogLevel::Disabled;
|
||||
return SrsLogLevelDisabled;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include <sys/resource.h>
|
||||
|
||||
#include <srs_app_st.hpp>
|
||||
#include <srs_kernel_log.hpp>
|
||||
|
||||
class SrsKbps;
|
||||
class SrsBuffer;
|
||||
|
@ -47,7 +48,7 @@ extern int srs_socket_connect(std::string server, int port, int64_t tm, st_netfd
|
|||
* convert level in string to log level in int.
|
||||
* @return the log level defined in SrsLogLevel.
|
||||
*/
|
||||
extern int srs_get_log_level(std::string level);
|
||||
extern SrsLogLevel srs_get_log_level(std::string level);
|
||||
|
||||
/**
|
||||
* build the path according to vhost/app/stream, where replace variables:
|
||||
|
|
|
@ -38,18 +38,17 @@
|
|||
* 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
|
||||
enum SrsLogLevel
|
||||
{
|
||||
public:
|
||||
SrsLogLevelForbidden = 0x00,
|
||||
// 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;
|
||||
// specified the disabled level, no log, for utest.
|
||||
static const int Disabled = 0x06;
|
||||
SrsLogLevelVerbose = 0x01,
|
||||
SrsLogLevelInfo = 0x02,
|
||||
SrsLogLevelTrace = 0x04,
|
||||
SrsLogLevelWarn = 0x08,
|
||||
SrsLogLevelError = 0x10,
|
||||
SrsLogLevelDisabled = 0x20,
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -32,7 +32,7 @@ using namespace std;
|
|||
#include <srs_kernel_error.hpp>
|
||||
#include <srs_app_server.hpp>
|
||||
#include <srs_app_config.hpp>
|
||||
#include <srs_app_log.hpp>
|
||||
#include <srs_service_log.hpp>
|
||||
#include <srs_kernel_utility.hpp>
|
||||
#include <srs_rtmp_stack.hpp>
|
||||
#include <srs_kernel_stream.hpp>
|
||||
|
@ -54,8 +54,8 @@ using namespace std;
|
|||
int proxy_hls2rtmp(std::string hls, std::string rtmp);
|
||||
|
||||
// @global log and context.
|
||||
ISrsLog* _srs_log = new SrsFastLog();
|
||||
ISrsThreadContext* _srs_context = new ISrsThreadContext();
|
||||
ISrsLog* _srs_log = new SrsConsoleLog(SrsLogLevelTrace, false);
|
||||
ISrsThreadContext* _srs_context = new SrsThreadContext();
|
||||
// @global config object for app module.
|
||||
SrsConfig* _srs_config = NULL;
|
||||
|
||||
|
|
263
trunk/src/service/srs_service_log.cpp
Normal file
263
trunk/src/service/srs_service_log.cpp
Normal file
|
@ -0,0 +1,263 @@
|
|||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013-2017 OSSRS(winlin)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <srs_service_log.hpp>
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <sys/time.h>
|
||||
using namespace std;
|
||||
|
||||
#include <srs_kernel_error.hpp>
|
||||
#include <srs_kernel_utility.hpp>
|
||||
|
||||
#define SRS_BASIC_LOG_SIZE 1024
|
||||
|
||||
SrsThreadContext::SrsThreadContext()
|
||||
{
|
||||
}
|
||||
|
||||
SrsThreadContext::~SrsThreadContext()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsThreadContext::generate_id()
|
||||
{
|
||||
static int id = 100;
|
||||
|
||||
int gid = id++;
|
||||
cache[st_thread_self()] = gid;
|
||||
return gid;
|
||||
}
|
||||
|
||||
int SrsThreadContext::get_id()
|
||||
{
|
||||
return cache[st_thread_self()];
|
||||
}
|
||||
|
||||
int SrsThreadContext::set_id(int v)
|
||||
{
|
||||
st_thread_t self = st_thread_self();
|
||||
|
||||
int ov = 0;
|
||||
if (cache.find(self) != cache.end()) {
|
||||
ov = cache[self];
|
||||
}
|
||||
|
||||
cache[self] = v;
|
||||
|
||||
return ov;
|
||||
}
|
||||
|
||||
void SrsThreadContext::clear_cid()
|
||||
{
|
||||
st_thread_t self = st_thread_self();
|
||||
std::map<st_thread_t, int>::iterator it = cache.find(self);
|
||||
if (it != cache.end()) {
|
||||
cache.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
SrsConsoleLog::SrsConsoleLog(SrsLogLevel l, bool u)
|
||||
{
|
||||
level = l;
|
||||
utc = u;
|
||||
|
||||
buffer = new char[SRS_BASIC_LOG_SIZE];
|
||||
}
|
||||
|
||||
SrsConsoleLog::~SrsConsoleLog()
|
||||
{
|
||||
srs_freepa(buffer);
|
||||
}
|
||||
|
||||
int SrsConsoleLog::initialize()
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
void SrsConsoleLog::reopen()
|
||||
{
|
||||
}
|
||||
|
||||
void SrsConsoleLog::verbose(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (level > SrsLogLevelVerbose) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, false, tag, context_id, "Verb", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
// we reserved 1 bytes for the new line.
|
||||
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
fprintf(stdout, "%s", buffer);
|
||||
}
|
||||
|
||||
void SrsConsoleLog::info(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (level > SrsLogLevelInfo) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, false, tag, context_id, "Debug", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
// we reserved 1 bytes for the new line.
|
||||
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
fprintf(stdout, "%s", buffer);
|
||||
}
|
||||
|
||||
void SrsConsoleLog::trace(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (level > SrsLogLevelTrace) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, false, tag, context_id, "Trace", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
// we reserved 1 bytes for the new line.
|
||||
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
fprintf(stdout, "%s", buffer);
|
||||
}
|
||||
|
||||
void SrsConsoleLog::warn(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (level > SrsLogLevelWarn) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, true, tag, context_id, "Warn", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
// we reserved 1 bytes for the new line.
|
||||
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
fprintf(stderr, "%s", buffer);
|
||||
}
|
||||
|
||||
void SrsConsoleLog::error(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (level > SrsLogLevelError) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, true, tag, context_id, "Error", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
// we reserved 1 bytes for the new line.
|
||||
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
// add strerror() to error msg.
|
||||
if (errno != 0) {
|
||||
size += snprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, "(%s)", strerror(errno));
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s", buffer);
|
||||
}
|
||||
|
||||
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, int cid, const char* level, int* psize)
|
||||
{
|
||||
// clock time
|
||||
timeval tv;
|
||||
if (gettimeofday(&tv, NULL) == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// to calendar time
|
||||
struct tm* tm;
|
||||
if (utc) {
|
||||
if ((tm = gmtime(&tv.tv_sec)) == NULL) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ((tm = localtime(&tv.tv_sec)) == NULL) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int nb_header = -1;
|
||||
if (dangerous) {
|
||||
if (tag) {
|
||||
nb_header = snprintf(buffer, size,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%d][%d] ",
|
||||
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
|
||||
level, tag, getpid(), cid, errno);
|
||||
} else {
|
||||
nb_header = snprintf(buffer, size,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%d][%d] ",
|
||||
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
|
||||
level, getpid(), cid, errno);
|
||||
}
|
||||
} else {
|
||||
if (tag) {
|
||||
nb_header = snprintf(buffer, size,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%d] ",
|
||||
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
|
||||
level, tag, getpid(), cid);
|
||||
} else {
|
||||
nb_header = snprintf(buffer, size,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%d] ",
|
||||
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
|
||||
level, getpid(), cid);
|
||||
}
|
||||
}
|
||||
|
||||
if (nb_header == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// write the header size.
|
||||
*psize = srs_min(size - 1, nb_header);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
85
trunk/src/service/srs_service_log.hpp
Normal file
85
trunk/src/service/srs_service_log.hpp
Normal file
|
@ -0,0 +1,85 @@
|
|||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013-2017 OSSRS(winlin)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRS_SERVICE_LOG_HPP
|
||||
#define SRS_SERVICE_LOG_HPP
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <srs_service_st.hpp>
|
||||
#include <srs_kernel_log.hpp>
|
||||
|
||||
/**
|
||||
* st thread context, get_id will get the st-thread id,
|
||||
* which identify the client.
|
||||
*/
|
||||
class SrsThreadContext : public ISrsThreadContext
|
||||
{
|
||||
private:
|
||||
std::map<st_thread_t, int> cache;
|
||||
public:
|
||||
SrsThreadContext();
|
||||
virtual ~SrsThreadContext();
|
||||
public:
|
||||
virtual int generate_id();
|
||||
virtual int get_id();
|
||||
virtual int set_id(int v);
|
||||
public:
|
||||
virtual void clear_cid();
|
||||
};
|
||||
|
||||
/**
|
||||
* The basic console log, which write log to console.
|
||||
*/
|
||||
class SrsConsoleLog : public ISrsLog
|
||||
{
|
||||
private:
|
||||
SrsLogLevel level;
|
||||
bool utc;
|
||||
private:
|
||||
char* buffer;
|
||||
public:
|
||||
SrsConsoleLog(SrsLogLevel l, bool u);
|
||||
virtual ~SrsConsoleLog();
|
||||
// interface ISrsLog
|
||||
public:
|
||||
virtual int initialize();
|
||||
virtual void reopen();
|
||||
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, ...);
|
||||
virtual void warn(const char* tag, int context_id, const char* fmt, ...);
|
||||
virtual void error(const char* tag, int context_id, const char* fmt, ...);
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate the log header.
|
||||
* @Param dangerous Whether log is warning or error, log the errno if true.
|
||||
* @Param utc Whether use UTC time format in the log header.
|
||||
* @Param psize Output the actual header size.
|
||||
*/
|
||||
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, int cid, const char* level, int* psize);
|
||||
|
||||
#endif
|
25
trunk/src/service/srs_service_st.cpp
Normal file
25
trunk/src/service/srs_service_st.cpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013-2017 OSSRS(winlin)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <srs_service_st.hpp>
|
||||
|
32
trunk/src/service/srs_service_st.hpp
Normal file
32
trunk/src/service/srs_service_st.hpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013-2017 OSSRS(winlin)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRS_SERVICE_ST_HPP
|
||||
#define SRS_SERVICE_ST_HPP
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <st.h>
|
||||
|
||||
#endif
|
||||
|
|
@ -30,15 +30,15 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#include <srs_app_log.hpp>
|
||||
|
||||
// kernel module.
|
||||
ISrsLog* _srs_log = new MockEmptyLog(SrsLogLevel::Disabled);
|
||||
ISrsLog* _srs_log = new MockEmptyLog(SrsLogLevelDisabled);
|
||||
ISrsThreadContext* _srs_context = new ISrsThreadContext();
|
||||
// app module.
|
||||
SrsConfig* _srs_config = NULL;
|
||||
SrsServer* _srs_server = NULL;
|
||||
|
||||
MockEmptyLog::MockEmptyLog(int level)
|
||||
MockEmptyLog::MockEmptyLog(SrsLogLevel l)
|
||||
{
|
||||
_level = level;
|
||||
level = l;
|
||||
}
|
||||
|
||||
MockEmptyLog::~MockEmptyLog()
|
||||
|
|
|
@ -84,7 +84,7 @@ void srs_bytes_print(char* pa, int size);
|
|||
class MockEmptyLog : public SrsFastLog
|
||||
{
|
||||
public:
|
||||
MockEmptyLog(int level);
|
||||
MockEmptyLog(SrsLogLevel l);
|
||||
virtual ~MockEmptyLog();
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue