-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwayuu_log.c
72 lines (61 loc) · 1.87 KB
/
wayuu_log.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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2018-2020 SCANOSS LTD
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <stdint.h>
#include "router.h"
#include "log.h"
#include "wayuu_log.h"
/**
* Logs access in NGINX Combined log format.
*/
void log_access(api_request *req, int status)
{
char *ts = format_ts_common_log();
char *referer = http_get_header(req, "Referer");
char *ua = http_get_header(req, "User-Agent");
// FORMAT: REMOTE_IP [TIMESTAMP] "HTTP REQUEST" "REFERRER" "USER AGENT"
FILE *fp = fopen(WAYUU_ANALYTICS_LOG, "a");
if (fp != NULL)
{
fprintf(fp, "%s - - [%s] \"%s\" %d %u \"%s\" \"%s\" %.3f\n", req->IP, ts, req->request_line, status, req->response_length, referer, ua, response_time(req));
fclose(fp);
}
free(ts);
if (strlen(referer) > 0)
free(referer);
if (strlen(ua) > 0)
free(ua);
}
uint64_t epoch_millis()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t)(tv.tv_sec) * 1000 +
(uint64_t)(tv.tv_usec) / 1000;
}
double response_time(api_request *req)
{
return (double)(epoch_millis() - req->request_start) / 1000;
}