2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "stc-manager-util.h"
24 log_info_s g_log_info[] = {
32 static void __sync_file_to_disk(const char *path)
35 fp = fopen(path, "a+");
40 STC_LOGD("Sync the file to disk");
44 static GKeyFile *__load_key_file(const char *path)
46 GKeyFile *keyfile = NULL;
49 STC_LOGD("Loading [%s]", path);
51 keyfile = g_key_file_new();
53 if (!g_key_file_load_from_file(keyfile, path, 0, &error)) {
54 STC_LOGD("Unable to load [%s] : %s", path, error->message); //LCOV_EXCL_LINE
55 g_clear_error(&error); //LCOV_EXCL_LINE
56 g_key_file_free(keyfile); //LCOV_EXCL_LINE
57 keyfile = NULL; //LCOV_EXCL_LINE
63 static int __save_key_file(GKeyFile *keyfile, char *path)
70 data = g_key_file_to_data(keyfile, &length, NULL);
72 if (!g_file_set_contents(path, data, length, &error)) {
73 STC_LOGD("Failed to save information : %s", error->message); //LCOV_EXCL_LINE
74 g_error_free(error); //LCOV_EXCL_LINE
75 ret = -EIO; //LCOV_EXCL_LINE
78 __sync_file_to_disk(path);
85 gboolean stc_util_get_config_bool(char *key)
87 char path[MAX_PATH_LENGTH];
91 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
93 keyfile = __load_key_file(path);
95 keyfile = g_key_file_new();
97 value = g_key_file_get_boolean(keyfile, path, key, NULL);
98 g_key_file_free(keyfile);
103 gchar * stc_util_get_config_str(char *key)
105 char path[MAX_PATH_LENGTH];
109 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
111 keyfile = __load_key_file(path);
113 keyfile = g_key_file_new();
115 value = g_key_file_get_string(keyfile, path, key, NULL);
116 g_key_file_free(keyfile);
122 int stc_util_get_config_int(char *key)
124 char path[MAX_PATH_LENGTH];
128 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
130 keyfile = __load_key_file(path);
132 keyfile = g_key_file_new(); //LCOV_EXCL_LINE
135 value = g_key_file_get_integer(keyfile, path, key, NULL);
136 g_key_file_free(keyfile);
142 API void stc_util_update_log_state(void)
146 for (i = DEBUG_LOG_INFO; i < MAX_LOG_INFO; ++i)
147 g_log_info[i].state = stc_util_get_config_int(g_log_info[i].key);
149 STC_LOGD("log info [%d:%d:%d:%d:%d]",
150 g_log_info[DEBUG_LOG_INFO].state, g_log_info[STAT_LOG_INFO].state,
151 g_log_info[RSTN_LOG_INFO].state, g_log_info[FW_LOG_INFO].state,
152 g_log_info[PCAP_LOG_INFO].state);
155 API void stc_util_set_log_state(log_info_e info, int state)
157 g_log_info[info].state = state;
161 API int stc_util_get_log_state(log_info_e info)
163 if (g_log_info[info].state == -1)
164 g_log_info[info].state = stc_util_get_config_int(g_log_info[info].key);
166 return g_log_info[info].state;
169 void stc_util_initialize_config(void)
171 char path[MAX_PATH_LENGTH];
175 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
177 keyfile = __load_key_file(path);
179 keyfile = g_key_file_new(); //LCOV_EXCL_LINE
180 for (i = DEBUG_LOG_INFO; i < MAX_LOG_INFO; ++i)
181 g_key_file_set_integer(keyfile, path, g_log_info[i].key, 0);
184 __save_key_file(keyfile, path);
185 g_key_file_free(keyfile);