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

ZSS Logging Improvements #444

Open
wants to merge 17 commits into
base: v2.x/staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
217 changes: 194 additions & 23 deletions c/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>

#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#endif

#include "zowetypes.h"
Expand All @@ -36,6 +38,7 @@
#include "utils.h"
#include "logging.h"
#include "printables_for_dump.h"
#include "zos.h"

#ifdef __ZOWE_OS_ZOS
#include "le.h"
Expand Down Expand Up @@ -457,29 +460,168 @@ LoggingDestination *logConfigureDestination2(LoggingContext *context,
return destination;
}

void printStdout(LoggingContext *context, LoggingComponent *component, void *data, char *formatString, va_list argList){
static char *logLevelToString(int level) {
switch (level) {
case ZOWE_LOG_SEVERE: return "CRITICAL";
case ZOWE_LOG_WARNING: return "WARN";
case ZOWE_LOG_INFO: return "INFO";
case ZOWE_LOG_DEBUG: return "DEBUG";
case ZOWE_LOG_DEBUG2: return "FINER";
case ZOWE_LOG_DEBUG3: return "TRACE";
default: return "";
}
}

typedef struct zl_time_t {
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved
char value[32];
} zl_time_t;

static zl_time_t gettime(void) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With all the non-Metal stuff, the ZIS build fails now:

ERROR CCN3277 ../../deps/zowe-common-c/c/logging.c:490   Syntax error: possible missing ';' or ','?
ERROR CCN3045 ../../deps/zowe-common-c/c/logging.c:490   Undeclared identifier time_t.
ERROR CCN3007 ../../deps/zowe-common-c/c/logging.c:493   "struct tm" is undefined.
ERROR CCN3045 ../../deps/zowe-common-c/c/logging.c:496   Undeclared identifier t.
ERROR CCN3007 ../../deps/zowe-common-c/c/logging.c:500   "struct timeval" is undefined.
ERROR CCN3277 ../../deps/zowe-common-c/c/logging.c:513   Syntax error: possible missing ';' or ','?
ERROR CCN3045 ../../deps/zowe-common-c/c/logging.c:513   Undeclared identifier pthread_t.
ERROR CCN3045 ../../deps/zowe-common-c/c/logging.c:514   Undeclared identifier pThread.
ERROR CCN3277 ../../deps/zowe-common-c/c/logging.c:605   Syntax error: possible missing ')' or ','?
CCN0793(I) Compilation failed for file ../../deps/zowe-common-c/c/logging.c.  Object file not created.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you name this function according to the style of this file? E.g., getCurrentTime.


time_t t = time(NULL);
const char *format = "%Y-%m-%d %H:%M:%S";

struct tm lt;
zl_time_t result;

gmtime_r(&t, &lt);
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved

strftime(result.value, sizeof(result.value), format, &lt);
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved

struct timeval now;
gettimeofday(&now, NULL);
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved
int milli = now.tv_usec / 1000;
snprintf(result.value+strlen(result.value), 5, ".%03d", milli);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 5? I also suggest you check the return value for -1 and at least make sure the buffer is terminated if the call fails.


return result;
}

static char *getServiceName() {
ifakhrutdinov marked this conversation as resolved.
Show resolved Hide resolved
return "ZWESZ1";
}

static void getTaskInformation(char *taskInformation, unsigned int taskInformationSize) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also fine to use size_t as the type for sizes because it's what the C library functions do (I know, you're probably not going to have a buffer larger than 4GB but that's not the only reason).

pthread_t pThread = pthread_self();
snprintf(taskInformation, taskInformationSize, "tcb=0x%p,threadid=0x%p,pid=0x%p", getTCB(), &pThread, getpid());
}

static void getMessage(char *message, unsigned int messageSize, const char *formatString, va_list argList) {
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved
vsnprintf(message, messageSize, formatString, argList);
}

/*
* 1. Try to get the name from the TCB.
* 2. Try to get the name from the login data base.
*/
static void getUserID(char *user, unsigned int userSize) {
TCB *tcb = getTCB();
ACEE *acee = (ACEE *) tcb->tcbsenv;
if (acee) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But no ACEE can also mean no impersonation and there should probably be a job-level ACEE and user in that case. Why have you chosen getlogin_r over that? And will getlogin_r return the proper value if zss is running as an STC?

snprintf(user, userSize, "%.*s", acee->aceeuser[0], acee->aceeuser[1]);
} else {
getlogin_r(user, userSize);
}
}

