3 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 #include "logconfig.h"
24 #include "loglimiter.h"
26 /* Functions possible return value */
27 #define RET_ERROR (-1)
30 #define CONFIG_LINE_MAX_LEN 256
31 #define CONFIG_OPTION_MAX_LEN 64
34 #define isCOMMENT(c) ('#' == c)
35 #define isNEWLINE(c) ('\n' == c || '\r' == c)
37 /* Dlog options definition */
38 #define LOG_PLATFORM_STRING "PlatformLogs"
39 #define LOG_LIMITER_STRING "LogLimiter"
41 /* Define options possible values */
42 #define ALLOW_STRING "allow"
43 #define DENY_STRING "deny"
44 #define ON_STRING "on"
45 #define OFF_STRING "off"
46 #define isYES(c) (c == '1')
47 #define isNO(c) (c == '0')
50 static int log_config_multiplex_opt(char* opt_str, char* val_str, int prio,
51 struct log_config* config)
55 /* There are only two ways to interpret the lines, so make here a short circuit */
56 if (0 < prio) { /* Is it a rule or an option ? */
57 /* For the filtering rule ... */
59 if (!strncasecmp(ALLOW_STRING, val_str, sizeof(ALLOW_STRING))) {
60 value = __LOG_LIMITER_LIMIT_MAX + 1;
61 } else if (!strncasecmp(DENY_STRING, val_str,
62 sizeof(DENY_STRING))) {
67 value = strtoul(val_str, &endptr, 0);
68 if (*endptr != '\0') {
73 return __log_limiter_add_rule(opt_str, prio, value);
75 } else { /* It's an option then */
76 if (isYES(*val_str)) {
78 } else if (isNO(*val_str)) {
80 } else if (!strncasecmp(ON_STRING, val_str,
83 } else if (!strncasecmp(OFF_STRING, val_str,
84 sizeof(OFF_STRING))) {
90 if (!strncasecmp(LOG_PLATFORM_STRING, opt_str,
91 sizeof(LOG_PLATFORM_STRING))) {
92 config->lc_plog = value;
93 } else if (!strncasecmp(LOG_LIMITER_STRING, opt_str,
94 sizeof(LOG_LIMITER_STRING))) {
95 config->lc_limiter = value;
104 /* Function returns 0 for success or -1 when error occurred */
105 int __log_config_read(const char* config_file, struct log_config* config)
107 FILE* fconfig = NULL;
108 char buf[CONFIG_LINE_MAX_LEN];
109 char opt[CONFIG_OPTION_MAX_LEN];
110 char opt_value[CONFIG_OPTION_MAX_LEN];
115 if (NULL == config_file || NULL == config) {
119 if (NULL == (fconfig = fopen(config_file, "r"))) {
124 memset(buf, 0, CONFIG_LINE_MAX_LEN);
126 if (NULL == fgets(buf, CONFIG_LINE_MAX_LEN, fconfig)) {
133 /* We ignore comments and blank lines */
134 if (isCOMMENT(*buf) || isNEWLINE(*buf)) {
138 memset(opt, 0, sizeof(opt));
139 memset(opt_value, 0, sizeof(opt_value));
141 /* Read configure line, sscanf() should return two tokens,
142 * even for tag filtering rule */
143 ret = sscanf(buf, "%[A-z0-9-]\t%[A-z0-9]",
145 if (ret != 2) { /* The line is malformed ? */
147 /* This could be rule with space inside TAG */
148 ret = sscanf(buf, "\"%[]A-z0-9*\x20_+:;/-]\"\t|\t%c\t%[A-z0-9]",
157 if (0 > log_config_multiplex_opt(opt, opt_value, prio, config)) {
166 /* These actions should warranty that
167 we cleanly handle initialization errors */
169 /* Clean rules list to prevent log limiter initialization,
170 if configuration error occured. */
171 __log_limiter_rules_purge();