1 Star 0 Fork 129

hmb-2018 / nginx-http-flv-module

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ngx_rtmp_log_module.c 30.17 KB
一键复制 编辑 原始数据 按行查看 历史
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
/*
* Copyright (C) Roman Arutyunyan
* Copyright (C) Winshining
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include "ngx_rtmp_cmd_module.h"
static ngx_rtmp_publish_pt next_publish;
static ngx_rtmp_play_pt next_play;
static ngx_int_t ngx_rtmp_log_postconfiguration(ngx_conf_t *cf);
static void *ngx_rtmp_log_create_main_conf(ngx_conf_t *cf);
static void * ngx_rtmp_log_create_app_conf(ngx_conf_t *cf);
static char * ngx_rtmp_log_merge_app_conf(ngx_conf_t *cf,
void *parent, void *child);
static char * ngx_rtmp_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char * ngx_rtmp_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char * ngx_rtmp_log_compile_format(ngx_conf_t *cf, ngx_array_t *ops,
ngx_array_t *args, ngx_uint_t s);
static ngx_int_t ngx_rtmp_log_flush(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in);
typedef struct ngx_rtmp_log_op_s ngx_rtmp_log_op_t;
typedef size_t (*ngx_rtmp_log_op_getlen_pt)(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op);
typedef u_char * (*ngx_rtmp_log_op_getdata_pt)(ngx_rtmp_session_t *s,
u_char *buf, ngx_rtmp_log_op_t *log);
struct ngx_rtmp_log_op_s {
ngx_rtmp_log_op_getlen_pt getlen;
ngx_rtmp_log_op_getdata_pt getdata;
ngx_str_t value;
ngx_uint_t offset;
};
typedef struct {
ngx_str_t name;
ngx_rtmp_log_op_getlen_pt getlen;
ngx_rtmp_log_op_getdata_pt getdata;
ngx_uint_t offset;
} ngx_rtmp_log_var_t;
typedef struct {
ngx_str_t name;
ngx_array_t *ops; /* ngx_rtmp_log_op_t */
} ngx_rtmp_log_fmt_t;
typedef struct {
ngx_open_file_t *file;
time_t disk_full_time;
time_t error_log_time;
ngx_rtmp_log_fmt_t *format;
} ngx_rtmp_log_t;
typedef struct {
ngx_array_t *logs; /* ngx_rtmp_log_t */
ngx_uint_t off;
ngx_msec_t interval;
size_t size;
} ngx_rtmp_log_app_conf_t;
typedef struct {
ngx_array_t formats; /* ngx_rtmp_log_fmt_t */
ngx_uint_t combined_used;
} ngx_rtmp_log_main_conf_t;
typedef struct {
u_char *line;
ngx_event_t ev;
unsigned play:1;
unsigned publish:1;
u_char name[NGX_RTMP_MAX_NAME];
u_char args[NGX_RTMP_MAX_ARGS];
uint32_t last_sent;
uint32_t last_received;
} ngx_rtmp_log_ctx_t;
static ngx_str_t ngx_rtmp_access_log = ngx_string(NGX_HTTP_LOG_PATH);
static ngx_command_t ngx_rtmp_log_commands[] = {
{ ngx_string("access_log"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE12,
ngx_rtmp_log_set_log,
NGX_RTMP_APP_CONF_OFFSET,
0,
NULL },
{ ngx_string("log_format"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_2MORE,
ngx_rtmp_log_set_format,
NGX_RTMP_MAIN_CONF_OFFSET,
0,
NULL },
{
ngx_string("log_interval"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_rtmp_log_app_conf_t, interval),
NULL
},
{
ngx_string("log_size"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_rtmp_log_app_conf_t, size),
NULL
},
ngx_null_command
};
static ngx_rtmp_module_t ngx_rtmp_log_module_ctx = {
NULL, /* preconfiguration */
ngx_rtmp_log_postconfiguration, /* postconfiguration */
ngx_rtmp_log_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_rtmp_log_create_app_conf, /* create app configuration */
ngx_rtmp_log_merge_app_conf /* merge app configuration */
};
ngx_module_t ngx_rtmp_log_module = {
NGX_MODULE_V1,
&ngx_rtmp_log_module_ctx, /* module context */
ngx_rtmp_log_commands, /* module directives */
NGX_RTMP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_str_t ngx_rtmp_combined_fmt =
ngx_string("$remote_addr [$time_local] $command "
"\"$app\" \"$name\" \"$args\" - "
"$bytes_received $bytes_sent "
"\"$pageurl\" \"$flashver\" ($session_readable_time)");
static size_t
ngx_rtmp_log_var_default_getlen(ngx_rtmp_session_t *s, ngx_rtmp_log_op_t *op)
{
return op->value.len;
}
static u_char *
ngx_rtmp_log_var_default_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
return ngx_cpymem(buf, op->value.data, op->value.len);
}
static size_t
ngx_rtmp_log_var_connection_getlen(ngx_rtmp_session_t *s, ngx_rtmp_log_op_t *op)
{
return NGX_INT_T_LEN;
}
static u_char *
ngx_rtmp_log_var_connection_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
return ngx_sprintf(buf, "%ui", (ngx_uint_t) s->connection->number);
}
static size_t
ngx_rtmp_log_var_remote_addr_getlen(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op)
{
return s->connection->addr_text.len;
}
static u_char *
ngx_rtmp_log_var_remote_addr_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
return ngx_cpymem(buf, s->connection->addr_text.data,
s->connection->addr_text.len);
}
static size_t
ngx_rtmp_log_var_msec_getlen(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op)
{
return NGX_TIME_T_LEN + 4;
}
static u_char *
ngx_rtmp_log_var_msec_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
ngx_time_t *tp;
tp = ngx_timeofday();
return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
}
static size_t
ngx_rtmp_log_var_session_string_getlen(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op)
{
return ((ngx_str_t *) ((u_char *) s + op->offset))->len;
}
static u_char *
ngx_rtmp_log_var_session_string_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
ngx_str_t *str;
str = (ngx_str_t *) ((u_char *) s + op->offset);
return ngx_cpymem(buf, str->data, str->len);
}
static size_t
ngx_rtmp_log_var_command_getlen(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op)
{
return sizeof("PLAY+PUBLISH") - 1;
}
static u_char *
ngx_rtmp_log_var_command_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
ngx_rtmp_log_ctx_t *ctx;
ngx_str_t *cmd;
ngx_uint_t n;
static ngx_str_t commands[] = {
ngx_string("NONE"),
ngx_string("PLAY"),
ngx_string("PUBLISH"),
ngx_string("PLAY+PUBLISH")
};
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_log_module);
n = ctx ? (ctx->play + ctx->publish * 2) : 0;
cmd = &commands[n];
return ngx_cpymem(buf, cmd->data, cmd->len);
}
static size_t
ngx_rtmp_log_var_context_cstring_getlen(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op)
{
return ngx_max(NGX_RTMP_MAX_NAME, NGX_RTMP_MAX_ARGS);
}
static u_char *
ngx_rtmp_log_var_context_cstring_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
ngx_rtmp_log_ctx_t *ctx;
u_char *p;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_log_module);
if (ctx == NULL) {
return buf;
}
p = (u_char *) ctx + op->offset;
while (*p) {
*buf++ = *p++;
}
return buf;
}
static size_t
ngx_rtmp_log_var_session_uint32_getlen(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op)
{
return NGX_INT32_LEN;
}
#if 0
static u_char *
ngx_rtmp_log_var_session_uint32_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
uint32_t *v;
v = (uint32_t *) ((uint8_t *) s + op->offset);
return ngx_sprintf(buf, "%uD", *v);
}
#endif
static size_t
ngx_rtmp_log_var_time_local_getlen(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op)
{
return ngx_cached_http_log_time.len;
}
static u_char *
ngx_rtmp_log_var_time_local_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
return ngx_cpymem(buf, ngx_cached_http_log_time.data,
ngx_cached_http_log_time.len);
}
static size_t
ngx_rtmp_log_var_session_time_getlen(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op)
{
return NGX_INT64_LEN;
}
static u_char *
ngx_rtmp_log_var_session_time_getdata(ngx_rtmp_session_t *s, u_char *buf,
ngx_rtmp_log_op_t *op)
{
return ngx_sprintf(buf, "%L",
(int64_t) (ngx_current_msec - s->epoch) / 1000);
}
static size_t
ngx_rtmp_log_var_session_readable_time_getlen(ngx_rtmp_session_t *s,
ngx_rtmp_log_op_t *op)
{
return NGX_INT_T_LEN + sizeof("d 23h 59m 59s") - 1;
}
static u_char *
ngx_rtmp_log_var_session_readable_time_getdata(ngx_rtmp_session_t *s,
u_char *buf, ngx_rtmp_log_op_t *op)
{
int64_t v;
ngx_uint_t days, hours, minutes, seconds;
v = (ngx_current_msec - s->epoch) / 1000;
days = (ngx_uint_t) (v / (60 * 60 * 24));
hours = (ngx_uint_t) (v / (60 * 60) % 24);
minutes = (ngx_uint_t) (v / 60 % 60);
seconds = (ngx_uint_t) (v % 60);
if (days) {
buf = ngx_sprintf(buf, "%uid ", days);
}
if (days || hours) {
buf = ngx_sprintf(buf, "%uih ", hours);
}
if (days || hours || minutes) {
buf = ngx_sprintf(buf, "%uim ", minutes);
}
buf = ngx_sprintf(buf, "%uis", seconds);
return buf;
}
static u_char *
ngx_rtmp_log_var_session_bytesent_getdata(ngx_rtmp_session_t *s,
u_char *buf, ngx_rtmp_log_op_t *op)
{
ngx_rtmp_log_ctx_t *ctx;
uint32_t sent;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_log_module);
if (ctx == NULL) {
if (s->out_bytes > 0) {
return ngx_sprintf(buf, "%uD", s->out_bytes);
}
*buf = '0';
return buf + 1;
}
sent = s->out_bytes - ctx->last_sent;
ctx->last_sent = s->out_bytes;
if (sent > 0) {
return ngx_sprintf(buf, "%uD", sent);
}
*buf = '0';
return buf + 1;
}
static u_char *
ngx_rtmp_log_var_session_bytereceived_getdata(ngx_rtmp_session_t *s,
u_char *buf, ngx_rtmp_log_op_t *op)
{
ngx_rtmp_log_ctx_t *ctx;
uint32_t received;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_log_module);
if (ctx == NULL) {
if (s->in_bytes > 0) {
return ngx_sprintf(buf, "%uD", s->in_bytes);
}
*buf = '0';
return buf + 1;
}
received = s->in_bytes - ctx->last_received;
ctx->last_received = s->in_bytes;
if (received > 0) {
return ngx_sprintf(buf, "%uD", received);
}
*buf = '0';
return buf + 1;
}
static ngx_rtmp_log_var_t ngx_rtmp_log_vars[] = {
{ ngx_string("connection"),
ngx_rtmp_log_var_connection_getlen,
ngx_rtmp_log_var_connection_getdata,
0 },
{ ngx_string("remote_addr"),
ngx_rtmp_log_var_remote_addr_getlen,
ngx_rtmp_log_var_remote_addr_getdata,
0 },
{ ngx_string("app"),
ngx_rtmp_log_var_session_string_getlen,
ngx_rtmp_log_var_session_string_getdata,
offsetof(ngx_rtmp_session_t, app) },
{ ngx_string("flashver"),
ngx_rtmp_log_var_session_string_getlen,
ngx_rtmp_log_var_session_string_getdata,
offsetof(ngx_rtmp_session_t, flashver) },
{ ngx_string("swfurl"),
ngx_rtmp_log_var_session_string_getlen,
ngx_rtmp_log_var_session_string_getdata,
offsetof(ngx_rtmp_session_t, swf_url) },
{ ngx_string("tcurl"),
ngx_rtmp_log_var_session_string_getlen,
ngx_rtmp_log_var_session_string_getdata,
offsetof(ngx_rtmp_session_t, tc_url) },
{ ngx_string("pageurl"),
ngx_rtmp_log_var_session_string_getlen,
ngx_rtmp_log_var_session_string_getdata,
offsetof(ngx_rtmp_session_t, page_url) },
{ ngx_string("command"),
ngx_rtmp_log_var_command_getlen,
ngx_rtmp_log_var_command_getdata,
0 },
{ ngx_string("name"),
ngx_rtmp_log_var_context_cstring_getlen,
ngx_rtmp_log_var_context_cstring_getdata,
offsetof(ngx_rtmp_log_ctx_t, name) },
{ ngx_string("args"),
ngx_rtmp_log_var_context_cstring_getlen,
ngx_rtmp_log_var_context_cstring_getdata,
offsetof(ngx_rtmp_log_ctx_t, args) },
{ ngx_string("bytes_sent"),
ngx_rtmp_log_var_session_uint32_getlen,
ngx_rtmp_log_var_session_bytesent_getdata,
0 },
{ ngx_string("bytes_received"),
ngx_rtmp_log_var_session_uint32_getlen,
ngx_rtmp_log_var_session_bytereceived_getdata,
0 },
{ ngx_string("time_local"),
ngx_rtmp_log_var_time_local_getlen,
ngx_rtmp_log_var_time_local_getdata,
0 },
{ ngx_string("msec"),
ngx_rtmp_log_var_msec_getlen,
ngx_rtmp_log_var_msec_getdata,
0 },
{ ngx_string("session_time"),
ngx_rtmp_log_var_session_time_getlen,
ngx_rtmp_log_var_session_time_getdata,
0 },
{ ngx_string("session_readable_time"),
ngx_rtmp_log_var_session_readable_time_getlen,
ngx_rtmp_log_var_session_readable_time_getdata,
0 },
{ ngx_null_string, NULL, NULL, 0 }
};
static void *
ngx_rtmp_log_create_main_conf(ngx_conf_t *cf)
{
ngx_rtmp_log_main_conf_t *lmcf;
ngx_rtmp_log_fmt_t *fmt;
lmcf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_log_main_conf_t));
if (lmcf == NULL) {
return NULL;
}
if (ngx_array_init(&lmcf->formats, cf->pool, 4, sizeof(ngx_rtmp_log_fmt_t))
!= NGX_OK)
{
return NULL;
}
fmt = ngx_array_push(&lmcf->formats);
if (fmt == NULL) {
return NULL;
}
ngx_str_set(&fmt->name, "combined");
fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_rtmp_log_op_t));
if (fmt->ops == NULL) {
return NULL;
}
return lmcf;
}
static void *
ngx_rtmp_log_create_app_conf(ngx_conf_t *cf)
{
ngx_rtmp_log_app_conf_t *lacf;
lacf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_log_app_conf_t));
if (lacf == NULL) {
return NULL;
}
lacf->interval = NGX_CONF_UNSET_MSEC;
lacf->size = NGX_CONF_UNSET_SIZE;
return lacf;
}
static char *
ngx_rtmp_log_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_rtmp_log_app_conf_t *prev = parent;
ngx_rtmp_log_app_conf_t *conf = child;
ngx_rtmp_log_main_conf_t *lmcf;
ngx_rtmp_log_fmt_t *fmt;
ngx_rtmp_log_t *log;
ngx_conf_merge_msec_value(conf->interval, prev->interval, 0);
ngx_conf_merge_size_value(conf->size, prev->size, 1 * 1024 * 1024);
if (conf->logs || conf->off) {
return NGX_OK;
}
conf->logs = prev->logs;
conf->off = prev->off;
if (conf->logs || conf->off) {
return NGX_OK;
}
conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_rtmp_log_t));
if (conf->logs == NULL) {
return NGX_CONF_ERROR;
}
log = ngx_array_push(conf->logs);
if (log == NULL) {
return NGX_CONF_ERROR;
}
log->file = ngx_conf_open_file(cf->cycle, &ngx_rtmp_access_log);
if (log->file == NULL) {
return NGX_CONF_ERROR;
}
log->disk_full_time = 0;
log->error_log_time = 0;
lmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_log_module);
fmt = lmcf->formats.elts;
log->format = &fmt[0];
lmcf->combined_used = 1;
return NGX_CONF_OK;
}
static char *
ngx_rtmp_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_rtmp_log_app_conf_t *lacf = conf;
ngx_rtmp_log_main_conf_t *lmcf;
ngx_rtmp_log_fmt_t *fmt;
ngx_rtmp_log_t *log;
ngx_str_t *value, name;
ngx_uint_t n;
value = cf->args->elts;
if (ngx_strcmp(value[1].data, "off") == 0) {
lacf->off = 1;
return NGX_CONF_OK;
}
if (lacf->logs == NULL) {
lacf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_rtmp_log_t));
if (lacf->logs == NULL) {
return NGX_CONF_ERROR;
}
}
log = ngx_array_push(lacf->logs);
if (log == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(log, sizeof(*log));
lmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_log_module);
log->file = ngx_conf_open_file(cf->cycle, &value[1]);
if (log->file == NULL) {
return NGX_CONF_ERROR;
}
if (cf->args->nelts == 2) {
ngx_str_set(&name, "combined");
lmcf->combined_used = 1;
} else {
name = value[2];
if (ngx_strcmp(name.data, "combined") == 0) {
lmcf->combined_used = 1;
}
}
fmt = lmcf->formats.elts;
for (n = 0; n < lmcf->formats.nelts; ++n, ++fmt) {
if (fmt->name.len == name.len &&
ngx_strncasecmp(fmt->name.data, name.data, name.len) == 0)
{
log->format = fmt;
break;
}
}
if (log->format == NULL) {
ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "unknown log format \"%V\"",
&name);
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static char *
ngx_rtmp_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_rtmp_log_main_conf_t *lmcf = conf;
ngx_rtmp_log_fmt_t *fmt;
ngx_str_t *value;
ngx_uint_t i;
value = cf->args->elts;
if (cf->cmd_type != NGX_RTMP_MAIN_CONF) {
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"\"log_format\" directive can only be used on "
"\"rtmp\" level");
}
fmt = lmcf->formats.elts;
for (i = 0; i < lmcf->formats.nelts; i++) {
if (fmt[i].name.len == value[1].len &&
ngx_strcmp(fmt[i].name.data, value[1].data) == 0)
{
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"duplicate \"log_format\" name \"%V\"",
&value[1]);
return NGX_CONF_ERROR;
}
}
fmt = ngx_array_push(&lmcf->formats);
if (fmt == NULL) {
return NGX_CONF_ERROR;
}
fmt->name = value[1];
fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_rtmp_log_op_t));
if (fmt->ops == NULL) {
return NGX_CONF_ERROR;
}
return ngx_rtmp_log_compile_format(cf, fmt->ops, cf->args, 2);
}
static char *
ngx_rtmp_log_compile_format(ngx_conf_t *cf, ngx_array_t *ops, ngx_array_t *args,
ngx_uint_t s)
{
size_t i, len;
u_char *data, *d, c;
ngx_uint_t bracket;
ngx_str_t *value, var;
ngx_rtmp_log_op_t *op;
ngx_rtmp_log_var_t *v;
value = args->elts;
for (; s < args->nelts; ++s) {
i = 0;
len = value[s].len;
d = value[s].data;
while (i < len) {
op = ngx_array_push(ops);
if (op == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(op, sizeof(*op));
data = &d[i];
if (d[i] == '$') {
if (++i == len) {
goto invalid;
}
if (d[i] == '{') {
bracket = 1;
if (++i == len) {
goto invalid;
}
} else {
bracket = 0;
}
var.data = &d[i];
for (var.len = 0; i < len; ++i, ++var.len) {
c = d[i];
if (c == '}' && bracket) {
++i;
bracket = 0;
break;
}
if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
(c == '_'))
{
continue;
}
break;
}
if (bracket) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"missing closing bracket in \"%V\"",
&var);
return NGX_CONF_ERROR;
}
if (var.len == 0) {
goto invalid;
}
for (v = ngx_rtmp_log_vars; v->name.len; ++v) {
if (v->name.len == var.len &&
ngx_strncmp(v->name.data, var.data, var.len) == 0)
{
op->getlen = v->getlen;
op->getdata = v->getdata;
op->offset = v->offset;
break;
}
}
if (v->name.len == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"unknown variable \"%V\"", &var);
return NGX_CONF_ERROR;
}
continue;
}
++i;
while (i < len && d[i] != '$') {
++i;
}
op->getlen = ngx_rtmp_log_var_default_getlen;
op->getdata = ngx_rtmp_log_var_default_getdata;
op->value.len = &d[i] - data;
op->value.data = ngx_pnalloc(cf->pool, op->value.len);
if (op->value.data == NULL) {
return NGX_CONF_ERROR;
}
ngx_memcpy(op->value.data, data, op->value.len);
}
}
return NGX_CONF_OK;
invalid:
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
return NGX_CONF_ERROR;
}
static void
ngx_rtmp_log_split_output_handler(ngx_event_t *ev)
{
ngx_rtmp_session_t *s;
ngx_rtmp_log_app_conf_t *lacf;
s = ev->data;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_log_module);
if (lacf == NULL || lacf->off || lacf->logs == NULL) {
return;
}
ngx_add_timer(ev, lacf->interval);
ngx_rtmp_log_flush(s, 0, 0);
}
static ngx_rtmp_log_ctx_t *
ngx_rtmp_log_set_names(ngx_rtmp_session_t *s, u_char *name, u_char *args)
{
ngx_rtmp_log_ctx_t *ctx;
ngx_rtmp_log_app_conf_t *lacf;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_log_module);
if (lacf == NULL || lacf->off || lacf->logs == NULL) {
return NULL;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_log_module);
if (ctx == NULL) {
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_log_ctx_t));
if (ctx == NULL) {
return NULL;
}
ctx->line = ngx_pcalloc(s->connection->pool, lacf->size);
if (ctx->line == NULL) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"failed to allocate buffer for log line");
return NULL;
}
if (lacf->interval) {
ctx->ev.handler = ngx_rtmp_log_split_output_handler;
ctx->ev.log = s->connection->log;
ctx->ev.data = s;
ctx->ev.timer_set = 0;
ctx->last_sent = 0;
ctx->last_received = 0;
ngx_add_timer(&ctx->ev, lacf->interval);
}
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_log_module);
}
ngx_memcpy(ctx->name, name, NGX_RTMP_MAX_NAME);
ngx_memcpy(ctx->args, args, NGX_RTMP_MAX_ARGS);
return ctx;
}
static ngx_int_t
ngx_rtmp_log_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
{
ngx_rtmp_log_ctx_t *ctx;
if (s->auto_pushed || s->relay) {
goto next;
}
ctx = ngx_rtmp_log_set_names(s, v->name, v->args);
if (ctx == NULL) {
goto next;
}
ctx->publish = 1;
next:
return next_publish(s, v);
}
static ngx_int_t
ngx_rtmp_log_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
{
ngx_rtmp_log_ctx_t *ctx;
if (s->auto_pushed || s->relay) {
goto next;
}
ctx = ngx_rtmp_log_set_names(s, v->name, v->args);
if (ctx == NULL) {
goto next;
}
ctx->play = 1;
next:
return next_play(s, v);
}
static void
ngx_rtmp_log_write(ngx_rtmp_session_t *s, ngx_rtmp_log_t *log, u_char *buf,
size_t len)
{
u_char *name;
time_t now;
ssize_t n;
int err;
err = 0;
name = log->file->name.data;
n = ngx_write_fd(log->file->fd, buf, len);
if (n == (ssize_t) len) {
return;
}
now = ngx_time();
if (n == -1) {
err = ngx_errno;
if (err == NGX_ENOSPC) {
log->disk_full_time = now;
}
if (now - log->error_log_time > 59) {
ngx_log_error(NGX_LOG_ALERT, s->connection->log, err,
ngx_write_fd_n " to \"%s\" failed", name);
log->error_log_time = now;
}
}
if (now - log->error_log_time > 59) {
ngx_log_error(NGX_LOG_ALERT, s->connection->log, err,
ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
name, n, len);
log->error_log_time = now;
}
}
static ngx_int_t
ngx_rtmp_log_flush(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_rtmp_log_app_conf_t *lacf;
ngx_rtmp_log_t *log;
ngx_rtmp_log_op_t *op;
ngx_rtmp_log_ctx_t *ctx;
ngx_uint_t n, i;
u_char *p;
size_t len;
if (s->auto_pushed || s->relay) {
return NGX_OK;
}
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_log_module);
if (lacf == NULL || lacf->off || lacf->logs == NULL) {
return NGX_OK;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_log_module);
if (ctx == NULL) {
return NGX_OK;
}
log = lacf->logs->elts;
for (i = 0; i < lacf->logs->nelts; ++i, ++log) {
if (ngx_time() == log->disk_full_time) {
/* FreeBSD full disk protection;
* nginx http logger does the same */
continue;
}
len = 0;
op = log->format->ops->elts;
for (n = 0; n < log->format->ops->nelts; ++n, ++op) {
if (len + NGX_LINEFEED_SIZE <= lacf->size) {
len += op->getlen(s, op);
} else {
break;
}
}
len += NGX_LINEFEED_SIZE;
p = ctx->line;
op = log->format->ops->elts;
for (n = 0; n < log->format->ops->nelts; ++n, ++op) {
if (p + NGX_LINEFEED_SIZE <= ctx->line + lacf->size) {
p = op->getdata(s, p, op);
} else {
break;
}
}
ngx_linefeed(p);
ngx_rtmp_log_write(s, log, ctx->line, p - ctx->line);
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_log_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_rtmp_log_app_conf_t *lacf;
ngx_rtmp_log_t *log;
ngx_rtmp_log_op_t *op;
ngx_uint_t n, i;
u_char *p;
ngx_rtmp_log_ctx_t *ctx;
size_t len;
if (s->auto_pushed || s->relay) {
return NGX_OK;
}
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_log_module);
if (lacf == NULL || lacf->off || lacf->logs == NULL) {
return NGX_OK;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_log_module);
if (ctx == NULL) {
return NGX_OK;
}
log = lacf->logs->elts;
for (i = 0; i < lacf->logs->nelts; ++i, ++log) {
if (ngx_time() == log->disk_full_time) {
/* FreeBSD full disk protection;
* nginx http logger does the same */
continue;
}
len = 0;
op = log->format->ops->elts;
for (n = 0; n < log->format->ops->nelts; ++n, ++op) {
if (len + NGX_LINEFEED_SIZE <= lacf->size) {
len += op->getlen(s, op);
} else {
break;
}
}
len += NGX_LINEFEED_SIZE;
p = ctx->line;
op = log->format->ops->elts;
for (n = 0; n < log->format->ops->nelts; ++n, ++op) {
if (p + NGX_LINEFEED_SIZE <= ctx->line + lacf->size) {
p = op->getdata(s, p, op);
} else {
break;
}
}
ngx_linefeed(p);
ngx_rtmp_log_write(s, log, ctx->line, p - ctx->line);
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_log_module);
if(ctx && ctx->ev.timer_set) {
ngx_del_timer(&ctx->ev);
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_log_postconfiguration(ngx_conf_t *cf)
{
ngx_rtmp_core_main_conf_t *cmcf;
ngx_rtmp_handler_pt *h;
ngx_rtmp_log_main_conf_t *lmcf;
ngx_array_t a;
ngx_rtmp_log_fmt_t *fmt;
ngx_str_t *value;
lmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_log_module);
if (lmcf->combined_used) {
if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
return NGX_ERROR;
}
value = ngx_array_push(&a);
if (value == NULL) {
return NGX_ERROR;
}
*value = ngx_rtmp_combined_fmt;
fmt = lmcf->formats.elts;
if (ngx_rtmp_log_compile_format(cf, fmt->ops, &a, 0)
!= NGX_CONF_OK)
{
return NGX_ERROR;
}
}
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
h = ngx_array_push(&cmcf->events[NGX_RTMP_DISCONNECT]);
*h = ngx_rtmp_log_disconnect;
next_publish = ngx_rtmp_publish;
ngx_rtmp_publish = ngx_rtmp_log_publish;
next_play = ngx_rtmp_play;
ngx_rtmp_play = ngx_rtmp_log_play;
return NGX_OK;
}
C
1
https://gitee.com/hmb-2018-admin/nginx-http-flv-module.git
git@gitee.com:hmb-2018-admin/nginx-http-flv-module.git
hmb-2018-admin
nginx-http-flv-module
nginx-http-flv-module
master

搜索帮助