2022-07-25 13:15:01 +02:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2022-08-26 16:02:04 +02:00
|
|
|
#include "common/parsing.h"
|
2022-10-18 22:55:28 +08:00
|
|
|
#include "common/bar.h"
|
2022-09-05 10:57:09 +02:00
|
|
|
#include "detection/memory/memory.h"
|
2023-05-11 21:07:04 +08:00
|
|
|
#include "modules/memory/memory.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2022-06-18 14:25:31 +02:00
|
|
|
|
2022-08-26 16:02:04 +02:00
|
|
|
#define FF_MEMORY_NUM_FORMAT_ARGS 3
|
2021-04-15 18:59:26 +02:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintMemory(FFMemoryOptions* options)
|
2022-10-18 23:46:42 +08:00
|
|
|
{
|
2023-05-11 20:39:54 +08:00
|
|
|
FFMemoryResult storage;
|
|
|
|
|
const char* error = ffDetectMemory(&storage);
|
2022-10-18 23:46:42 +08:00
|
|
|
|
2023-05-11 20:39:54 +08:00
|
|
|
if(error)
|
2021-04-15 18:59:26 +02:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
2021-04-15 18:59:26 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
|
2023-06-24 10:59:53 +08:00
|
|
|
ffParseSize(storage.bytesUsed, instance.config.binaryPrefixType, &usedPretty);
|
2022-08-26 16:02:04 +02:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
|
2023-06-24 10:59:53 +08:00
|
|
|
ffParseSize(storage.bytesTotal, instance.config.binaryPrefixType, &totalPretty);
|
2022-08-26 16:02:04 +02:00
|
|
|
|
2023-05-11 20:39:54 +08:00
|
|
|
uint8_t percentage = storage.bytesTotal == 0
|
|
|
|
|
? 0
|
|
|
|
|
: (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0);
|
2022-10-18 23:46:42 +08:00
|
|
|
|
2023-05-11 21:07:04 +08:00
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
2021-03-19 20:57:07 +01:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintLogoAndKey(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor);
|
2023-05-11 20:39:54 +08:00
|
|
|
if (storage.bytesTotal == 0)
|
2022-09-21 15:21:51 +08:00
|
|
|
puts("Disabled");
|
|
|
|
|
else
|
2022-10-18 22:55:28 +08:00
|
|
|
{
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
|
2022-10-18 22:55:28 +08:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
2022-10-18 22:55:28 +08:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffAppendPercentBar(&str, percentage, 0, 5, 8);
|
2022-10-18 22:55:28 +08:00
|
|
|
ffStrbufAppendC(&str, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
if(!(instance.config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
2023-01-12 22:41:11 +08:00
|
|
|
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
|
2022-10-18 22:55:28 +08:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
|
|
|
|
ffAppendPercentNum(&str, (uint8_t) percentage, 50, 80, str.length > 0);
|
2022-10-18 22:55:28 +08:00
|
|
|
|
2023-01-12 22:41:11 +08:00
|
|
|
ffStrbufTrimRight(&str, ' ');
|
2022-10-18 22:55:28 +08:00
|
|
|
ffStrbufPutTo(&str, stdout);
|
|
|
|
|
}
|
2021-03-19 20:57:07 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintFormat(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2022-08-26 16:02:04 +02:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
|
2022-10-18 23:46:42 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_UINT8, &percentage},
|
2021-04-15 18:59:26 +02:00
|
|
|
});
|
2021-03-19 20:57:07 +01:00
|
|
|
}
|
2022-09-08 14:51:31 +02:00
|
|
|
}
|
2023-05-11 21:07:04 +08:00
|
|
|
|
|
|
|
|
void ffInitMemoryOptions(FFMemoryOptions* options)
|
|
|
|
|
{
|
|
|
|
|
options->moduleName = FF_MEMORY_MODULE_NAME;
|
|
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseMemoryCommandOptions(FFMemoryOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_MEMORY_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyMemoryOptions(FFMemoryOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffParseMemoryJsonObject(yyjson_val* module)
|
2023-05-11 21:07:04 +08:00
|
|
|
{
|
|
|
|
|
FFMemoryOptions __attribute__((__cleanup__(ffDestroyMemoryOptions))) options;
|
|
|
|
|
ffInitMemoryOptions(&options);
|
|
|
|
|
|
|
|
|
|
if (module)
|
|
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-05-11 21:07:04 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
2023-06-10 15:01:54 +08:00
|
|
|
continue;
|
|
|
|
|
|
2023-05-11 21:07:04 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_MEMORY_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
2023-05-11 21:07:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintMemory(&options);
|
2023-05-11 21:07:04 +08:00
|
|
|
}
|