2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <tzplatform_config.h>
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
35 static FILE *log_file = NULL;
37 static inline void __netconfig_log_update_file_revision(int rev)
40 char *log_file = NULL;
41 char *next_log_file = NULL;
43 next_log_rev = rev + 1;
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);
48 if (next_log_rev >= MAX_LOG_COUNT)
49 remove(next_log_file);
51 if (access(next_log_file, F_OK) == 0)
52 __netconfig_log_update_file_revision(next_log_rev);
54 if (rename(log_file, next_log_file) != 0)
58 g_free(next_log_file);
61 static inline void __netconfig_log_make_backup(void)
66 backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
68 if (access(backup, F_OK) == 0)
69 __netconfig_log_update_file_revision(rev);
71 if (rename(LOG_FILE_PATH, backup) != 0)
72 remove(LOG_FILE_PATH);
77 static inline void __netconfig_log_get_local_time(char *strtime, const int size)
81 struct tm result = {0, };
85 local_ptm = localtime_r(&buf, &result);
88 strftime(strtime, size, "%m/%d %H:%M:%S", local_ptm);
91 EXPORT_SYM void netconfig_log(const char *format, ...)
100 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
102 if (log_file == NULL)
105 va_start(ap, format);
107 if (fstat(fileno(log_file), &buf) == 0)
108 log_size = buf.st_size;
110 if (log_size >= MAX_LOG_SIZE) {
114 __netconfig_log_make_backup();
116 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
118 if (log_file == NULL) {
124 __netconfig_log_get_local_time(strtime, sizeof(strtime));
126 if (vsnprintf(str, sizeof(str), format, ap) > 0)
127 fprintf(log_file, "%s %s", strtime, str);
132 EXPORT_SYM void log_cleanup(void)
134 if (log_file == NULL)