summaryrefslogtreecommitdiffstats
path: root/libs/utils/LogSocket.cpp
blob: 55c1b99aff96af5b1cf8800388bbed0f8ee8c540 (plain)
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
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#ifndef HAVE_WINSOCK
//#define SOCKETLOG
#endif

#ifdef SOCKETLOG

#define LOG_TAG "SOCKETLOG"

#include <string.h>
#include <cutils/log.h>
#include "utils/LogSocket.h"
#include "utils/logger.h"
#include "cutils/hashmap.h"

// defined in //device/data/etc/event-log-tags
#define SOCKET_CLOSE_LOG 51000

static Hashmap* statsMap = NULL;

#define LOG_LIST_NUMBER 5

typedef struct SocketStats {
    int fd;
    unsigned int send;
    unsigned int recv;
    unsigned int ip;
    unsigned short port;
    short reason;
}SocketStats;

SocketStats *get_socket_stats(int fd) {
    if (statsMap == NULL) {
        statsMap = hashmapCreate(8, &hashmapIntHash, &hashmapIntEquals);
    }

    SocketStats *s = (SocketStats*) hashmapGet(statsMap, &fd);
    if (s == NULL) {
        // LOGD("create SocketStats for fd %d", fd);
        s = (SocketStats*) malloc(sizeof(SocketStats));
        memset(s, 0, sizeof(SocketStats));
        s->fd = fd;
        hashmapPut(statsMap, &s->fd, s);
    }
    return s;
}

void log_socket_connect(int fd, unsigned int ip, unsigned short port) {
    // LOGD("log_socket_connect for fd %d ip %d port%d", fd, ip, port);
    SocketStats *s = get_socket_stats(fd);
    s->ip = ip;
    s->port = port;
}

void add_send_stats(int fd, int send) {
    if (send <=0) {
        LOGE("add_send_stats send %d", send);
        return;
    }
    SocketStats *s = get_socket_stats(fd);
    s->send += send;
    // LOGD("add_send_stats for fd %d ip %d port%d", fd, s->ip, s->port);
}

void add_recv_stats(int fd, int recv) {
    if (recv <=0) {
        LOGE("add_recv_stats recv %d", recv);
        return;
    }
    SocketStats *s = get_socket_stats(fd);
    s->recv += recv;
    // LOGD("add_recv_stats for fd %d ip %d port%d", fd, s->ip, s->port);
}

char* put_int(char* buf, int value) {
    *buf = EVENT_TYPE_INT;
    buf++;
    memcpy(buf, &value, sizeof(int));
    return buf + sizeof(int);
}

void log_socket_close(int fd, short reason) {
    if (statsMap) {
        SocketStats *s = (SocketStats*) hashmapGet(statsMap, &fd);
        if (s != NULL) {
            if (s->send != 0 || s->recv != 0) {
                s->reason = reason;
                // 5 int + list type need 2 bytes
                char buf[LOG_LIST_NUMBER * 5 + 2];
                buf[0] = EVENT_TYPE_LIST;
                buf[1] = LOG_LIST_NUMBER;
                char* writePos = buf + 2;
                writePos = put_int(writePos, s->send);
                writePos = put_int(writePos, s->recv);
                writePos = put_int(writePos, s->ip);
                writePos = put_int(writePos, s->port);
                writePos = put_int(writePos, s->reason);
                
                android_bWriteLog(SOCKET_CLOSE_LOG, buf, sizeof(buf));
                // LOGD("send %d recv %d reason %d", s->send, s->recv, s->reason);
            }
            hashmapRemove(statsMap, &s->fd);
            free(s);
        }
    }
}

#else
void add_send_stats(int fd, int send) {} 
void add_recv_stats(int fd, int recv) {}
void log_socket_close(int fd, short reason) {}
void log_socket_connect(int fd, unsigned int ip, unsigned short port) {}
#endif