From 4fd38897fd66c245991a4066c7bb3db373087e70 Mon Sep 17 00:00:00 2001 From: FormerLurker Date: Fri, 16 Oct 2020 18:34:25 -0500 Subject: Add additional statistics. Fix windows c++ build for python 2.7 compilers. --- GcodeProcessorLib/GcodeProcessorLib.vcxproj | 2 +- GcodeProcessorLib/gcode_parser.cpp | 40 +++++++++++++----- GcodeProcessorLib/gcode_parser.h | 2 + GcodeProcessorLib/logger.cpp | 63 +++++++++++++++++++++------- GcodeProcessorLib/logger.h | 9 +++- GcodeProcessorLib/utilities.cpp | 65 +++++++++++++++++++++++++++++ GcodeProcessorLib/utilities.h | 7 +++- 7 files changed, 159 insertions(+), 29 deletions(-) (limited to 'GcodeProcessorLib') diff --git a/GcodeProcessorLib/GcodeProcessorLib.vcxproj b/GcodeProcessorLib/GcodeProcessorLib.vcxproj index ec6b6a7..0457545 100644 --- a/GcodeProcessorLib/GcodeProcessorLib.vcxproj +++ b/GcodeProcessorLib/GcodeProcessorLib.vcxproj @@ -129,7 +129,7 @@ Level3 true - _CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true diff --git a/GcodeProcessorLib/gcode_parser.cpp b/GcodeProcessorLib/gcode_parser.cpp index a4e7d0a..b12b4e9 100644 --- a/GcodeProcessorLib/gcode_parser.cpp +++ b/GcodeProcessorLib/gcode_parser.cpp @@ -104,14 +104,25 @@ gcode_parser::~gcode_parser() parsed_command gcode_parser::parse_gcode(const char * gcode) { + parsed_command p_cmd; + try_parse_gcode(gcode, p_cmd, true); + return p_cmd; +} +parsed_command gcode_parser::parse_gcode(const char* gcode, bool preserve_format) +{ parsed_command p_cmd; - try_parse_gcode(gcode, p_cmd); + try_parse_gcode(gcode, p_cmd, preserve_format); return p_cmd; } + +bool gcode_parser::try_parse_gcode(const char* gcode, parsed_command& command) +{ + return try_parse_gcode(gcode, command, true) ; +} // Superfast gcode parser - v2 -bool gcode_parser::try_parse_gcode(const char * gcode, parsed_command & command) +bool gcode_parser::try_parse_gcode(const char * gcode, parsed_command & command, bool preserve_format) { // Create a command char * p_gcode = const_cast(gcode); @@ -130,6 +141,9 @@ bool gcode_parser::try_parse_gcode(const char * gcode, parsed_command & command) command.is_empty = false; break; } + else if (preserve_format) { + command.gcode.push_back(c); + } p_gcode++; } command.command = ""; @@ -138,6 +152,9 @@ bool gcode_parser::try_parse_gcode(const char * gcode, parsed_command & command) command.is_empty = false; bool has_seen_character = false; + + bool is_text_only_parameter = text_only_functions_.find(command.command) != text_only_functions_.end(); + while (true) { char cur_char = *p_gcode; @@ -145,23 +162,25 @@ bool gcode_parser::try_parse_gcode(const char * gcode, parsed_command & command) break; else if (cur_char > 32 || (cur_char == ' ' && has_seen_character)) { - if (cur_char >= 'a' && cur_char <= 'z') + if (!preserve_format && !is_text_only_parameter && (cur_char >= 'a' && cur_char <= 'z')) command.gcode.push_back(cur_char - 32); else command.gcode.push_back(cur_char); has_seen_character = true; } + else if (preserve_format) + { + command.gcode.push_back(cur_char); + } p_gcode++; } - command.gcode = utilities::rtrim(command.gcode); + if (!preserve_format){ + command.gcode = utilities::rtrim(command.gcode); + } - if (command.is_known_command) + if (command.is_known_command && parsable_commands_.find(command.command) != parsable_commands_.end()) { - if (parsable_commands_.find(command.command) == parsable_commands_.end()) - { - return true; - } if (command.command.length() > 0 && command.command == "@OCTOLAPSE") { @@ -188,7 +207,7 @@ bool gcode_parser::try_parse_gcode(const char * gcode, parsed_command & command) } else if ( - text_only_functions_.find(command.command) != text_only_functions_.end() || + is_text_only_parameter || ( command.command.length() > 0 && command.command[0] == '@' ) @@ -232,6 +251,7 @@ bool gcode_parser::try_parse_gcode(const char * gcode, parsed_command & command) } } } + try_extract_comment(&p_gcode, &(command.comment)); diff --git a/GcodeProcessorLib/gcode_parser.h b/GcodeProcessorLib/gcode_parser.h index 50cb01b..714dfe4 100644 --- a/GcodeProcessorLib/gcode_parser.h +++ b/GcodeProcessorLib/gcode_parser.h @@ -35,7 +35,9 @@ public: gcode_parser(); ~gcode_parser(); bool try_parse_gcode(const char * gcode, parsed_command & command); + bool try_parse_gcode(const char* gcode, parsed_command& command, bool preserve_format); parsed_command parse_gcode(const char * gcode); + parsed_command parse_gcode(const char* gcode, bool preserve_format); private: gcode_parser(const gcode_parser &source); // Variables and lookups diff --git a/GcodeProcessorLib/logger.cpp b/GcodeProcessorLib/logger.cpp index 0118e52..8511777 100644 --- a/GcodeProcessorLib/logger.cpp +++ b/GcodeProcessorLib/logger.cpp @@ -19,7 +19,9 @@ // You can contact the author at the following email address: // FormerLurker@pm.me //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - +#if _MSC_VER > 1200 +#define _CRT_SECURE_NO_DEPRECATE +#endif #include "logger.h" logger::logger(std::vector names, std::vector levels) { // set to true by default, but can be changed by inheritance to support mandatory innitialization (for python or other integrations) @@ -89,20 +91,8 @@ void logger::create_log_message(const int logger_type, const int log_level, cons // example message // 2020-04-20 21:36:59,414 - arc_welder.__init__ - INFO - MESSAGE_GOES_HERE - // Create the time string in YYYY-MM-DD HH:MM:SS format - std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); - std::chrono::milliseconds ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; - const time_t now_time = std::chrono::system_clock::to_time_t(now); - struct tm tstruct; - char buf[25]; - tstruct = *localtime(&now_time); - // DOESN'T WORK WITH ALL COMPILERS... - //localtime_s(&tstruct, &now_time); - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.", &tstruct); - output = buf; - std::string s_miliseconds = std::to_string(ms.count()); - // Add the milliseconds, padded with 0s, to the output - output.append(std::string(3 - s_miliseconds.length(), '0') + s_miliseconds); + // Create the time string in YYYY-MM-DD HH:MM:SS.ms format + logger::get_timestamp(output); // Add a spacer output.append(" - "); // Add the logger name @@ -148,3 +138,46 @@ void logger::log(const int logger_type, const int log_level, const std::string& std::cout.flush(); } + +void logger::get_timestamp(std::string ×tamp) +{ + std::time_t rawtime; + std::tm* timeinfo; + char buffer[80]; + + std::time(&rawtime); + timeinfo = std::localtime(&rawtime); + std::strftime(buffer, 80, "%Y-%m-%d %H:%M:%S.", timeinfo); + + timestamp = buffer; + clock_t t = std::clock(); + int ms = static_cast((t / CLOCKS_PER_MS)) % 1000; + + std::string s_miliseconds; + sprintf(buffer, "%d", ms) ;// std::to_string(ms); + s_miliseconds = buffer; + timestamp.append(std::string(3 - s_miliseconds.length(), '0') + s_miliseconds); + +} + +/* + +Severity Code Description Project File Line Suppression State + + + +std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + std::chrono::milliseconds ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; + const time_t now_time = std::chrono::system_clock::to_time_t(now); + struct tm tstruct; + char buf[25]; + tstruct = *localtime(&now_time); + // DOESN'T WORK WITH ALL COMPILERS... + //localtime_s(&tstruct, &now_time); + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.", &tstruct); + output = buf; + std::string s_miliseconds = std::to_string(ms.count()); + // Add the milliseconds, padded with 0s, to the output + output.append(std::string(3 - s_miliseconds.length(), '0') + s_miliseconds); + +*/ \ No newline at end of file diff --git a/GcodeProcessorLib/logger.h b/GcodeProcessorLib/logger.h index c681674..9db2df1 100644 --- a/GcodeProcessorLib/logger.h +++ b/GcodeProcessorLib/logger.h @@ -25,12 +25,16 @@ #include #include #include -#include +#include +//#include #include #define LOG_LEVEL_COUNT 7 +#define CLOCKS_PER_MS (CLOCKS_PER_SEC / 1000.0) enum log_levels { NOSET, VERBOSE, DEBUG, INFO, WARNING , ERROR, CRITICAL}; -const std::array log_level_names = { {"NOSET", "VERBOSE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"} }; +//const std::array log_level_names = { {"NOSET", "VERBOSE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"} }; +static const int log_level_names_size = 7; +static const char* log_level_names[] = {"NOSET", "VERBOSE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}; const static int log_level_values[LOG_LEVEL_COUNT] = { 0, 5, 10, 20, 30, 40, 50}; class logger @@ -59,6 +63,7 @@ private: std::string* logger_names_; int * logger_levels_; int num_loggers_; + static void get_timestamp(std::string ×tamp); }; diff --git a/GcodeProcessorLib/utilities.cpp b/GcodeProcessorLib/utilities.cpp index 5e90768..0af96a9 100644 --- a/GcodeProcessorLib/utilities.cpp +++ b/GcodeProcessorLib/utilities.cpp @@ -23,6 +23,7 @@ #include #include #include +#include // Had to increase the zero tolerance because prusa slicer doesn't always retract enough while wiping. const double ZERO_TOLERANCE = 0.000005; @@ -123,6 +124,13 @@ std::string utilities::to_string(double value) return os.str(); } +std::string utilities::to_string(int value) +{ + std::ostringstream os; + os << value; + return os.str(); +} + char * utilities::to_string(double value, unsigned short precision, char * str) { char reversed_int[20]; @@ -219,3 +227,60 @@ std::istream& utilities::safe_get_line(std::istream& is, std::string& t) } } } + +std::string utilities::center(std::string input, int width) +{ + int input_width = input.length(); + int difference = width - input_width; + if (difference < 1) + { + return input; + } + int left_padding = difference /2; + int right_padding = width - left_padding - input_width; + return std::string(left_padding, ' ') + input + std::string(right_padding, ' '); +} + +std::string utilities::get_percent_change_string(int v1, int v2, int precision) +{ + std::stringstream format_stream; + format_stream.str(std::string()); + std::string percent_change_string; + if (v1 == 0) + { + if (v2 > 0) + { + format_stream << "INF"; + } + else + { + format_stream << std::fixed << std::setprecision(1) << 0.0 << "%"; + } + } + else + { + double percent_change = (((double)v2 - (double)v1) / (double)v1) * 100.0; + format_stream << std::fixed << std::setprecision(precision) << percent_change << "%"; + } + return format_stream.str(); +} + +int utilities::get_num_digits(int x) +{ + x = abs(x); + return (x < 10 ? 1 : + (x < 100 ? 2 : + (x < 1000 ? 3 : + (x < 10000 ? 4 : + (x < 100000 ? 5 : + (x < 1000000 ? 6 : + (x < 10000000 ? 7 : + (x < 100000000 ? 8 : + (x < 1000000000 ? 9 : + 10))))))))); +} + +int utilities::get_num_digits(double x) +{ + return get_num_digits((int) x); +} diff --git a/GcodeProcessorLib/utilities.h b/GcodeProcessorLib/utilities.h index db858ad..a6ddbd2 100644 --- a/GcodeProcessorLib/utilities.h +++ b/GcodeProcessorLib/utilities.h @@ -44,12 +44,17 @@ public: static double get_cartesian_distance(double x1, double y1, double x2, double y2); static double get_cartesian_distance(double x1, double y1, double z1, double x2, double y2, double z2); static std::string to_string(double value); + static std::string to_string(int value); static char* to_string(double value, unsigned short precision, char* str); static std::string ltrim(const std::string& s); static std::string rtrim(const std::string& s); static std::string trim(const std::string& s); static std::istream& safe_get_line(std::istream& is, std::string& t); - + static std::string center(std::string input, int width); + static std::string get_percent_change_string(int v1, int v2, int precision); + + static int get_num_digits(int x); + static int get_num_digits(double x); protected: static const std::string WHITESPACE_; private: -- cgit v1.2.3