Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ target_compile_definitions(math_test PUBLIC -DEXACT_MATH_TEST)

if (MYSQL_FOUND)
message(STATUS "mysql found, adding db_conn to exact_common library!")
add_library(exact_common arguments random exp db_conn color_table log files weight_initialize)
add_library(exact_common arguments random exp db_conn color_table log files weight_initialize string_format)
else (MYSQL_FOUND)
add_library(exact_common arguments exp random color_table log files weight_initialize)
add_library(exact_common arguments exp random color_table log files weight_initialize string_format)
endif (MYSQL_FOUND)
243 changes: 63 additions & 180 deletions common/log.cxx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cstdio>
#include <cstring>
using std::fprintf;
using std::printf;
using std::snprintf;
Expand All @@ -17,12 +18,11 @@ using std::thread;
#include "files.hxx"
#include "log.hxx"


using std::cerr;
using std::endl;

int32_t Log::std_message_level = INFO;
int32_t Log::file_message_level = INFO;
log_level_t Log::std_message_level = LOG_LEVEL_INFO;
log_level_t Log::file_message_level = LOG_LEVEL_INFO;
bool Log::write_to_file = true;
int32_t Log::max_header_length = 256;
int32_t Log::max_message_length = 1024;
Expand All @@ -45,23 +45,23 @@ void Log::register_command_line_arguments() {
//CommandLine::
}

