正在显示
6 个修改的文件
包含
153 行增加
和
1 行删除
| @@ -81,7 +81,7 @@ LibSTfile="${LibSTRoot}/libst.a" | @@ -81,7 +81,7 @@ LibSTfile="${LibSTRoot}/libst.a" | ||
| 81 | #Core Module | 81 | #Core Module |
| 82 | MODULE_ID="CORE" | 82 | MODULE_ID="CORE" |
| 83 | MODULE_DEPENDS=() | 83 | MODULE_DEPENDS=() |
| 84 | -ModuleLibIncs=() | 84 | +ModuleLibIncs=(${LibSTRoot}) |
| 85 | MODULE_FILES=("srs_core_log") | 85 | MODULE_FILES=("srs_core_log") |
| 86 | MODULE_DIR="src/core" . auto/modules.sh | 86 | MODULE_DIR="src/core" . auto/modules.sh |
| 87 | CORE_OBJS="${MODULE_OBJS[@]}" | 87 | CORE_OBJS="${MODULE_OBJS[@]}" |
trunk/src/core/srs_core.hpp
0 → 100755
| 1 | #include <srs_core_log.hpp> | 1 | #include <srs_core_log.hpp> |
| 2 | + | ||
| 3 | +#include <string.h> | ||
| 4 | +#include <sys/time.h> | ||
| 5 | + | ||
| 6 | +#include <string> | ||
| 7 | +#include <map> | ||
| 8 | + | ||
| 9 | +#include <st.h> | ||
| 10 | + | ||
| 11 | +ILogContext::ILogContext(){ | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +ILogContext::~ILogContext(){ | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +class LogContext : public ILogContext | ||
| 18 | +{ | ||
| 19 | +private: | ||
| 20 | + class DateTime | ||
| 21 | + { | ||
| 22 | + private: | ||
| 23 | + // %d-%02d-%02d %02d:%02d:%02d.%03d | ||
| 24 | + #define DATE_LEN 24 | ||
| 25 | + char time_data[DATE_LEN]; | ||
| 26 | + public: | ||
| 27 | + DateTime(); | ||
| 28 | + virtual ~DateTime(); | ||
| 29 | + public: | ||
| 30 | + virtual const char* FormatTime(); | ||
| 31 | + }; | ||
| 32 | +private: | ||
| 33 | + DateTime time; | ||
| 34 | + std::map<st_thread_t, int> cache; | ||
| 35 | +public: | ||
| 36 | + LogContext(); | ||
| 37 | + virtual ~LogContext(); | ||
| 38 | +public: | ||
| 39 | + virtual void SetId(); | ||
| 40 | + virtual int GetId(); | ||
| 41 | +public: | ||
| 42 | + virtual const char* FormatTime(); | ||
| 43 | +}; | ||
| 44 | + | ||
| 45 | +ILogContext* log_context = new LogContext(); | ||
| 46 | + | ||
| 47 | +LogContext::DateTime::DateTime(){ | ||
| 48 | + memset(time_data, 0, DATE_LEN); | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +LogContext::DateTime::~DateTime(){ | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +const char* LogContext::DateTime::FormatTime(){ | ||
| 55 | + // clock time | ||
| 56 | + timeval tv; | ||
| 57 | + if(gettimeofday(&tv, NULL) == -1){ | ||
| 58 | + return ""; | ||
| 59 | + } | ||
| 60 | + // to calendar time | ||
| 61 | + struct tm* tm; | ||
| 62 | + if((tm = localtime(&tv.tv_sec)) == NULL){ | ||
| 63 | + return ""; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + // log header, the time/pid/level of log | ||
| 67 | + // reserved 1bytes for the new line. | ||
| 68 | + snprintf(time_data, DATE_LEN, "%d-%02d-%02d %02d:%02d:%02d.%03d", | ||
| 69 | + 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, | ||
| 70 | + (int)(tv.tv_usec / 1000)); | ||
| 71 | + | ||
| 72 | + return time_data; | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +LogContext::LogContext(){ | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +LogContext::~LogContext(){ | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +void LogContext::SetId(){ | ||
| 82 | + static int id = 0; | ||
| 83 | + cache[st_thread_self()] = id++; | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +int LogContext::GetId(){ | ||
| 87 | + return cache[st_thread_self()]; | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +const char* LogContext::FormatTime(){ | ||
| 91 | + return time.FormatTime(); | ||
| 92 | +} | ||
| 93 | + |
| 1 | #ifndef SRS_CORE_LOG_HPP | 1 | #ifndef SRS_CORE_LOG_HPP |
| 2 | #define SRS_CORE_LOG_HPP | 2 | #define SRS_CORE_LOG_HPP |
| 3 | 3 | ||
| 4 | +/* | ||
| 5 | +#include <srs_core_log.hpp> | ||
| 6 | +*/ | ||
| 7 | + | ||
| 8 | +#include <srs_core.hpp> | ||
| 9 | + | ||
| 10 | +#include <stdio.h> | ||
| 11 | + | ||
| 12 | +#include <errno.h> | ||
| 13 | +#include <string.h> | ||
| 14 | + | ||
| 15 | +class ILogContext | ||
| 16 | +{ | ||
| 17 | +public: | ||
| 18 | + ILogContext(); | ||
| 19 | + virtual ~ILogContext(); | ||
| 20 | +public: | ||
| 21 | + virtual void SetId() = 0; | ||
| 22 | + virtual int GetId() = 0; | ||
| 23 | +public: | ||
| 24 | + virtual const char* FormatTime() = 0; | ||
| 25 | +}; | ||
| 26 | + | ||
| 27 | +// user must implements the LogContext and define a global instance. | ||
| 28 | +extern ILogContext* log_context; | ||
| 29 | + | ||
| 30 | +#if 0 | ||
| 31 | + #define SrsVerbose(msg, ...) printf("[%s][%d][verbs] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n") | ||
| 32 | + #define SrsInfo(msg, ...) printf("[%s][%d][infos] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n") | ||
| 33 | + #define SrsTrace(msg, ...) printf("[%s][%d][trace] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n") | ||
| 34 | + #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") | ||
| 35 | + #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") | ||
| 36 | +#else | ||
| 37 | + #define SrsVerbose(msg, ...) printf("[%s][%d][verbs][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n") | ||
| 38 | + #define SrsInfo(msg, ...) printf("[%s][%d][infos][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n") | ||
| 39 | + #define SrsTrace(msg, ...) printf("[%s][%d][trace][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n") | ||
| 40 | + #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") | ||
| 41 | + #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") | ||
| 42 | +#endif | ||
| 43 | + | ||
| 4 | #endif | 44 | #endif |
| @@ -2,6 +2,7 @@ file | @@ -2,6 +2,7 @@ file | ||
| 2 | main readonly separator, | 2 | main readonly separator, |
| 3 | ..\main\srs_main_server.cpp, | 3 | ..\main\srs_main_server.cpp, |
| 4 | core readonly separator, | 4 | core readonly separator, |
| 5 | + ..\core\srs_core.hpp, | ||
| 5 | ..\core\srs_core_log.hpp, | 6 | ..\core\srs_core_log.hpp, |
| 6 | ..\core\srs_core_log.cpp; | 7 | ..\core\srs_core_log.cpp; |
| 7 | mainconfig | 8 | mainconfig |
-
请 注册 或 登录 后发表评论