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 11 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
78 changes: 73 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,79 @@ 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);

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

#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) \
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);

/* Uncomment code to try to pull ID from 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(input_string, sys_message_id)) {
wtoPrintf3(input_string); // Print our match to the syslog
break;
}
}

}

#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 @@ -1461,6 +1520,7 @@ static int process_workspace_dir(ConfigManager *configmgr) {

static void print_line(void *data, const char *line) {
printf("%s", line);
check_for_and_print_sys_message(line);
}

static char* get_start_prepare_cmd(char *sharedenv) {
Expand Down Expand Up @@ -1560,13 +1620,15 @@ int main(int argc, char **argv) {
}

INFO(MSG_LAUNCHER_START);
wtoPrintf3(MSG_LAUNCHER_START); // Manual sys log print (messages not set here yet)

zl_config_t config = read_config(argc, argv);
zl_context.config = config;

LoggingContext *logContext = makeLoggingContext();
if (!logContext) {
ERROR(MSG_NO_LOG_CONTEXT);
wtoPrintf3(MSG_NO_LOG_CONTEXT); // Manual sys log print (messages not set here yet)
exit(EXIT_FAILURE);
}
logConfigureStandardDestinations(logContext);
Expand All @@ -1577,6 +1639,7 @@ int main(int argc, char **argv) {
cfgSetTraceLevel(configmgr, zl_context.config.debug_mode ? 2 : 0);
if (init_context(argc, argv, &config, configmgr)) {
ERROR(MSG_CTX_INIT_FAILED);
wtoPrintf3(MSG_CTX_INIT_FAILED); // Manual sys log print (messages not set here yet)
exit(EXIT_FAILURE);
}

Expand All @@ -1588,17 +1651,21 @@ int main(int argc, char **argv) {

if (cfgLoadConfiguration(configmgr, ZOWE_CONFIG_NAME) != 0){
ERROR(MSG_CFG_LOAD_FAIL);
wtoPrintf3(MSG_CFG_LOAD_FAIL); // Manual sys log print (messages not set here yet)
exit(EXIT_FAILURE);
}

if (setup_signal_handlers()) {
ERROR(MSG_SIGNAL_ERR);
wtoPrintf3(MSG_SIGNAL_ERR); // Manual sys log print (messages not set here yet)
exit(EXIT_FAILURE);
}

if (process_root_dir(configmgr)) {
exit(EXIT_FAILURE);
}

set_sys_messages(configmgr);

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


set_shared_uss_env(configmgr);

if (process_workspace_dir(configmgr)) {
Expand Down
Loading