int8_t Log::parse_level_from_string(string level) {
log_level_t Log::parse_level_from_string(string level) {
if (level.compare("0") == 0 || level.compare("NONE") == 0|| level.compare("none") == 0) {
return Log::NONE;
return LOG_LEVEL_NONE;
} else if (level.compare("1") == 0 || level.compare("FATAL") == 0 || level.compare("fatal") == 0) {
return Log::FATAL;
return LOG_LEVEL_FATAL;
} else if (level.compare("2") == 0 || level.compare("ERROR") == 0 || level.compare("error") == 0) {
return Log::ERROR;
return LOG_LEVEL_ERROR;
} else if (level.compare("3") == 0 || level.compare("WARNING") == 0 || level.compare("warning") == 0) {
return Log::WARNING;
return LOG_LEVEL_WARNING;
} else if (level.compare("4") == 0 || level.compare("INFO") == 0 || level.compare("info") == 0) {
return Log::INFO;
return LOG_LEVEL_INFO;
} else if (level.compare("5") == 0 || level.compare("DEBUG") == 0 || level.compare("debug") == 0) {
return Log::DEBUG;
return LOG_LEVEL_DEBUG;
} else if (level.compare("6") == 0 || level.compare("TRACE") == 0 || level.compare("trace") == 0) {
return Log::TRACE;
return LOG_LEVEL_TRACE;
} else if (level.compare("7") == 0 || level.compare("ALL") == 0 || level.compare("all") == 0) {
return Log::ALL;
return LOG_LEVEL_ALL;
} else {
cerr << "ERROR: specified an incorrect message level for the Log: '" << level << "'" << endl;
cerr << "Options are:" << endl;
Expand All @@ -77,6 +77,29 @@ int8_t Log::parse_level_from_string(string level) {
}
}

string Log::get_level_str(log_level_t level) {
switch (level) {
case LOG_LEVEL_NONE:
return "NONE";
case LOG_LEVEL_FATAL:
return "FATAL";
case LOG_LEVEL_ERROR:
return "ERROR";
case LOG_LEVEL_WARNING:
return "WARNING";
case LOG_LEVEL_INFO:
return "INFO";
case LOG_LEVEL_DEBUG:
return "DEBUG";
case LOG_LEVEL_TRACE:
return "TRACE";
case LOG_LEVEL_ALL:
return "ALL";
default:
return "<unknown>";
}
}

void Log::initialize(const vector<string> &arguments) {
//TODO: should read these from the CommandLine (to be created)

Expand Down Expand Up @@ -146,11 +169,12 @@ void Log::release_id(string human_readable_id) {
log_ids_mutex.unlock();
}

void Log::log(const char *file, size_t filelen, const char *func, size_t funclen, long line, log_level_t level, const char *format, ...) {

void Log::write_message(bool print_header, int8_t message_level, const char *message_type, const char *format, va_list arguments) {

va_list arguments;
va_start(arguments, format);

thread::id id = std::this_thread::get_id();

if (log_ids.count(id) == 0) {
cerr << "ERROR: could not write message from thread '" << id << "' because it did not have a human readable id assigned (please use the Log::set_id(string) function before writing to the Log on any thread)." << endl;
cerr << "message:" << endl;
Expand All @@ -159,29 +183,32 @@ void Log::write_message(bool print_header, int8_t message_level, const char *mes
exit(1);
}

string human_readable_id = log_ids[id];
// file is relative path to ~/. getting file name here
char filename_buffer[filelen];
strcpy(filename_buffer, file);
char* file_token = strtok(filename_buffer, "/");
char* file_name = file_token;
for (; (file_token = strtok(NULL, "/")) != NULL; file_name = file_token);

//print the message header into a string
char header_buffer[max_header_length];
//we only need to print the header for some messages
if (print_header) {
//snprintf(header_buffer, max_header_length, "[%-8s %-20s]", message_type, human_readable_id.c_str());
snprintf(header_buffer, max_header_length, "[%-7s %-21s]", message_type, human_readable_id.c_str());
}
// TODO: find replace \r\n with ' '. ensure one new line at end of every message

char func_name[funclen];
strcpy(func_name, func);

//print the actual message contents into a string
char message_buffer[max_message_length];
vsnprintf(message_buffer, max_message_length, format, arguments);

if (std_message_level >= message_level) {
if (print_header) {
printf("%s %s", header_buffer, message_buffer);
} else {
printf("%s", message_buffer);
}
string human_readable_id = log_ids[id];
string level_str = Log::get_level_str(level);

string log_str = "";
log_str = string_format("[%s %s]: %s:%ld %s:\t %s", level_str.c_str(), human_readable_id.c_str(), file_name, line, func_name, message_buffer);

if (std_message_level >= level) {
printf("%s", log_str.c_str());
}

if (file_message_level >= message_level) {
if (file_message_level >= level) {
LogFile* log_file = NULL;

//check and see if we've already opened a file for this human readable id, if we haven't
Expand All @@ -199,161 +226,17 @@ void Log::write_message(bool print_header, int8_t message_level, const char *mes
//lock this log_file in case multiple threads are trying to write
//to the same file
log_file->file_mutex.lock();
if (print_header) {
fprintf(log_file->file, "%s %s", header_buffer, message_buffer);
} else {
fprintf(log_file->file, "%s", message_buffer);
}
fprintf(log_file->file, "%s", log_str.c_str());
fflush(log_file->file);
log_file->file_mutex.unlock();
}
}

bool Log::at_level(int8_t level) {
bool Log::at_level(log_level_t level) {
return level >= std_message_level || level >= file_message_level;
}

void Log::fatal(const char *format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < FATAL && file_message_level < FATAL) return;

va_list arguments;
va_start(arguments, format);
write_message(true, FATAL, "FATAL", format, arguments);
bool Log::should_log(log_level_t level) {
return !(std_message_level < level && file_message_level < level)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@travisdesell I'll simplify this

&& !(restricted_rank >= 0 && restricted_rank != process_rank);
}

void Log::error(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < ERROR && file_message_level < ERROR) return;

va_list arguments;
va_start(arguments, format);
write_message(true, ERROR, "ERROR", format, arguments);
}

void Log::warning(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < WARNING && file_message_level < WARNING) return;

va_list arguments;
va_start(arguments, format);
write_message(true, WARNING, "WARNING", format, arguments);
}

void Log::info(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < INFO && file_message_level < INFO) return;

va_list arguments;
va_start(arguments, format);
write_message(true, INFO, "INFO", format, arguments);
}

void Log::debug(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < DEBUG && file_message_level < DEBUG) return;

va_list arguments;
va_start(arguments, format);
write_message(true, DEBUG, "DEBUG", format, arguments);
}

void Log::trace(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < TRACE && file_message_level < TRACE) return;

va_list arguments;
va_start(arguments, format);
write_message(true, TRACE, "TRACE", format, arguments);
}

void Log::fatal_no_header(const char *format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < FATAL && file_message_level < FATAL) return;

va_list arguments;
va_start(arguments, format);
write_message(false, FATAL, "FATAL", format, arguments);
}

void Log::error_no_header(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < ERROR && file_message_level < ERROR) return;

va_list arguments;
va_start(arguments, format);
write_message(false, ERROR, "ERROR", format, arguments);
}

void Log::warning_no_header(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < WARNING && file_message_level < WARNING) return;

va_list arguments;
va_start(arguments, format);
write_message(false, WARNING, "WARNING", format, arguments);
}

void Log::info_no_header(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < INFO && file_message_level < INFO) return;

va_list arguments;
va_start(arguments, format);
write_message(false, INFO, "INFO", format, arguments);
}

void Log::debug_no_header(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < DEBUG && file_message_level < DEBUG) return;

va_list arguments;
va_start(arguments, format);
write_message(false, DEBUG, "DEBUG", format, arguments);
}

void Log::trace_no_header(const char* format, ...) {
//don't write if this is the wrong process rank
if (restricted_rank >= 0 && restricted_rank != process_rank) return;

//not writing this type of message to either std out or a file
if (std_message_level < TRACE && file_message_level < TRACE) return;

va_list arguments;
va_start(arguments, format);
write_message(false, TRACE, "TRACE", format, arguments);
}

Loading