Modify send_arp
[platform/core/connectivity/net-config.git] / src / utils / log.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <glib.h>
21 #include <sys/time.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <tzplatform_config.h>
27
28 #include "log.h"
29 #include "neterror.h"
30
31 #define LOG_FILE_PATH   "/opt/usr/data/network/netconfig.log"
32 #define MAX_LOG_SIZE    1 * 1024 * 1024
33 #define MAX_LOG_COUNT   1
34
35 static FILE *log_file = NULL;
36
37 static inline void __netconfig_log_update_file_revision(int rev)
38 {
39         int next_log_rev = 0;
40         char *log_file = NULL;
41         char *next_log_file = NULL;
42
43         next_log_rev = rev + 1;
44
45         log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
46         next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
47
48         if (next_log_rev >= MAX_LOG_COUNT)
49                 remove(next_log_file);
50
51         if (access(next_log_file, F_OK) == 0)
52                 __netconfig_log_update_file_revision(next_log_rev);
53
54         if (rename(log_file, next_log_file) != 0)
55                 remove(log_file);
56
57         g_free(log_file);
58         g_free(next_log_file);
59 }
60
61 static inline void __netconfig_log_make_backup(void)
62 {
63         const int rev = 0;
64         char *backup = NULL;
65
66         backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
67
68         if (access(backup, F_OK) == 0)
69                 __netconfig_log_update_file_revision(rev);
70
71         if (rename(LOG_FILE_PATH, backup) != 0)
72                 remove(LOG_FILE_PATH);
73
74         g_free(backup);
75 }
76
77 static inline void __netconfig_log_get_local_time(char *strtime, const int size)
78 {
79         time_t buf;
80         struct tm *local_ptm;
81         struct tm result = {0, };
82
83         time(&buf);
84         buf = time(NULL);
85         local_ptm = localtime_r(&buf, &result);
86
87         if (local_ptm)
88                 strftime(strtime, size, "%m/%d %H:%M:%S", local_ptm);
89 }
90
91 EXPORT_SYM void netconfig_log(const char *format, ...)
92 {
93         va_list ap;
94         int log_size = 0;
95         struct stat buf;
96         char str[256];
97         char strtime[40];
98
99         if (log_file == NULL)
100                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
101
102         if (log_file == NULL)
103                 return;
104
105         va_start(ap, format);
106
107         if (fstat(fileno(log_file), &buf) == 0)
108                 log_size = buf.st_size;
109
110         if (log_size >= MAX_LOG_SIZE) {
111                 fclose(log_file);
112                 log_file = NULL;
113
114                 __netconfig_log_make_backup();
115
116                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
117
118                 if (log_file == NULL) {
119                         va_end(ap);
120                         return;
121                 }
122         }
123
124         __netconfig_log_get_local_time(strtime, sizeof(strtime));
125
126         if (vsnprintf(str, sizeof(str), format, ap) > 0)
127                 fprintf(log_file, "%s %s", strtime, str);
128
129         va_end(ap);
130 }
131
132 EXPORT_SYM void log_cleanup(void)
133 {
134         if (log_file == NULL)
135                 return;
136
137         fclose(log_file);
138         log_file = NULL;
139 }