/** Writes a message to the log file. @param type The type of message to log @param fileName The filename where this function was called from. @param lineNum The line number in the file where this function was called from. @param funcName The name of the function which called this function. @param msg The message to log */ void msg_details(msg_type type, const char *fileName, int lineNum, const char *funcName, const char *msg, ...) { msg_init(); /* Construct a string for the user's message */ char msgbuf[1024]; va_list args; va_start(args, msg); vsnprintf(msgbuf, 1024, msg, args); va_end(args); /* Remove any newlines at the end of the message. */ int msgbufidx = strlen(msgbuf)-1; while(msgbuf[msgbufidx] == '\n') { msgbuf[msgbufidx] = '\0'; msgbufidx--; } /* info to prepend to message printed to console */ char typestr[1024]; msg_type_string(type, typestr, 1024); /* Determine the stream that we are going to print out to: stdout, * stderr, or don't print to console */ FILE *stream = stdout; if(type == ERROR || type == FATAL) stream = stderr; if(msg_show_type(type) == 0) stream = NULL; char timestamp[1024]; msg_timestamp(timestamp, 1024); char *fileNameCopy = strdup(fileName); char *shortFileName = basename(fileNameCopy); /* Print the message to stderr or stdout */ if(stream) { msg_start_color(type, stream); /* Print additional details to console for fatal errors */ if(type == FATAL) { fprintf(stream, "%s %s\n", typestr, msgbuf); fprintf(stream, "%s Occurred at %s:%d in the function %s()\n", typestr, shortFileName, lineNum, funcName); } else fprintf(stream, "%s %s\n", typestr, msgbuf); msg_end_color(type, stream); } // Not using funcName to try to keep log shorter. fprintf(f, "%s%s %12s:%-4d %s\n", typestr, timestamp, shortFileName, lineNum, msgbuf); free(fileNameCopy); /* Ensure messages are written to the file or console. */ fflush(stream); fflush(f); }
/** Writes a message to the log file. @param type The type of message to log @param fileName The filename where this function was called from. @param lineNum The line number in the file where this function was called from. @param funcName The name of the function which called this function. @param msg The message to log */ void msg_details(msg_type type, const char *fileName, int lineNum, const char *funcName, const char *msg, ...) { msg_init(); /* Construct a string for the user's message */ char msgbuf[1024]; va_list args; va_start(args, msg); vsnprintf(msgbuf, 1024, msg, args); va_end(args); /* Remove any newlines at the end of the message. */ int msgbufidx = strlen(msgbuf)-1; while(msgbuf[msgbufidx] == '\n') { msgbuf[msgbufidx] = '\0'; msgbufidx--; } /* info to prepend to message printed to console */ char typestr[1024]; msg_type_string(type, typestr, 1024); /* Determine the stream that we are going to print out to: stdout, * stderr, or don't print to console */ FILE *stream = stdout; if(type == ERROR || type == FATAL) stream = stderr; if(msg_show_type(type) == 0) stream = NULL; char timestamp[1024]; msg_timestamp(timestamp, 1024); char *fileNameCopy = strdup(fileName); char *shortFileName = basename(fileNameCopy); /* Print the message to stderr or stdout */ if(stream) { // If using a non-standard logfile name, prepend the name to // the message. This makes it easier to distinguish between // which process is creating which message if there are // multiple programs running at once. char prepend[1024]; if(strcmp(logfile, "log.txt") == 0) prepend[0] = '\0'; else snprintf(prepend, 1024, "(%s) ", logfile); msg_start_color(type, stream); /* Print additional details to console for fatal errors */ if(type == FATAL) { fprintf(stream, "%s %s%s\n", typestr, prepend, msgbuf); fprintf(stream, "%s %sOccurred at %s:%d in the function %s()\n", typestr, prepend, shortFileName, lineNum, funcName); } else fprintf(stream, "%s %s%s\n", typestr, prepend, msgbuf); msg_end_color(type, stream); } // Not using funcName to try to keep log shorter. fprintf(f, "%s%s %12s:%-4d %s\n", typestr, timestamp, shortFileName, lineNum, msgbuf); free(fileNameCopy); /* Ensure messages are written to the file or console. */ fflush(stream); fflush(f); }