mirror of https://github.com/ossrs/srs.git
add log
parent
7268dd15a1
commit
67d96fcab4
@ -0,0 +1,13 @@
|
||||
#ifndef SRS_CORE_HPP
|
||||
#define SRS_CORE_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core.hpp>
|
||||
*/
|
||||
|
||||
// for int64_t print using PRId64 format.
|
||||
#ifndef __STDC_FORMAT_MACROS
|
||||
#define __STDC_FORMAT_MACROS
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1 +1,93 @@
|
||||
#include <srs_core_log.hpp>
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include <st.h>
|
||||
|
||||
ILogContext::ILogContext(){
|
||||
}
|
||||
|
||||
ILogContext::~ILogContext(){
|
||||
}
|
||||
|
||||
class LogContext : public ILogContext
|
||||
{
|
||||
private:
|
||||
class DateTime
|
||||
{
|
||||
private:
|
||||
// %d-%02d-%02d %02d:%02d:%02d.%03d
|
||||
#define DATE_LEN 24
|
||||
char time_data[DATE_LEN];
|
||||
public:
|
||||
DateTime();
|
||||
virtual ~DateTime();
|
||||
public:
|
||||
virtual const char* FormatTime();
|
||||
};
|
||||
private:
|
||||
DateTime time;
|
||||
std::map<st_thread_t, int> cache;
|
||||
public:
|
||||
LogContext();
|
||||
virtual ~LogContext();
|
||||
public:
|
||||
virtual void SetId();
|
||||
virtual int GetId();
|
||||
public:
|
||||
virtual const char* FormatTime();
|
||||
};
|
||||
|
||||
ILogContext* log_context = new LogContext();
|
||||
|
||||
LogContext::DateTime::DateTime(){
|
||||
memset(time_data, 0, DATE_LEN);
|
||||
}
|
||||
|
||||
LogContext::DateTime::~DateTime(){
|
||||
}
|
||||
|
||||
const char* LogContext::DateTime::FormatTime(){
|
||||
// clock time
|
||||
timeval tv;
|
||||
if(gettimeofday(&tv, NULL) == -1){
|
||||
return "";
|
||||
}
|
||||
// to calendar time
|
||||
struct tm* tm;
|
||||
if((tm = localtime(&tv.tv_sec)) == NULL){
|
||||
return "";
|
||||
}
|
||||
|
||||
// log header, the time/pid/level of log
|
||||
// reserved 1bytes for the new line.
|
||||
snprintf(time_data, DATE_LEN, "%d-%02d-%02d %02d:%02d:%02d.%03d",
|
||||
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));
|
||||
|
||||
return time_data;
|
||||
}
|
||||
|
||||
LogContext::LogContext(){
|
||||
}
|
||||
|
||||
LogContext::~LogContext(){
|
||||
}
|
||||
|
||||
void LogContext::SetId(){
|
||||
static int id = 0;
|
||||
cache[st_thread_self()] = id++;
|
||||
}
|
||||
|
||||
int LogContext::GetId(){
|
||||
return cache[st_thread_self()];
|
||||
}
|
||||
|
||||
const char* LogContext::FormatTime(){
|
||||
return time.FormatTime();
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,44 @@
|
||||
#ifndef SRS_CORE_LOG_HPP
|
||||
#define SRS_CORE_LOG_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_log.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
class ILogContext
|
||||
{
|
||||
public:
|
||||
ILogContext();
|
||||
virtual ~ILogContext();
|
||||
public:
|
||||
virtual void SetId() = 0;
|
||||
virtual int GetId() = 0;
|
||||
public:
|
||||
virtual const char* FormatTime() = 0;
|
||||
};
|
||||
|
||||
// user must implements the LogContext and define a global instance.
|
||||
extern ILogContext* log_context;
|
||||
|
||||
#if 0
|
||||
#define SrsVerbose(msg, ...) printf("[%s][%d][verbs] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsInfo(msg, ...) printf("[%s][%d][infos] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsTrace(msg, ...) printf("[%s][%d][trace] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsWarn(msg, ...) printf("[%s][%d][warns] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define SrsError(msg, ...) printf("[%s][%d][error] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#else
|
||||
#define SrsVerbose(msg, ...) printf("[%s][%d][verbs][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsInfo(msg, ...) printf("[%s][%d][infos][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsTrace(msg, ...) printf("[%s][%d][trace][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsWarn(msg, ...) printf("[%s][%d][warns][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define SrsError(msg, ...) printf("[%s][%d][error][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,11 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include <srs_core_log.hpp>
|
||||
|
||||
int main(int /*argc*/, char** /*argv*/){
|
||||
log_context->SetId();
|
||||
|
||||
SrsWarn("server start");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue