Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate log messages into syslog according to zowe.sysMessages #93

Merged
merged 14 commits into from
Aug 29, 2023
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 79 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "configmgr.h"
#include "logging.h"
#include "stcbase.h"
#include "zos.h"

extern char ** environ;
/*
Expand Down Expand Up @@ -162,21 +163,88 @@ struct {
char parm_member[8+1];
char *root_dir;
char *workspace_dir;

JsonArray *sys_messages;
char ha_instance_id[64];

pid_t pid;
char userid[9];

} zl_context = {.config = {.debug_mode = false}, .userid = "(NONE)"} ;

static void set_sys_messages(ConfigManager *configmgr) {
Json *env;
int cfgGetStatus = cfgGetAnyC(configmgr, ZOWE_CONFIG_NAME, &env, 2, "zowe", "sysMessages");

if (cfgGetStatus != ZCFG_SUCCESS) { // No sysMessages found in Zowe configuration
return;
}
JsonArray *sys_messages = jsonAsArray(env);

int count = jsonArrayGetCount(sys_messages);
printf("\nWHATS OUR SYSMSG COUNT %d?", count);
for (int i = 0; i < count; i++) {
char *sys_message = jsonArrayGetString(sys_messages, i);
printf("SYS MESSAGE HERE: %s\n", sys_message);
}

if (sys_messages) {
zl_context.sys_messages = sys_messages;
}
}

static void check_for_and_print_sys_message(const char* fmt, ...) {

if (!zl_context.sys_messages) {
return;
}

/* All of this stuff here is because I can't do
#define INFO(fmt, ...) check_for_and_print_sys_message(fmt, ...) so let's make a string */
char input_string[1024];
va_list args;
va_start(args, fmt);
vsnprintf(input_string, sizeof(input_string), fmt, args);
va_end(args);

printf("\nIS THIS EVEN FORMATTED %s ", input_string);

// Extract the ID from input_string
char msg_id[256]; // assuming the ID will not exceed 255 characters
const char* spacePos = strchr(input_string, ' ');
if (spacePos) {
int length = spacePos - input_string;
strncpy(msg_id, input_string, length);
msg_id[length] = '\0';
} else {
// If no space found, use the whole input_string as the ID
//strncpy(msg_id, input_string, sizeof(msg_id) - 1);
//msg_id[sizeof(msg_id) - 1] = '\0'; // ensure null termination

// If no space found, end
return;
}

int count = jsonArrayGetCount(zl_context.sys_messages);
for (int i = 0; i < count; i++) {
const char *sys_message_id = jsonArrayGetString(zl_context.sys_messages, i);
if (sys_message_id && strstr(msg_id, sys_message_id)) { // TODO: Maybe this should compare the whole formatted string?
printf("\nMATCH - SYS MESSAGE ID: |%s| MESSAGE ID FROM EXTRACTED OUTPUT |%s|", sys_message_id, msg_id);
wtoPrintfMetal(input_string); // Print our match to the syslog
break;
}
}

}

#define INFO(fmt, ...) printf("%s <%s:%d> %s INFO "fmt, gettime().value, COMP_ID, zl_context.pid, zl_context.userid, ##__VA_ARGS__)
#define WARN(fmt, ...) printf("%s <%s:%d> %s WARN "fmt, gettime().value, COMP_ID, zl_context.pid, zl_context.userid, ##__VA_ARGS__)
#define DEBUG(fmt, ...) if (zl_context.config.debug_mode) \
#define INFO(fmt, ...) check_for_and_print_sys_message(fmt, ##__VA_ARGS__); \
printf("%s <%s:%d> %s INFO "fmt, gettime().value, COMP_ID, zl_context.pid, zl_context.userid, ##__VA_ARGS__)
#define WARN(fmt, ...) check_for_and_print_sys_message(fmt, ##__VA_ARGS__); \
printf("%s <%s:%d> %s WARN "fmt, gettime().value, COMP_ID, zl_context.pid, zl_context.userid, ##__VA_ARGS__)
#define DEBUG(fmt, ...) check_for_and_print_sys_message(fmt, ##__VA_ARGS__); \
DivergentEuropeans marked this conversation as resolved.
Show resolved Hide resolved
if (zl_context.config.debug_mode) \
printf("%s <%s:%d> %s DEBUG "fmt, gettime().value, COMP_ID, zl_context.pid, zl_context.userid, ##__VA_ARGS__)
#define ERROR(fmt, ...) printf("%s <%s:%d> %s ERROR "fmt, gettime().value, COMP_ID, zl_context.pid, zl_context.userid, ##__VA_ARGS__)
#define ERROR(fmt, ...) check_for_and_print_sys_message(fmt, ##__VA_ARGS__); \
printf("%s <%s:%d> %s ERROR "fmt, gettime().value, COMP_ID, zl_context.pid, zl_context.userid, ##__VA_ARGS__)

static int mkdir_all(const char *path, mode_t mode) {
// test if path exists
Expand Down Expand Up @@ -848,6 +916,7 @@ static int start_component(zl_comp_t *comp) {
comp->clean_stop = false;

INFO(MSG_COMP_STARTED, comp->name);
wtoPrintfMetal("SYSLOG POSSIBLY????????????????????");

if (pthread_create(&comp->comm_thid, NULL, handle_comp_comm, comp) != 0) {
DEBUG("comm thread not started for %s - %s\n", comp->name, strerror(errno));
Expand Down Expand Up @@ -1596,6 +1665,10 @@ int main(int argc, char **argv) {
if (process_root_dir(configmgr)) {
exit(EXIT_FAILURE);
}

/* TODO(?): sys_messages could be set earlier than this w/ known sysMessages w/o having
DivergentEuropeans marked this conversation as resolved.
Show resolved Hide resolved
config manager available to check zowe.yaml? */
set_sys_messages(configmgr);

//got root dir, can now load up the schemas from it
char schemaList[PATH_MAX*2 + 4] = {0};
Expand All @@ -1610,6 +1683,7 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE);
}


set_shared_uss_env(configmgr);

if (process_workspace_dir(configmgr)) {
Expand Down