681f4b4e4a3f425ef9549e547c55f9ec4bdf15e2
[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
30 #define LOG_FILE_PATH   "/var/log/netconfig.log"
31 #define MAX_LOG_SIZE    1 * 1024 * 1024
32 #define MAX_LOG_COUNT   1
33
34 static FILE *log_file = NULL;
35
36 static inline void __netconfig_log_update_file_revision(int rev)
37 {
38         int next_log_rev = 0;
39         char *log_file = NULL;
40         char *next_log_file = NULL;
41
42         next_log_rev = rev + 1;
43
44         log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
45         next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
46
47         if (next_log_rev >= MAX_LOG_COUNT)
48                 remove(next_log_file);
49
50         if (access(next_log_file, F_OK) == 0)
51                 __netconfig_log_update_file_revision(next_log_rev);
52
53         if (rename(log_file, next_log_file) != 0)
54                 remove(log_file);
55
56         g_free(log_file);
57         g_free(next_log_file);
58 }
59
60 static inline void __netconfig_log_make_backup(void)
61 {
62         const int rev = 0;
63         char *backup = NULL;
64
65         backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
66
67         if (access(backup, F_OK) == 0)
68                 __netconfig_log_update_file_revision(rev);
69
70         if (rename(LOG_FILE_PATH, backup) != 0)
71                 remove(LOG_FILE_PATH);
72
73         g_free(backup);
74 }
75
76 static inline void __netconfig_log_get_local_time(char *strtime, const int size)
77 {
78         time_t buf;
79         struct tm *local_ptm;
80         struct tm result = {0, };
81
82         time(&buf);
83         buf = time(NULL);
84         local_ptm = localtime_r(&buf, &result);
85
86         if (local_ptm)
87                 strftime(strtime, size, "%m/%d %H:%M:%S", local_ptm);
88 }
89
90 void netconfig_log(const char *format, ...)
91 {
92         va_list ap;
93         int log_size = 0;
94         struct stat buf;
95         char str[256];
96         char strtime[40];
97
98         if (log_file == NULL)
99                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
100
101         if (log_file == NULL)
102                 return;
103
104         va_start(ap, format);
105
106         if (fstat(fileno(log_file), &buf) == 0)
107                 log_size = buf.st_size;
108
109         if (log_size >= MAX_LOG_SIZE) {
110                 fclose(log_file);
111                 log_file = NULL;
112
113                 __netconfig_log_make_backup();
114
115                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
116
117                 if (log_file == NULL) {
118                         va_end(ap);
119                         return;
120                 }
121         }
122
123         __netconfig_log_get_local_time(strtime, sizeof(strtime));
124
125         if (vsnprintf(str, sizeof(str), format, ap) > 0)
126                 fprintf(log_file, "%s %s", strtime, str);
127
128         va_end(ap);
129 }
130
131 void log_cleanup(void)
132 {
133         if (log_file == NULL)
134                 return;
135
136         fclose(log_file);
137         log_file = NULL;
138 }