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