static void prependMetadata(int logLevel,
char *fullMessage,
unsigned int fullMessageSize,
char *formatString,
va_list argList,
char *fileName,
char *functionName,
unsigned int lineNumber) {
char user[8+1] = {0};
getUserID((char *) &user, sizeof(user) - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function already null-terminates, so this may confuse people.

//char time[64] = {0};
zl_time_t time = {0};
time = gettime();
//getCurrentTime((char *) &time, sizeof(time));
char message[256] = {0};
getMessage((char *) &message, sizeof(message), formatString, argList);
char taskInformation[128] = {0};
getTaskInformation((char *) &taskInformation, sizeof(taskInformation));
if (!fileName || !functionName || lineNumber == 0) {
snprintf(fullMessage,
fullMessageSize,
"%s <%s:%s> %s %s %s",
time,
getServiceName(),
taskInformation,
user,
logLevelToString(logLevel),
message);
} else {
snprintf(fullMessage,
fullMessageSize,
"%s <%s:%s> %s %s (%s:%s:%d) %s",
time.value,
getServiceName(),
taskInformation,
user,
logLevelToString(logLevel),
basename(fileName),
functionName,
lineNumber,
message);
}
}

void printToLog(FILE *destination,
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved
char *formatString,
va_list argList,
int logLevel,
char *fileName,
char *functionName,
unsigned int lineNumber) {
#ifdef METTLE
printf("broken printf in logging.c\n");
#else
vfprintf(stdout,formatString,argList);
#else
char fullMessage[1024] = {0};
prependMetadata(logLevel, (char *) &fullMessage, sizeof(fullMessage), formatString, argList, fileName, functionName, lineNumber);
fprintf(destination, "%s\n", fullMessage);
#endif
}

void printStdout2(LoggingContext *context,
LoggingComponent *component,
void *data,
char *formatString,
va_list argList,
int logLevel,
char *fileName,
char *functionName,
unsigned int lineNumber) {
printToLog(stdout, formatString, argList, logLevel, fileName, functionName, lineNumber);
}

void printStdout(LoggingContext *context, LoggingComponent *component, void *data, char *formatString, va_list argList){
printStdout2(context, component, data, formatString, argList, ZOWE_LOG_NA, "", "", 0);
}

void printStderr2(LoggingContext *context,
LoggingComponent *component,
void *data,
char *formatString,
va_list argList,
int logLevel,
char *fileName,
char *functionName,
unsigned int lineNumber) {
printToLog(stderr, formatString, argList, logLevel, fileName, functionName, lineNumber);
}

void printStderr(LoggingContext *context, LoggingComponent *component, void *data, char *formatString, va_list argList){
#ifdef METTLE
printf("broken printf in logging.c\n");
#else
vfprintf(stderr,formatString,argList);
#endif
printStderr2(context, component, data, formatString, argList, ZOWE_LOG_NA, "", "", 0);
}

void logConfigureStandardDestinations(LoggingContext *context){
if (context == NULL) {
context = getLoggingContext();
}
logConfigureDestination(context,LOG_DEST_DEV_NULL,"/dev/null",NULL,NULL);
logConfigureDestination(context,LOG_DEST_PRINTF_STDOUT,"printf(stdout)",NULL,printStdout);
logConfigureDestination(context,LOG_DEST_PRINTF_STDERR,"printf(stderr)",NULL,printStderr);
logConfigureDestination(context,LOG_DEST_PRINTF_STDOUT,"printf(stdout)",NULL,printStdout2);
logConfigureDestination(context,LOG_DEST_PRINTF_STDERR,"printf(stderr)",NULL,printStderr2);
}

void logConfigureComponent(LoggingContext *context, uint64 compID, char *compName, int destination, int level){
Expand Down Expand Up @@ -649,7 +791,14 @@ int logGetLevel(LoggingContext *context, uint64 compID){
return component ? component->currentDetailLevel : ZOWE_LOG_NA;
}

void zowelog(LoggingContext *context, uint64 compID, int level, char *formatString, ...){
void zowelogInner(LoggingContext *context,
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved
uint64 compID,
int level,
char *fileName,
char *functionName,
unsigned int lineNumber,
char *formatString,
va_list argPointer) {

if (logShouldTrace(context, compID, level) == FALSE) {
return;
Expand All @@ -666,8 +815,6 @@ void zowelog(LoggingContext *context, uint64 compID, int level, char *formatStri
}

if (maxDetailLevel >= level){
va_list argPointer;

LoggingDestination *destination = &getDestinationTable(context, compID)[component->destination];
// printf("log.2 comp.dest=%d\n",component->destination);fflush(stdout);
if (component->destination >= MAX_LOGGING_DESTINATIONS){
Expand All @@ -687,27 +834,44 @@ void zowelog(LoggingContext *context, uint64 compID, int level, char *formatStri
lastResortLog(message);
return;
}
/* here, pass to a var-args handler */
va_start(argPointer, formatString);

destination->handler(context,component,destination->data,formatString,argPointer);

va_end(argPointer);
destination->handler(context,component,destination->data,formatString,argPointer,level,fileName,functionName,lineNumber);
}
}

void zowelog2(LoggingContext *context, uint64 compID, int level, char *fileName, char *functionName, unsigned int lineNumber, char *formatString, ...) {
va_list argPointer;
va_start(argPointer, formatString);
zowelogInner(context, compID, level, fileName, functionName, lineNumber, formatString, argPointer);
va_end(argPointer);
}

void zowelog(LoggingContext *context, uint64 compID, int level, char *formatString, ...){
va_list argPointer;
va_start(argPointer, formatString);
zowelogInner(context, compID, level, "", "", 0, formatString, argPointer);
va_end(argPointer);
}

static void printToDestination(LoggingDestination *destination,
struct LoggingContext_tag *context,
LoggingComponent *component,
int level, char *fileName,
char *functionName, unsigned int lineNumber,
void *data, char *formatString, ...){
va_list argPointer;
va_start(argPointer, formatString);
destination->handler(context,component,destination->data,formatString,argPointer);
destination->handler(context,component,destination->data,formatString,argPointer,level,fileName,functionName,lineNumber);
va_end(argPointer);
}

void zowedump(LoggingContext *context, uint64 compID, int level, void *data, int dataSize){
void zowedumpInner(LoggingContext *context,
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved
uint64 compID,
int level,
void *data,
int dataSize,
char *fileName,
char *functionName,
unsigned int lineNumber) {

if (logShouldTrace(context, compID, level) == FALSE) {
return;
Expand Down Expand Up @@ -748,15 +912,22 @@ void zowedump(LoggingContext *context, uint64 compID, int level, void *data, int
for (int i = 0; ; i++){
char *result = destination->dumper(workBuffer, sizeof(workBuffer), data, dataSize, i);
if (result != NULL){
printToDestination(destination, context, component, destination->data, "%s\n", result);
printToDestination(destination, context, component, level, fileName, functionName, lineNumber, destination->data, "%s\n", result);
}
else {
break;
}
}

}
}

void zowedump2(LoggingContext *context, uint64 compID, int level, void *data, int dataSize, char *fileName, char *functionName, unsigned int lineNumber) {
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved
zowedumpInner(context, compID, level, data, dataSize, fileName, functionName, lineNumber);
}

void zowedump(LoggingContext *context, uint64 compID, int level, void *data, int dataSize){
zowedumpInner(context, compID, level, data, dataSize, "", "", 0);
}

bool logShouldTraceInternal(LoggingContext *context, uint64 componentID, int level) {
Expand Down
33 changes: 32 additions & 1 deletion h/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@
#include "collections.h"
#include "le.h"

#define ZOWELOG_SEVERE($id, $fmt, ...) \
zowelog2(NULL, $id, ZOWE_LOG_SEVERE, \
___FILE__, __FUNCTION__, __LINE__, $fmt, ##__VA_ARGS__)

#define ZOWELOG_WARNING($id, $fmt, ...) \
zowelog2(NULL, $id, ZOWE_LOG_WARNING, \
___FILE__, __FUNCTION__, __LINE__, $fmt, ##__VA_ARGS__)

#define ZOWELOG_INFO($id, $fmt, ...) \
zowelog2(NULL, $id, ZOWE_LOG_INFO, \
___FILE__, __FUNCTION__, __LINE__, $fmt, ##__VA_ARGS__)

#define ZOWELOG_DEBUG($id, $fmt, ...) \
zowelog2(NULL, $id, ZOWE_LOG_DEBUG, \
___FILE__, __FUNCTION__, __LINE__, $fmt, ##__VA_ARGS__)

#define ZOWELOG_DEBUG2($id, $fmt, ...) \
zowelog2(NULL, $id, ZOWE_LOG_DEBUG2, \
___FILE__, __FUNCTION__, __LINE__, $fmt, ##__VA_ARGS__)

#define ZOWELOG_DEBUG3($id, $fmt, ...) \
zowelog2(NULL, $id, ZOWE_LOG_DEBUG3, \
___FILE__, __FUNCTION__, __LINE__, $fmt, ##__VA_ARGS__)

/** \file
* \brief logging.h defines a platform-independent logging facility that echoes some of Java logging.
*
Expand All @@ -46,6 +70,7 @@ ZOWE_PRAGMA_PACK_RESET
#define RC_LOG_ERROR 8

#define ZOWE_LOG_NA -1

#define ZOWE_LOG_SEVERE 0
#define ZOWE_LOG_ALWAYS 0
#define ZOWE_LOG_WARNING 1
Expand Down Expand Up @@ -135,7 +160,11 @@ typedef void (*LogHandler)(struct LoggingContext_tag *context,
LoggingComponent *component,
jordanfilteau1995 marked this conversation as resolved.
Show resolved Hide resolved
void *data,
char *formatString,
va_list argList);
va_list argList,
int logLevel,
char *fileName,
char *functionName,
unsigned int lineNumber);
typedef char *(*DataDumper)(char *workBuffer, int workBufferSize, void *data, int dataSize, int lineNumber);

ZOWE_PRAGMA_PACK
Expand Down Expand Up @@ -369,7 +398,9 @@ extern int logSetExternalContext(LoggingContext *context);
extern LoggingContext *logGetExternalContext();

void printStdout(LoggingContext *context, LoggingComponent *component, void *data, char *formatString, va_list argList);
void printStdout2(LoggingContext *context, LoggingComponent *component, void *data, char *formatString, va_list argList, int logLevel, char *fileName, char *functionName, unsigned int lineNumber);
void printStderr(LoggingContext *context, LoggingComponent *component, void *data, char *formatString, va_list argList);
void printStderr2(LoggingContext *context, LoggingComponent *component, void *data, char *formatString, va_list argList, int logLevel, char *fileName, char *functionName, unsigned int lineNumber);

#endif

Expand Down
Loading