forked from winshining/nginx-http-flv-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_rtmp_log_module.c
1228 lines (939 loc) · 30.2 KB
/
ngx_rtmp_log_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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;