Add log::type for 'json' (work in progress)

This commit is contained in:
Bram Matthys 2021-07-12 17:23:27 +02:00
parent f2eef5caca
commit b871d2a177
No known key found for this signature in database
GPG key ID: BF8116B163EAAE98
4 changed files with 76 additions and 12 deletions

View file

@ -1080,6 +1080,8 @@ extern FloodSettings *get_floodsettings_for_user(Client *client, FloodOption opt
extern MODVAR char *floodoption_names[];
extern void flood_limit_exceeded_log(Client *client, char *floodname);
/* logging */
extern LogType log_type_stringtoval(char *str);
extern char *log_type_valtostring(LogType v);
#ifdef DEBUGMODE
#define unreal_log(...) do_unreal_log(__VA_ARGS__, log_data_source(__FILE__, __LINE__, __FUNCTION__), NULL)
#else

View file

@ -241,6 +241,13 @@ typedef enum LogLevel {
ULOG_FATAL = 3
} LogLevel;
/** Logging types (text, json, etc) */
typedef enum LogType {
LOG_TYPE_INVALID = 0,
LOG_TYPE_TEXT = 1,
LOG_TYPE_JSON = 2,
} LogType;
/*
** 'offsetof' is defined in ANSI-C. The following definition
** is not absolutely portable (I have been told), but so far
@ -1794,12 +1801,13 @@ struct ConfigItem_allow_dcc {
struct ConfigItem_log {
ConfigItem_log *prev, *next;
ConfigFlag flag;
char *file; /**< Filename to log to (either generated or specified) */
char *filefmt; /**< Filename with dynamic % stuff */
long maxsize;
int flags;
int logfd;
ConfigFlag flag; /**< Eh, is this used? */
LogType type; /**< Log type, eg text or json */
char *file; /**< Filename to log to (either generated or specified) */
char *filefmt; /**< Filename with dynamic % stuff */
long maxsize; /**< Maximum file size to allow, after which we rotate logs */
int flags; /**< What to log */
int logfd; /**< The file descriptor (internal) */
};
struct ConfigItem_unknown {

View file

@ -6302,6 +6302,10 @@ int _conf_log(ConfigFile *conf, ConfigEntry *ce)
{
ca->maxsize = config_checkval(cep->ce_vardata,CFG_SIZE);
}
else if (!strcmp(cep->ce_varname, "type"))
{
ca->type = log_type_stringtoval(cep->ce_vardata);
}
else if (!strcmp(cep->ce_varname, "flags"))
{
for (cepp = cep->ce_entries; cepp; cepp = cepp->ce_next)
@ -6316,7 +6320,8 @@ int _conf_log(ConfigFile *conf, ConfigEntry *ce)
}
int _test_log(ConfigFile *conf, ConfigEntry *ce) {
int _test_log(ConfigFile *conf, ConfigEntry *ce)
{
int fd, errors = 0;
ConfigEntry *cep, *cepp;
char has_flags = 0, has_maxsize = 0;
@ -6383,6 +6388,23 @@ int _test_log(ConfigFile *conf, ConfigEntry *ce) {
errors++;
}
}
else if (!strcmp(cep->ce_varname, "type"))
{
if (!cep->ce_vardata)
{
config_error_empty(cep->ce_fileptr->cf_filename,
cep->ce_varlinenum, "log", cep->ce_varname);
errors++;
continue;
}
if (!log_type_stringtoval(cep->ce_vardata))
{
config_error("%s:%i: unknown log type '%s'",
cep->ce_fileptr->cf_filename, cep->ce_varlinenum,
cep->ce_vardata);
errors++;
}
}
else
{
config_error_unknown(cep->ce_fileptr->cf_filename, cep->ce_varlinenum,

View file

@ -26,6 +26,28 @@
#include "unrealircd.h"
LogType log_type_stringtoval(char *str)
{
if (!strcmp(str, "json"))
return LOG_TYPE_JSON;
if (!strcmp(str, "text"))
return LOG_TYPE_TEXT;
return LOG_TYPE_INVALID;
}
char *log_type_valtostring(LogType v)
{
switch(v)
{
case LOG_TYPE_TEXT:
return "text";
case LOG_TYPE_JSON:
return "json";
default:
return "???";
}
}
// TODO: validate that all 'key' values are lowercase+underscore+digits in all functions below.
void json_expand_client(json_t *j, char *key, Client *client, int detail)
@ -244,13 +266,21 @@ literal:
*o = '\0';
}
/** Do the actual writing to log files */
void do_unreal_log_loggers(LogLevel loglevel, char *subsystem, char *event_id, char *msg, char *json_serialized)
{
ircd_log(LOG_ERROR, "STD-STR: %s %s %s %s", loglevel_to_string(loglevel), subsystem, event_id, msg);
ircd_log(LOG_ERROR, "JSON-STR: %s", json_serialized);
}
/* Logging function, called by the unreal_log() macro. */
void do_unreal_log(LogLevel loglevel, char *subsystem, char *event_id,
Client *client,
char *msg, ...)
{
va_list vl;
LogData *d;
char *str;
char *json_serialized;
json_t *j = NULL;
json_t *j_details = NULL;
char msgbuf[1024];
@ -308,12 +338,14 @@ void do_unreal_log(LogLevel loglevel, char *subsystem, char *event_id,
/* Now merge the details into root object 'j': */
json_object_update_missing(j, j_details);
/* Generate the JSON */
json_serialized = json_dumps(j, 0);
str = json_dumps(j, 0);
ircd_log(LOG_ERROR, "STD-STR: %s %s %s %s", loglevel_to_string(loglevel), subsystem, event_id, msgbuf);
ircd_log(LOG_ERROR, "JSON-STR: %s", str);
free(str);
/* Now call the actual loggers */
do_unreal_log_loggers(loglevel, subsystem, event_id, msgbuf, json_serialized);
/* Free everything */
safe_free(json_serialized);
json_decref(j_details);
json_decref(j);
}