Skip to content

Commit

Permalink
renamed log context & implemented command/name/args in access logger
Browse files Browse the repository at this point in the history
  • Loading branch information
arut committed Jan 13, 2013
1 parent 3dff38d commit b85499c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ngx_rtmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ typedef struct {
typedef struct {
ngx_str_t *client;
ngx_rtmp_session_t *session;
} ngx_rtmp_log_ctx_t;
} ngx_rtmp_error_log_ctx_t;


typedef struct {
Expand Down
10 changes: 5 additions & 5 deletions ngx_rtmp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ ngx_rtmp_init_session(ngx_connection_t *c, ngx_rtmp_addr_conf_t *addr_conf)
{
ngx_rtmp_session_t *s;
ngx_rtmp_core_srv_conf_t *cscf;
ngx_rtmp_log_ctx_t *ctx;
ngx_rtmp_error_log_ctx_t *ctx;

s = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_session_t) +
sizeof(ngx_chain_t *) * ((ngx_rtmp_core_srv_conf_t *)
Expand All @@ -152,7 +152,7 @@ ngx_rtmp_init_session(ngx_connection_t *c, ngx_rtmp_addr_conf_t *addr_conf)
c->data = s;
s->connection = c;

ctx = ngx_palloc(c->pool, sizeof(ngx_rtmp_log_ctx_t));
ctx = ngx_palloc(c->pool, sizeof(ngx_rtmp_error_log_ctx_t));
if (ctx == NULL) {
ngx_rtmp_close_connection(c);
return NULL;
Expand Down Expand Up @@ -202,9 +202,9 @@ ngx_rtmp_init_session(ngx_connection_t *c, ngx_rtmp_addr_conf_t *addr_conf)
static u_char *
ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len)
{
u_char *p;
ngx_rtmp_session_t *s;
ngx_rtmp_log_ctx_t *ctx;
u_char *p;
ngx_rtmp_session_t *s;
ngx_rtmp_error_log_ctx_t *ctx;

if (log->action) {
p = ngx_snprintf(buf, len, " while %s", log->action);
Expand Down
129 changes: 111 additions & 18 deletions ngx_rtmp_log_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,37 @@ typedef struct {


typedef struct {
ngx_str_t name;
ngx_array_t *ops; /* ngx_rtmp_log_op_t */
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_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_array_t *logs; /* ngx_rtmp_log_t */
ngx_uint_t off;
} ngx_rtmp_log_app_conf_t;


typedef struct {
ngx_array_t formats; /* ngx_rtmp_log_fmt_t */
ngx_uint_t combined_used;
ngx_array_t formats; /* ngx_rtmp_log_fmt_t */
ngx_uint_t combined_used;
} ngx_rtmp_log_main_conf_t;


typedef struct {
u_char name[NGX_RTMP_MAX_NAME];
u_char args[NGX_RTMP_MAX_NAME];
} ngx_rtmp_log_ctx_struct_t;
unsigned play:1;
unsigned publish:1;
u_char name[NGX_RTMP_MAX_NAME];
u_char args[NGX_RTMP_MAX_ARGS];
} ngx_rtmp_log_ctx_t;


static ngx_str_t ngx_rtmp_access_log = ngx_string(NGX_HTTP_LOG_PATH);
Expand Down Expand Up @@ -132,7 +134,8 @@ ngx_module_t ngx_rtmp_log_module = {


static ngx_str_t ngx_rtmp_combined_fmt =
ngx_string("$remote_addr - \"$app\" \"$name\" \"$args\" [$time_local] "
ngx_string("$remote_addr [$time_local] $command "
"\"$app\" \"$name\" \"$args\" - "
"$bytes_received $bytes_sent "
"\"$pageurl\" \"$flashver\" ($session_readable_time)");

Expand All @@ -152,7 +155,6 @@ ngx_rtmp_log_var_default_getdata(ngx_rtmp_session_t *s, u_char *buf,
}



static size_t
ngx_rtmp_log_var_number_getlen(ngx_rtmp_session_t *s, ngx_rtmp_log_op_t *op)
{
Expand Down Expand Up @@ -204,19 +206,65 @@ ngx_rtmp_log_var_session_string_getdata(ngx_rtmp_session_t *s, u_char *buf,
}


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;

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);
if (ctx == NULL) {
return buf;
}

cmd = &commands[ctx->play + ctx->publish * 2];

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 0; /*TODO*/
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)
{
return buf; /*TODO*/
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;
}


Expand Down Expand Up @@ -350,15 +398,20 @@ static ngx_rtmp_log_var_t ngx_rtmp_log_vars[] = {
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_struct_t, name) },
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_struct_t, args) },
offsetof(ngx_rtmp_log_ctx_t, args) },

{ ngx_string("bytes_sent"),
ngx_rtmp_log_var_session_uint32_getlen,
Expand Down Expand Up @@ -726,13 +779,44 @@ ngx_rtmp_log_compile_format(ngx_conf_t *cf, ngx_array_t *ops, ngx_array_t *args,
}


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;

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;
}

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) {
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);
}
Expand All @@ -741,10 +825,19 @@ ngx_rtmp_log_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *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) {
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);
}
Expand Down

0 comments on commit b85499c

Please sign in to comment.