Global: use time string when generating JSON result

This commit is contained in:
李通洲
2024-04-09 20:37:43 +08:00
committed by 李通洲
parent 282939f548
commit 691cb4ac24
4 changed files with 58 additions and 30 deletions
+36 -2
View File
@@ -1,12 +1,12 @@
#pragma once
#include <stdint.h>
#include <time.h>
#include <stdio.h>
#ifdef _WIN32
#include <synchapi.h>
#include <profileapi.h>
#include <sysinfoapi.h>
#else
#include <time.h>
#endif
static inline uint64_t ffTimeGetTick() //In msec
@@ -45,3 +45,37 @@ static inline void ffTimeSleep(uint32_t msec)
nanosleep(&(struct timespec){ msec / 1000, (long) (msec % 1000) * 1000000 }, NULL);
#endif
}
#ifdef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#endif
// Not thread-safe
static inline const char* ffTimeToFullStr(uint64_t msec)
{
if (msec == 0) return "";
time_t tsec = (time_t) (msec / 1000);
const struct tm* tm = localtime(&tsec);
static char buf[32];
strftime(buf, __builtin_strlen("0000-00-00T00:00:00") + 1, "%FT%T", tm);
snprintf(buf + __builtin_strlen("0000-00-00T00:00:00"), __builtin_strlen(".000") + 1, ".%03u", (unsigned) (msec % 1000));
strftime(buf + __builtin_strlen("0000-00-00T00:00:00.000"), __builtin_strlen("+0000") + 1, "%z", tm);
return buf;
}
// Not thread-safe
static inline const char* ffTimeToShortStr(uint64_t msec)
{
if (msec == 0) return "";
time_t tsec = (time_t) (msec / 1000);
static char buf[32];
strftime(buf, sizeof(buf), "%F %T", localtime(&tsec));
return buf;
}
#ifdef _WIN32
#pragma GCC diagnostic pop
#endif
+8 -11
View File
@@ -2,12 +2,11 @@
#include "common/jsonconfig.h"
#include "common/parsing.h"
#include "common/percent.h"
#include "common/time.h"
#include "detection/disk/disk.h"
#include "modules/disk/disk.h"
#include "util/stringUtils.h"
#include <time.h>
#define FF_DISK_NUM_FORMAT_ARGS 12
#pragma GCC diagnostic ignored "-Wsign-conversion"
@@ -115,12 +114,6 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk)
bool isHidden = !!(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT);
bool isReadOnly = !!(disk->type & FF_DISK_VOLUME_TYPE_READONLY_BIT);
char buf[64] = "";
if(disk->createTime)
{
time_t time = (time_t) (disk->createTime / 1000);
strftime(buf, sizeof(buf), "%FT%T%z", localtime(&time));
}
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_DISK_NUM_FORMAT_ARGS, ((FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
@@ -133,7 +126,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk)
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem},
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->name},
{FF_FORMAT_ARG_TYPE_BOOL, &isReadOnly},
{FF_FORMAT_ARG_TYPE_STRING, buf},
{FF_FORMAT_ARG_TYPE_STRING, ffTimeToShortStr(disk->createTime)},
}));
}
}
@@ -459,7 +452,11 @@ void ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjso
if(item->type & FF_DISK_VOLUME_TYPE_READONLY_BIT)
yyjson_mut_arr_add_str(doc, typeArr, "Read-only");
yyjson_mut_obj_add_uint(doc, obj, "createTime", item->createTime);
const char* pstr = ffTimeToFullStr(item->createTime);
if (*pstr)
yyjson_mut_obj_add_strcpy(doc, obj, "createTime", pstr);
else
yyjson_mut_obj_add_null(doc, obj, "createTime");
}
FF_LIST_FOR_EACH(FFDisk, item, disks)
@@ -485,7 +482,7 @@ void ffPrintDiskHelpFormat(void)
"Filesystem",
"Label / name",
"True if read-only",
"Create time",
"Create time in local timezone",
}));
}
+5 -6
View File
@@ -1,10 +1,11 @@
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/time.h"
#include "detection/uptime/uptime.h"
#include "modules/uptime/uptime.h"
#include "util/stringUtils.h"
#define FF_UPTIME_NUM_FORMAT_ARGS 7
#define FF_UPTIME_NUM_FORMAT_ARGS 6
void ffPrintUptime(FFUptimeOptions* options)
{
@@ -83,8 +84,7 @@ void ffPrintUptime(FFUptimeOptions* options)
{FF_FORMAT_ARG_TYPE_UINT, &minutes},
{FF_FORMAT_ARG_TYPE_UINT, &seconds},
{FF_FORMAT_ARG_TYPE_UINT, &milliseconds},
{FF_FORMAT_ARG_TYPE_UINT64, &result.uptime},
{FF_FORMAT_ARG_TYPE_UINT64, &result.bootTime},
{FF_FORMAT_ARG_TYPE_STRING, ffTimeToShortStr(result.uptime)},
}));
}
}
@@ -137,7 +137,7 @@ void ffGenerateUptimeJsonResult(FF_MAYBE_UNUSED FFUptimeOptions* options, yyjson
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_uint(doc, obj, "uptime", result.uptime);
yyjson_mut_obj_add_uint(doc, obj, "bootTime", result.bootTime);
yyjson_mut_obj_add_strcpy(doc, obj, "bootTime", ffTimeToFullStr(result.bootTime));
}
void ffPrintUptimeHelpFormat(void)
@@ -148,8 +148,7 @@ void ffPrintUptimeHelpFormat(void)
"Minutes",
"Seconds",
"Milliseconds",
"Uptime in unix timestamp (ms)",
"Boot time in unix timestamp (ms)",
"Boot time in local timezone",
}));
}
+9 -11
View File
@@ -1,11 +1,10 @@
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/time.h"
#include "detection/users/users.h"
#include "modules/users/users.h"
#include "util/stringUtils.h"
#include <time.h>
#pragma GCC diagnostic ignored "-Wformat" // warning: unknown conversion type character 'F' in format
#define FF_USERS_NUM_FORMAT_ARGS 5
@@ -57,12 +56,7 @@ void ffPrintUsers(FFUsersOptions* options)
ffStrbufAppendF(&result, "@%s", user->hostName.chars);
if(user->loginTime)
{
char buf[64];
time_t time = (time_t) (user->loginTime / 1000);
strftime(buf, sizeof(buf), "%FT%T%z", localtime(&time));
ffStrbufAppendF(&result, " - login time %s", buf);
}
ffStrbufAppendF(&result, " - login time %s", ffTimeToShortStr(user->loginTime));
ffStrbufPutTo(&result, stdout);
}
@@ -79,7 +73,7 @@ void ffPrintUsers(FFUsersOptions* options)
{FF_FORMAT_ARG_TYPE_STRBUF, &user->hostName},
{FF_FORMAT_ARG_TYPE_STRBUF, &user->sessionName},
{FF_FORMAT_ARG_TYPE_STRBUF, &user->clientIp},
{FF_FORMAT_ARG_TYPE_UINT64, &user->loginTime},
{FF_FORMAT_ARG_TYPE_STRING, ffTimeToShortStr(user->loginTime)},
}));
}
}
@@ -163,7 +157,11 @@ void ffGenerateUsersJsonResult(FF_MAYBE_UNUSED FFUsersOptions* options, yyjson_m
yyjson_mut_obj_add_strbuf(doc, obj, "hostName", &user->hostName);
yyjson_mut_obj_add_strbuf(doc, obj, "sessionName", &user->sessionName);
yyjson_mut_obj_add_strbuf(doc, obj, "clientIp", &user->clientIp);
yyjson_mut_obj_add_uint(doc, obj, "loginTime", user->loginTime);
const char* pstr = ffTimeToFullStr(user->loginTime);
if (*pstr)
yyjson_mut_obj_add_strcpy(doc, obj, "loginTime", pstr);
else
yyjson_mut_obj_add_null(doc, obj, "loginTime");
}
exit:
@@ -183,7 +181,7 @@ void ffPrintUsersHelpFormat(void)
"Host name",
"Session name",
"Client IP",
"Login Time"
"Login Time in local timezone"
}));
}