parse-util: add format string macro for outputting permyriad
Let's define a set of macros for making output of permyriad values easy. They are printed in pure ASCII, i.e. without the permille/permyriad suffix, using just percent and two places after the dot.
This commit is contained in:
@@ -59,3 +59,6 @@ static inline int UINT32_SCALE_TO_PERMYRIAD(uint32_t scale) {
|
||||
|
||||
return (int) u;
|
||||
}
|
||||
|
||||
#define PERMYRIAD_AS_PERCENT_FORMAT_STR "%i.%02i%%"
|
||||
#define PERMYRIAD_AS_PERCENT_FORMAT_VAL(x) ((x)/100), ((x)%100)
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "nulstr-util.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "percent-util.h"
|
||||
#include "process-util.h"
|
||||
#include "procfs-util.h"
|
||||
#include "special.h"
|
||||
|
||||
@@ -706,9 +706,9 @@ static int bus_cgroup_set_boolean(
|
||||
assert_se(endswith(name, "Scale")); \
|
||||
\
|
||||
int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \
|
||||
unit_write_settingf(u, flags, name, "%.*s=%i.%02i%%", \
|
||||
unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \
|
||||
(int)(strlen(name) - strlen("Scale")), name, \
|
||||
scaled / 100, scaled % 100); \
|
||||
PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \
|
||||
} \
|
||||
\
|
||||
return 1; \
|
||||
|
||||
Reference in New Issue
Block a user