lin
2025-08-21 57113df3a0e2be01232281fad9a5f2c060567981
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
/******************************************************************************
 *
 *  Copyright (C) 2020-2021 SeekWave Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 ******************************************************************************/
 
#include <time.h>
#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <utils/Log.h>
#include <errno.h>
#include <sys/stat.h>
#include "skw_log.h"
#include "skw_common.h"
 
 
static pthread_mutex_t skwlog_lock;
 
static int skwlog_fp = -1;
unsigned int skwlog_rev_length = 0;
unsigned int skwlog_cnts = 0;
 
extern char skw_btsnoop_path[];
extern char btcp_log_en;
extern char skwlog_slice;
extern char btsnoop_save_log;
 
 
void skwlog_open(char new_file)
{
    char log_path[PATH_MAX];
    char basepath[PATH_MAX] = {0};
    char lastpath[PATH_MAX] = {0};
    int  i;
    int size = strlen(skw_btsnoop_path);
    if((!btcp_log_en) || (size <= 0))
    {
        return ;
    }
 
    for( i = size - 1; i > 0; i--)//get log dir
    {
        char ch = skw_btsnoop_path[i];
        if((ch == '/') || (ch == '\\'))
        {
            memcpy(basepath, skw_btsnoop_path, i);
            //ALOGD("cp:%d", i);
            break;
        }
    }
    snprintf(log_path, PATH_MAX, "%s/skwlog.log", basepath);
 
    struct stat buf;
    int fd = -1;
    if(stat(log_path, &buf) == 0)
    {
        fd = open(log_path, O_RDONLY);
    }
    if(fd > 0)
    {
        fstat(fd, &buf);
        int file_zie = (int)buf.st_size;
        close(fd);
        ALOGD("%s cp log file size:%d", __func__, file_zie);
        if(skwlog_slice)
        {
            if(new_file)
            {
                snprintf(lastpath, PATH_MAX, "%s/skwlog-last.log", basepath);
                remove(lastpath);
                if (!rename(log_path, lastpath) && (errno != ENOENT))
                {
                    ALOGE("%s unable to rename '%s' to '%s': %s", __func__, log_path, lastpath, strerror(errno));
                }
            }
            else
            {
                skwlog_rev_length = (unsigned int)file_zie;
            }
        }
        else
        {
            if(file_zie > (16 + 12))
            {
                time_t current_time = time(NULL);
                struct tm *time_created = localtime(&current_time);
                char config_time_created[sizeof("YYYY-MM-DD-HHMMSS") + 1];
                strftime(config_time_created, sizeof("YYYY-MM-DD-HHMMSS"), "%Y-%m-%d-%H%M%S", time_created);
 
                snprintf(lastpath, PATH_MAX, "%s/skwlog-%s_%03d-%02d.log", basepath, config_time_created, rand() % 1000, (int)skwlog_cnts);
 
                if (!rename(log_path, lastpath) && (errno != ENOENT))
                {
                    ALOGE("%s unable to rename '%s' to '%s': %s", __func__, log_path, lastpath, strerror(errno));
                }
 
            }
        }
        if(0 == file_zie)
        {
            new_file = TRUE;
        }
    }
    else
    {
        new_file = TRUE;
    }
 
    //ALOGD("basepath:%d, %s,%s",size, basepath, skw_btsnoop_path);
 
    ALOGD("skwlog:%s, %s", log_path, lastpath);
 
    skwlog_fp = open(log_path, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
 
    ALOGD("%s open '%s', fd:%d, is new:%d", __func__, log_path, skwlog_fp, new_file);
    //lseek(skwlog_fp, 0, SEEK_END);
    if(skwlog_fp > 0)
    {
        if(new_file)
        {
            write(skwlog_fp, "skwcplog\0\1\0\2\0\0\x3\xEA", 16);
        }
        //skwlog_print_current_time();
    }
    skwlog_cnts ++;
}
 
void skwlog_reopen(char new_file)
{
    pthread_mutex_lock(&skwlog_lock);
    skwlog_rev_length = 0;
    if (skwlog_fp != -1)
    {
        close(skwlog_fp);
    }
    skwlog_open(new_file);
    pthread_mutex_unlock(&skwlog_lock);
    if(skwlog_fp > 0)
    {
        skwlog_print_current_time();
    }
}
 
 
void skwlog_print_current_time()
{
    time_t current_time = time(NULL);
    struct tm *time_created = localtime(&current_time);
 
    //
    unsigned char buffer[16] = {0x07, 0xFF, 0x08, 0x00, 0x01, 0xD0, 0x55, 0x55};
    buffer[8] = time_created->tm_sec;//[0,59]
    buffer[9] = time_created->tm_min;//[0,59]
    buffer[10] = time_created->tm_hour;//[0,23]
    buffer[11] = time_created->tm_mday;//[1,31]
 
    skwlog_write(buffer, 12);
}
 
void skwlog_init()
{
    pthread_mutex_init(&skwlog_lock, NULL);
    skwlog_fp = -1;
    skwlog_rev_length = 0;
    skwlog_cnts = 0;
    if(btcp_log_en && ((!btsnoop_save_log) || (!skwlog_slice)))
    {
        skwlog_open(TRUE);
        if(skwlog_fp > 0)
        {
            skwlog_print_current_time();
        }
    }
    ALOGD("skwlog_init,en:%d, slice:%d, fd:%d", btcp_log_en, skwlog_slice, skwlog_fp);
}
 
 
void skwlog_write(unsigned char *buffer, unsigned int length)
{
    if(skwlog_fp > 0)
    {
        pthread_mutex_lock(&skwlog_lock);
 
        write(skwlog_fp, buffer, length);
        skwlog_rev_length += length;
 
        if(skwlog_rev_length >= SKW_LOG_DEFAULT_SIZE)
        {
            skwlog_rev_length = 0;
            close(skwlog_fp);
            skwlog_fp = -1;
            skwlog_open(TRUE);
        }
        pthread_mutex_unlock(&skwlog_lock);
    }
}
 
void skwlog_close()
{
    pthread_mutex_destroy(&skwlog_lock);
    if (skwlog_fp != -1)
    {
        close(skwlog_fp);
    }
    skwlog_fp = -1;
    skwlog_rev_length = 0;
    skwlog_cnts = 0;
}