bool check_verbose_options(struct oscap_action *action) { if (action->verbosity_level == NULL && action->f_verbose_log != NULL) { oscap_module_usage(action->module, stderr, "Verbosity level is not specified!"); return false; } if (action->verbosity_level != NULL && action->f_verbose_log == NULL) { oscap_module_usage(action->module, stderr, "Log file is not specified!"); return false; } if (action->verbosity_level != NULL && oscap_verbosity_level_from_cstr(action->verbosity_level) == -1) { oscap_module_usage(action->module, stderr, "Inavlid verbosity level!"); return false; } return true; }
bool oscap_set_verbose(const char *verbosity_level, const char *filename, bool is_probe) { if (verbosity_level == NULL) { return true; } __debuglog_level = oscap_verbosity_level_from_cstr(verbosity_level); if (__debuglog_level == DBG_UNKNOWN) { return false; } if (!is_probe) { setenv("OSCAP_PROBE_VERBOSITY_LEVEL", verbosity_level, 1); } if (filename == NULL) { __debuglog_fp = stderr; return true; } int fd; if (is_probe) { fd = open(filename, O_APPEND | O_WRONLY); } else { setenv("OSCAP_PROBE_VERBOSE_LOG_FILE", filename, 1); /* Open a file. If the file doesn't exist, create it. * If the file exists, erase its content. * File is opened in "append" mode. * Append mode is necessary when more processes write to same file. * Every process using the log file must open it in append mode, * because otherwise some data may be missing on output. */ fd = open(filename, O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); } if (fd == -1) { oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to open file %s: %s.", filename, strerror(errno)); return false; } __debuglog_fp = fdopen(fd, "a"); if (__debuglog_fp == NULL) { oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to associate stream with file %s: %s.", filename, strerror(errno)); return false; } setbuf(__debuglog_fp, NULL); atexit(&__oscap_debuglog_close); return true; }