本文整理汇总了C++中snprintfz函数的典型用法代码示例。如果您正苦于以下问题:C++ snprintfz函数的具体用法?C++ snprintfz怎么用?C++ snprintfz使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snprintfz函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: calculated_number_fabs
static inline char *format_value_with_precision_and_unit(char *value_string, size_t value_string_len, calculated_number value, const char *units, int precision) {
if(unlikely(isnan(value) || isinf(value)))
value = 0.0;
char *separator = "";
if(unlikely(isalnum(*units)))
separator = " ";
if(precision < 0) {
int len, lstop = 0, trim_zeros = 1;
calculated_number abs = value;
if(isless(value, 0)) {
lstop = 1;
abs = calculated_number_fabs(value);
}
if(isgreaterequal(abs, 1000)) {
len = snprintfz(value_string, value_string_len, "%0.0" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
trim_zeros = 0;
}
else if(isgreaterequal(abs, 10)) len = snprintfz(value_string, value_string_len, "%0.1" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
else if(isgreaterequal(abs, 1)) len = snprintfz(value_string, value_string_len, "%0.2" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, value_string_len, "%0.2" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
else if(isgreaterequal(abs, 0.01)) len = snprintfz(value_string, value_string_len, "%0.4" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
else if(isgreaterequal(abs, 0.001)) len = snprintfz(value_string, value_string_len, "%0.5" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
else if(isgreaterequal(abs, 0.0001)) len = snprintfz(value_string, value_string_len, "%0.6" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
else len = snprintfz(value_string, value_string_len, "%0.7" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE) value);
if(unlikely(trim_zeros)) {
int l;
// remove trailing zeros from the decimal part
for(l = len - 1; l > lstop; l--) {
if(likely(value_string[l] == '0')) {
value_string[l] = '\0';
len--;
}
else if(unlikely(value_string[l] == '.')) {
value_string[l] = '\0';
len--;
break;
}
else
break;
}
}
if(unlikely(len <= 0)) len = 1;
snprintfz(&value_string[len], value_string_len - len, "%s%s", separator, units);
}
else {
if(precision > 50) precision = 50;
snprintfz(value_string, value_string_len, "%0.*" LONG_DOUBLE_MODIFIER "%s%s", precision, (LONG_DOUBLE) value, separator, units);
}
return value_string;
}
示例2: read_cgroup_plugin_configuration
void read_cgroup_plugin_configuration() {
cgroup_check_for_new_every = config_get_number("plugin:cgroups", "check for new cgroups every", cgroup_check_for_new_every);
cgroup_enable_cpuacct_stat = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct stat", cgroup_enable_cpuacct_stat);
cgroup_enable_cpuacct_usage = config_get_boolean_ondemand("plugin:cgroups", "enable cpuacct usage", cgroup_enable_cpuacct_usage);
cgroup_enable_memory = config_get_boolean_ondemand("plugin:cgroups", "enable memory", cgroup_enable_memory);
cgroup_enable_blkio = config_get_boolean_ondemand("plugin:cgroups", "enable blkio", cgroup_enable_blkio);
char filename[FILENAME_MAX + 1], *s;
struct mountinfo *mi, *root = mountinfo_read();
mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "cpuacct");
if(!mi) mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "cpuacct");
if(!mi) {
error("Cannot find cgroup cpuacct mountinfo. Assuming default: /sys/fs/cgroup/cpuacct");
s = "/sys/fs/cgroup/cpuacct";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
cgroup_cpuacct_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/cpuacct", filename);
mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "blkio");
if(!mi) mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "blkio");
if(!mi) {
error("Cannot find cgroup blkio mountinfo. Assuming default: /sys/fs/cgroup/blkio");
s = "/sys/fs/cgroup/blkio";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
cgroup_blkio_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/blkio", filename);
mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "memory");
if(!mi) mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "memory");
if(!mi) {
error("Cannot find cgroup memory mountinfo. Assuming default: /sys/fs/cgroup/memory");
s = "/sys/fs/cgroup/memory";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
cgroup_memory_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/memory", filename);
mi = mountinfo_find_by_filesystem_mount_source(root, "cgroup", "devices");
if(!mi) mi = mountinfo_find_by_filesystem_super_option(root, "cgroup", "devices");
if(!mi) {
error("Cannot find cgroup devices mountinfo. Assuming default: /sys/fs/cgroup/devices");
s = "/sys/fs/cgroup/devices";
}
else s = mi->mount_point;
snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, s);
cgroup_devices_base = config_get("plugin:cgroups", "path to /sys/fs/cgroup/devices", filename);
cgroup_root_max = config_get_number("plugin:cgroups", "max cgroups to allow", cgroup_root_max);
cgroup_max_depth = config_get_number("plugin:cgroups", "max cgroups depth to monitor", cgroup_max_depth);
cgroup_enable_new_cgroups_detected_at_runtime = config_get_boolean("plugin:cgroups", "enable new cgroups detected at run time", cgroup_enable_new_cgroups_detected_at_runtime);
mountinfo_free(root);
}
示例3: registry_set_cookie
static void registry_set_cookie(struct web_client *w, const char *guid) {
char edate[100];
time_t et = now_realtime_sec() + registry.persons_expiration;
struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf);
strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm);
snprintfz(w->cookie1, NETDATA_WEB_REQUEST_COOKIE_SIZE, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", guid, edate);
if(registry.registry_domain && registry.registry_domain[0])
snprintfz(w->cookie2, NETDATA_WEB_REQUEST_COOKIE_SIZE, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", guid, registry.registry_domain, edate);
}
示例4: config_get
char *rrdset_cache_dir(const char *id)
{
char *ret = NULL;
static char *cache_dir = NULL;
if(!cache_dir) {
cache_dir = config_get("global", "cache directory", CACHE_DIR);
int r = mkdir(cache_dir, 0755);
if(r != 0 && errno != EEXIST)
error("Cannot create directory '%s'", cache_dir);
}
char b[FILENAME_MAX + 1];
char n[FILENAME_MAX + 1];
rrdset_strncpyz_name(b, id, FILENAME_MAX);
snprintfz(n, FILENAME_MAX, "%s/%s", cache_dir, b);
ret = config_get(id, "cache directory", n);
if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
int r = mkdir(ret, 0775);
if(r != 0 && errno != EEXIST)
error("Cannot create directory '%s'", ret);
}
return ret;
}
示例5: do_proc_sys_kernel_random_entropy_avail
int do_proc_sys_kernel_random_entropy_avail(int update_every, usec_t dt) {
(void)dt;
static procfile *ff = NULL;
if(unlikely(!ff)) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/sys/kernel/random/entropy_avail");
ff = procfile_open(config_get("plugin:proc:/proc/sys/kernel/random/entropy_avail", "filename to monitor", filename), "", PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) return 1;
}
ff = procfile_readall(ff);
if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
unsigned long long entropy = str2ull(procfile_lineword(ff, 0, 0));
RRDSET *st = rrdset_find_bytype("system", "entropy");
if(unlikely(!st)) {
st = rrdset_create("system", "entropy", NULL, "entropy", NULL, "Available Entropy", "entropy", 1000, update_every, RRDSET_TYPE_LINE);
rrddim_add(st, "entropy", NULL, 1, 1, RRDDIM_ABSOLUTE);
}
else rrdset_next(st);
rrddim_set(st, "entropy", entropy);
rrdset_done(st);
return 0;
}
示例6: savememory
int savememory(const char *filename, void *mem, size_t size) {
char tmpfilename[FILENAME_MAX + 1];
snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
if (fd < 0) {
error("Cannot create/open file '%s'.", filename);
return -1;
}
if (write(fd, mem, size) != (ssize_t) size) {
error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
close(fd);
return -1;
}
close(fd);
if (rename(tmpfilename, filename)) {
error("Cannot rename '%s' to '%s'", tmpfilename, filename);
return -1;
}
return 0;
}
示例7: rrddim_set_name
void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
{
debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
char varname[CONFIG_MAX_NAME + 1];
snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
config_set_default(st->id, varname, name);
}
示例8: log_init
void log_init(void) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/debug.log", netdata_configured_log_dir);
stdout_filename = config_get(CONFIG_SECTION_GLOBAL, "debug log", filename);
snprintfz(filename, FILENAME_MAX, "%s/error.log", netdata_configured_log_dir);
stderr_filename = config_get(CONFIG_SECTION_GLOBAL, "error log", filename);
snprintfz(filename, FILENAME_MAX, "%s/access.log", netdata_configured_log_dir);
stdaccess_filename = config_get(CONFIG_SECTION_GLOBAL, "access log", filename);
error_log_throttle_period_backup =
error_log_throttle_period = config_get_number(CONFIG_SECTION_GLOBAL, "errors flood protection period", error_log_throttle_period);
error_log_errors_per_period = (unsigned long)config_get_number(CONFIG_SECTION_GLOBAL, "errors to trigger flood protection", (long long int)error_log_errors_per_period);
setenv("NETDATA_ERRORS_THROTTLE_PERIOD", config_get(CONFIG_SECTION_GLOBAL, "errors flood protection period" , ""), 1);
setenv("NETDATA_ERRORS_PER_PERIOD", config_get(CONFIG_SECTION_GLOBAL, "errors to trigger flood protection", ""), 1);
}
示例9: get_system_cpus
long get_system_cpus(void) {
processors = 1;
#ifdef __APPLE__
int32_t tmp_processors;
if (unlikely(GETSYSCTL_BY_NAME("hw.logicalcpu", tmp_processors))) {
error("Assuming system has %d processors.", processors);
} else {
processors = tmp_processors;
}
return processors;
#elif __FreeBSD__
int32_t tmp_processors;
if (unlikely(GETSYSCTL_BY_NAME("hw.ncpu", tmp_processors))) {
error("Assuming system has %d processors.", processors);
} else {
processors = tmp_processors;
}
return processors;
#else
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix);
procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
if(!ff) {
error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
return processors;
}
ff = procfile_readall(ff);
if(!ff) {
error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
return processors;
}
processors = 0;
unsigned int i;
for(i = 0; i < procfile_lines(ff); i++) {
if(!procfile_linewords(ff, i)) continue;
if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
}
processors--;
if(processors < 1) processors = 1;
procfile_close(ff);
debug(D_SYSTEM, "System has %d processors.", processors);
return processors;
#endif /* __APPLE__, __FreeBSD__ */
}
示例10: find_all_mc
static void find_all_mc() {
char name[FILENAME_MAX + 1];
snprintfz(name, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/edac/mc");
char *dirname = config_get("plugin:proc:/sys/devices/system/edac/mc", "directory to monitor", name);
DIR *dir = opendir(dirname);
if(unlikely(!dir)) {
error("Cannot read ECC memory errors directory '%s'", dirname);
return;
}
struct dirent *de = NULL;
while((de = readdir(dir))) {
if(de->d_type == DT_DIR && de->d_name[0] == 'm' && de->d_name[1] == 'c' && isdigit(de->d_name[2])) {
struct mc *m = callocz(1, sizeof(struct mc));
m->name = strdupz(de->d_name);
struct stat st;
snprintfz(name, FILENAME_MAX, "%s/%s/ce_count", dirname, de->d_name);
if(stat(name, &st) != -1)
m->ce_count_filename = strdupz(name);
snprintfz(name, FILENAME_MAX, "%s/%s/ue_count", dirname, de->d_name);
if(stat(name, &st) != -1)
m->ue_count_filename = strdupz(name);
if(!m->ce_count_filename && !m->ue_count_filename) {
freez(m->name);
freez(m);
}
else {
m->next = mc_root;
mc_root = m;
}
}
}
closedir(dir);
}
示例11: rrdsetvar_create_variables
static inline void rrdsetvar_create_variables(RRDSETVAR *rs) {
RRDSET *st = rs->rrdset;
RRDHOST *host = st->rrdhost;
RRDVAR_OPTIONS options = rs->options;
if(rs->options & RRDVAR_OPTION_ALLOCATED)
options &= ~ RRDVAR_OPTION_ALLOCATED;
// ------------------------------------------------------------------------
// free the old ones (if any)
rrdsetvar_free_variables(rs);
// ------------------------------------------------------------------------
// KEYS
char buffer[RRDVAR_MAX_LENGTH + 1];
snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rs->variable);
rs->key_fullid = strdupz(buffer);
snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rs->variable);
rs->key_fullname = strdupz(buffer);
// ------------------------------------------------------------------------
// CHART
rs->var_local = rrdvar_create_and_index("local", &st->rrdvar_root_index, rs->variable, rs->type, options, rs->value);
// ------------------------------------------------------------------------
// FAMILY
rs->var_family = rrdvar_create_and_index("family", &st->rrdfamily->rrdvar_root_index, rs->key_fullid, rs->type, options, rs->value);
rs->var_family_name = rrdvar_create_and_index("family", &st->rrdfamily->rrdvar_root_index, rs->key_fullname, rs->type, options, rs->value);
// ------------------------------------------------------------------------
// HOST
rs->var_host = rrdvar_create_and_index("host", &host->rrdvar_root_index, rs->key_fullid, rs->type, options, rs->value);
rs->var_host_name = rrdvar_create_and_index("host", &host->rrdvar_root_index, rs->key_fullname, rs->type, options, rs->value);
}
示例12: set_global_environment
void set_global_environment() {
{
char b[16];
snprintfz(b, 15, "%d", default_rrd_update_every);
setenv("NETDATA_UPDATE_EVERY", b, 1);
}
setenv("NETDATA_HOSTNAME" , netdata_configured_hostname, 1);
setenv("NETDATA_CONFIG_DIR" , verify_required_directory(netdata_configured_config_dir), 1);
setenv("NETDATA_PLUGINS_DIR", verify_required_directory(netdata_configured_plugins_dir), 1);
setenv("NETDATA_WEB_DIR" , verify_required_directory(netdata_configured_web_dir), 1);
setenv("NETDATA_CACHE_DIR" , verify_required_directory(netdata_configured_cache_dir), 1);
setenv("NETDATA_LIB_DIR" , verify_required_directory(netdata_configured_varlib_dir), 1);
setenv("NETDATA_LOG_DIR" , verify_required_directory(netdata_configured_log_dir), 1);
setenv("HOME" , verify_required_directory(netdata_configured_home_dir), 1);
setenv("NETDATA_HOST_PREFIX", netdata_configured_host_prefix, 1);
get_system_timezone();
// set the path we need
char path[1024 + 1], *p = getenv("PATH");
if(!p) p = "/bin:/usr/bin";
snprintfz(path, 1024, "%s:%s", p, "/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
setenv("PATH", config_get(CONFIG_SECTION_PLUGINS, "PATH environment variable", path), 1);
// python options
p = getenv("PYTHONPATH");
if(!p) p = "";
setenv("PYTHONPATH", config_get(CONFIG_SECTION_PLUGINS, "PYTHONPATH environment variable", p), 1);
// disable buffering for python plugins
setenv("PYTHONUNBUFFERED", "1", 1);
// switch to standard locale for plugins
setenv("LC_ALL", "C", 1);
}
示例13: rrdset_set_name
void rrdset_set_name(RRDSET *st, const char *name)
{
debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
if(st->name) rrdset_index_del_name(st);
char b[CONFIG_MAX_VALUE + 1];
char n[RRD_ID_LENGTH_MAX + 1];
snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
st->name = config_get(st->id, "name", b);
st->hash_name = simple_hash(st->name);
rrdset_index_add_name(st);
}
示例14: add_listen_socket
static inline int add_listen_socket(int fd, const char *ip, int port) {
if(listen_fds_count >= MAX_LISTEN_FDS) {
error("Too many listening sockets. Failed to add listening socket at ip '%s' port %d", ip, port);
close(fd);
return -1;
}
listen_fds[listen_fds_count] = fd;
char buffer[100 + 1];
snprintfz(buffer, 100, "[%s]:%d", ip, port);
listen_fds_names[listen_fds_count] = strdup(buffer);
listen_fds_count++;
return 0;
}
示例15: cgroup_get_chart_id
void cgroup_get_chart_id(struct cgroup *cg) {
debug(D_CGROUP, "getting the name of cgroup '%s'", cg->id);
pid_t cgroup_pid;
char buffer[CGROUP_CHARTID_LINE_MAX + 1];
snprintfz(buffer, CGROUP_CHARTID_LINE_MAX, "exec %s '%s'",
config_get("plugin:cgroups", "script to get cgroup names", PLUGINS_DIR "/cgroup-name.sh"), cg->chart_id);
debug(D_CGROUP, "executing command '%s' for cgroup '%s'", buffer, cg->id);
FILE *fp = mypopen(buffer, &cgroup_pid);
if(!fp) {
error("CGROUP: Cannot popen(\"%s\", \"r\").", buffer);
return;
}
debug(D_CGROUP, "reading from command '%s' for cgroup '%s'", buffer, cg->id);
char *s = fgets(buffer, CGROUP_CHARTID_LINE_MAX, fp);
debug(D_CGROUP, "closing command for cgroup '%s'", cg->id);
mypclose(fp, cgroup_pid);
debug(D_CGROUP, "closed command for cgroup '%s'", cg->id);
if(s && *s && *s != '\n') {
debug(D_CGROUP, "cgroup '%s' should be renamed to '%s'", cg->id, s);
trim(s);
free(cg->chart_title);
cg->chart_title = strdup(s);
if(!cg->chart_title)
fatal("CGROUP: Cannot allocate memory for chart name of cgroup '%s' chart name: '%s'", cg->id, s);
netdata_fix_chart_name(cg->chart_title);
free(cg->chart_id);
cg->chart_id = strdup(s);
if(!cg->chart_id)
fatal("CGROUP: Cannot allocate memory for chart id of cgroup '%s' chart id: '%s'", cg->id, s);
netdata_fix_chart_id(cg->chart_id);
debug(D_CGROUP, "cgroup '%s' renamed to '%s' (title: '%s')", cg->id, cg->chart_id, cg->chart_title);
}
else debug(D_CGROUP, "cgroup '%s' is not to be renamed (will be shown as '%s')", cg->id, cg->chart_id);
}