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:
Lennart Poettering
2021-02-18 22:36:34 +01:00
parent 9cba32bcde
commit 1ead0b2a79
3 changed files with 6 additions and 2 deletions
+3
View File
@@ -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)
+1
View File
@@ -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"
+2 -2
View File
@@ -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; \