1 /* 2 * Copyright Andrey Semashev 2007 - 2015. 3 * Distributed under the Boost Software License, Version 1.0. 4 * (See accompanying file LICENSE_1_0.txt or copy at 5 * http://www.boost.org/LICENSE_1_0.txt) 6 */ 7 /*! 8 * \file parser_utils.hpp 9 * \author Andrey Semashev 10 * \date 31.03.2008 11 * 12 * \brief This header is the Boost.Log library implementation, see the library documentation 13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. 14 */ 15 16 #ifndef BOOST_LOG_PARSER_UTILS_HPP_INCLUDED_ 17 #define BOOST_LOG_PARSER_UTILS_HPP_INCLUDED_ 18 19 #include <boost/log/detail/config.hpp> 20 #include <string> 21 #include <iostream> 22 #include <cctype> 23 #include <boost/log/utility/string_literal.hpp> 24 #include <boost/log/detail/header.hpp> 25 26 #ifdef BOOST_HAS_PRAGMA_ONCE 27 #pragma once 28 #endif 29 30 namespace boost { 31 32 BOOST_LOG_OPEN_NAMESPACE 33 34 namespace aux { 35 36 //! Some constants and algorithms needed for parsing 37 template< typename > struct char_constants; 38 39 #ifdef BOOST_LOG_USE_CHAR 40 template< > 41 struct char_constants< char > 42 { 43 typedef char char_type; 44 typedef std::basic_string< char_type > string_type; 45 typedef boost::log::basic_string_literal< char_type > literal_type; 46 47 static const char_type char_comment = '#'; 48 static const char_type char_comma = ','; 49 static const char_type char_dot = '.'; 50 static const char_type char_quote = '"'; 51 static const char_type char_percent = '%'; 52 static const char_type char_exclamation = '!'; 53 static const char_type char_and = '&'; 54 static const char_type char_or = '|'; 55 static const char_type char_equal = '='; 56 static const char_type char_greater = '>'; 57 static const char_type char_less = '<'; 58 static const char_type char_underline = '_'; 59 static const char_type char_backslash = '\\'; 60 static const char_type char_section_bracket_left = '['; 61 static const char_type char_section_bracket_right = ']'; 62 static const char_type char_paren_bracket_left = '('; 63 static const char_type char_paren_bracket_right = ')'; 64 not_keywordboost::aux::char_constants65 static const char_type* not_keyword() { return "not"; } and_keywordboost::aux::char_constants66 static const char_type* and_keyword() { return "and"; } or_keywordboost::aux::char_constants67 static const char_type* or_keyword() { return "or"; } equal_keywordboost::aux::char_constants68 static const char_type* equal_keyword() { return "="; } greater_keywordboost::aux::char_constants69 static const char_type* greater_keyword() { return ">"; } less_keywordboost::aux::char_constants70 static const char_type* less_keyword() { return "<"; } not_equal_keywordboost::aux::char_constants71 static const char_type* not_equal_keyword() { return "!="; } greater_or_equal_keywordboost::aux::char_constants72 static const char_type* greater_or_equal_keyword() { return ">="; } less_or_equal_keywordboost::aux::char_constants73 static const char_type* less_or_equal_keyword() { return "<="; } begins_with_keywordboost::aux::char_constants74 static const char_type* begins_with_keyword() { return "begins_with"; } ends_with_keywordboost::aux::char_constants75 static const char_type* ends_with_keyword() { return "ends_with"; } contains_keywordboost::aux::char_constants76 static const char_type* contains_keyword() { return "contains"; } matches_keywordboost::aux::char_constants77 static const char_type* matches_keyword() { return "matches"; } 78 message_text_keywordboost::aux::char_constants79 static const char_type* message_text_keyword() { return "_"; } 80 true_keywordboost::aux::char_constants81 static literal_type true_keyword() { return literal_type("true"); } false_keywordboost::aux::char_constants82 static literal_type false_keyword() { return literal_type("false"); } 83 default_level_attribute_nameboost::aux::char_constants84 static const char_type* default_level_attribute_name() { return "Severity"; } 85 core_section_nameboost::aux::char_constants86 static const char_type* core_section_name() { return "Core"; } sink_section_name_prefixboost::aux::char_constants87 static const char_type* sink_section_name_prefix() { return "Sink:"; } 88 core_disable_logging_param_nameboost::aux::char_constants89 static const char_type* core_disable_logging_param_name() { return "DisableLogging"; } filter_param_nameboost::aux::char_constants90 static const char_type* filter_param_name() { return "Filter"; } 91 sink_destination_param_nameboost::aux::char_constants92 static const char_type* sink_destination_param_name() { return "Destination"; } file_name_param_nameboost::aux::char_constants93 static const char_type* file_name_param_name() { return "FileName"; } rotation_size_param_nameboost::aux::char_constants94 static const char_type* rotation_size_param_name() { return "RotationSize"; } rotation_interval_param_nameboost::aux::char_constants95 static const char_type* rotation_interval_param_name() { return "RotationInterval"; } rotation_time_point_param_nameboost::aux::char_constants96 static const char_type* rotation_time_point_param_name() { return "RotationTimePoint"; } append_param_nameboost::aux::char_constants97 static const char_type* append_param_name() { return "Append"; } enable_final_rotation_param_nameboost::aux::char_constants98 static const char_type* enable_final_rotation_param_name() { return "EnableFinalRotation"; } auto_flush_param_nameboost::aux::char_constants99 static const char_type* auto_flush_param_name() { return "AutoFlush"; } auto_newline_mode_param_nameboost::aux::char_constants100 static const char_type* auto_newline_mode_param_name() { return "AutoNewline"; } asynchronous_param_nameboost::aux::char_constants101 static const char_type* asynchronous_param_name() { return "Asynchronous"; } format_param_nameboost::aux::char_constants102 static const char_type* format_param_name() { return "Format"; } provider_id_param_nameboost::aux::char_constants103 static const char_type* provider_id_param_name() { return "ProviderID"; } log_name_param_nameboost::aux::char_constants104 static const char_type* log_name_param_name() { return "LogName"; } source_name_param_nameboost::aux::char_constants105 static const char_type* source_name_param_name() { return "LogSource"; } registration_param_nameboost::aux::char_constants106 static const char_type* registration_param_name() { return "Registration"; } local_address_param_nameboost::aux::char_constants107 static const char_type* local_address_param_name() { return "LocalAddress"; } target_address_param_nameboost::aux::char_constants108 static const char_type* target_address_param_name() { return "TargetAddress"; } target_param_nameboost::aux::char_constants109 static const char_type* target_param_name() { return "Target"; } max_size_param_nameboost::aux::char_constants110 static const char_type* max_size_param_name() { return "MaxSize"; } max_files_param_nameboost::aux::char_constants111 static const char_type* max_files_param_name() { return "MaxFiles"; } min_free_space_param_nameboost::aux::char_constants112 static const char_type* min_free_space_param_name() { return "MinFreeSpace"; } scan_for_files_param_nameboost::aux::char_constants113 static const char_type* scan_for_files_param_name() { return "ScanForFiles"; } 114 scan_method_allboost::aux::char_constants115 static const char_type* scan_method_all() { return "All"; } scan_method_matchingboost::aux::char_constants116 static const char_type* scan_method_matching() { return "Matching"; } 117 auto_newline_mode_disabledboost::aux::char_constants118 static const char_type* auto_newline_mode_disabled() { return "Disabled"; } auto_newline_mode_always_insertboost::aux::char_constants119 static const char_type* auto_newline_mode_always_insert() { return "AlwaysInsert"; } auto_newline_mode_insert_if_missingboost::aux::char_constants120 static const char_type* auto_newline_mode_insert_if_missing() { return "InsertIfMissing"; } 121 registration_neverboost::aux::char_constants122 static const char_type* registration_never() { return "Never"; } registration_on_demandboost::aux::char_constants123 static const char_type* registration_on_demand() { return "OnDemand"; } registration_forcedboost::aux::char_constants124 static const char_type* registration_forced() { return "Forced"; } 125 text_file_destinationboost::aux::char_constants126 static const char_type* text_file_destination() { return "TextFile"; } console_destinationboost::aux::char_constants127 static const char_type* console_destination() { return "Console"; } syslog_destinationboost::aux::char_constants128 static const char_type* syslog_destination() { return "Syslog"; } simple_event_log_destinationboost::aux::char_constants129 static const char_type* simple_event_log_destination() { return "SimpleEventLog"; } debugger_destinationboost::aux::char_constants130 static const char_type* debugger_destination() { return "Debugger"; } 131 monday_keywordboost::aux::char_constants132 static literal_type monday_keyword() { return literal_type("Monday"); } short_monday_keywordboost::aux::char_constants133 static literal_type short_monday_keyword() { return literal_type("Mon"); } tuesday_keywordboost::aux::char_constants134 static literal_type tuesday_keyword() { return literal_type("Tuesday"); } short_tuesday_keywordboost::aux::char_constants135 static literal_type short_tuesday_keyword() { return literal_type("Tue"); } wednesday_keywordboost::aux::char_constants136 static literal_type wednesday_keyword() { return literal_type("Wednesday"); } short_wednesday_keywordboost::aux::char_constants137 static literal_type short_wednesday_keyword() { return literal_type("Wed"); } thursday_keywordboost::aux::char_constants138 static literal_type thursday_keyword() { return literal_type("Thursday"); } short_thursday_keywordboost::aux::char_constants139 static literal_type short_thursday_keyword() { return literal_type("Thu"); } friday_keywordboost::aux::char_constants140 static literal_type friday_keyword() { return literal_type("Friday"); } short_friday_keywordboost::aux::char_constants141 static literal_type short_friday_keyword() { return literal_type("Fri"); } saturday_keywordboost::aux::char_constants142 static literal_type saturday_keyword() { return literal_type("Saturday"); } short_saturday_keywordboost::aux::char_constants143 static literal_type short_saturday_keyword() { return literal_type("Sat"); } sunday_keywordboost::aux::char_constants144 static literal_type sunday_keyword() { return literal_type("Sunday"); } short_sunday_keywordboost::aux::char_constants145 static literal_type short_sunday_keyword() { return literal_type("Sun"); } 146 get_console_log_streamboost::aux::char_constants147 static std::ostream& get_console_log_stream() { return std::clog; } 148 to_numberboost::aux::char_constants149 static int to_number(char_type c) 150 { 151 using namespace std; // to make sure we can use C functions unqualified 152 int n = 0; 153 if (isdigit(c)) 154 n = c - '0'; 155 else if (c >= 'a' && c <= 'f') 156 n = c - 'a' + 10; 157 else if (c >= 'A' && c <= 'F') 158 n = c - 'A' + 10; 159 return n; 160 } 161 162 //! Skips spaces in the beginning of the input 163 static const char_type* trim_spaces_left(const char_type* begin, const char_type* end); 164 //! Skips spaces in the end of the input 165 static const char_type* trim_spaces_right(const char_type* begin, const char_type* end); 166 //! Scans for the attribute name placeholder in the input 167 static const char_type* scan_attr_placeholder(const char_type* begin, const char_type* end); 168 //! Parses an operand string (possibly quoted) from the input 169 static const char_type* parse_operand(const char_type* begin, const char_type* end, string_type& operand); 170 //! Converts escape sequences to the corresponding characters 171 static void translate_escape_sequences(string_type& str); 172 }; 173 #endif 174 175 #ifdef BOOST_LOG_USE_WCHAR_T 176 template< > 177 struct char_constants< wchar_t > 178 { 179 typedef wchar_t char_type; 180 typedef std::basic_string< char_type > string_type; 181 typedef boost::log::basic_string_literal< char_type > literal_type; 182 183 static const char_type char_comment = L'#'; 184 static const char_type char_comma = L','; 185 static const char_type char_dot = L'.'; 186 static const char_type char_quote = L'"'; 187 static const char_type char_percent = L'%'; 188 static const char_type char_exclamation = L'!'; 189 static const char_type char_and = L'&'; 190 static const char_type char_or = L'|'; 191 static const char_type char_equal = L'='; 192 static const char_type char_greater = L'>'; 193 static const char_type char_less = L'<'; 194 static const char_type char_underline = L'_'; 195 static const char_type char_backslash = L'\\'; 196 static const char_type char_section_bracket_left = L'['; 197 static const char_type char_section_bracket_right = L']'; 198 static const char_type char_paren_bracket_left = L'('; 199 static const char_type char_paren_bracket_right = L')'; 200 not_keywordboost::aux::char_constants201 static const char_type* not_keyword() { return L"not"; } and_keywordboost::aux::char_constants202 static const char_type* and_keyword() { return L"and"; } or_keywordboost::aux::char_constants203 static const char_type* or_keyword() { return L"or"; } equal_keywordboost::aux::char_constants204 static const char_type* equal_keyword() { return L"="; } greater_keywordboost::aux::char_constants205 static const char_type* greater_keyword() { return L">"; } less_keywordboost::aux::char_constants206 static const char_type* less_keyword() { return L"<"; } not_equal_keywordboost::aux::char_constants207 static const char_type* not_equal_keyword() { return L"!="; } greater_or_equal_keywordboost::aux::char_constants208 static const char_type* greater_or_equal_keyword() { return L">="; } less_or_equal_keywordboost::aux::char_constants209 static const char_type* less_or_equal_keyword() { return L"<="; } begins_with_keywordboost::aux::char_constants210 static const char_type* begins_with_keyword() { return L"begins_with"; } ends_with_keywordboost::aux::char_constants211 static const char_type* ends_with_keyword() { return L"ends_with"; } contains_keywordboost::aux::char_constants212 static const char_type* contains_keyword() { return L"contains"; } matches_keywordboost::aux::char_constants213 static const char_type* matches_keyword() { return L"matches"; } 214 message_text_keywordboost::aux::char_constants215 static const char_type* message_text_keyword() { return L"_"; } 216 true_keywordboost::aux::char_constants217 static literal_type true_keyword() { return literal_type(L"true"); } false_keywordboost::aux::char_constants218 static literal_type false_keyword() { return literal_type(L"false"); } 219 default_level_attribute_nameboost::aux::char_constants220 static const char_type* default_level_attribute_name() { return L"Severity"; } 221 core_section_nameboost::aux::char_constants222 static const char_type* core_section_name() { return L"Core"; } sink_section_name_prefixboost::aux::char_constants223 static const char_type* sink_section_name_prefix() { return L"Sink:"; } 224 core_disable_logging_param_nameboost::aux::char_constants225 static const char_type* core_disable_logging_param_name() { return L"DisableLogging"; } filter_param_nameboost::aux::char_constants226 static const char_type* filter_param_name() { return L"Filter"; } 227 sink_destination_param_nameboost::aux::char_constants228 static const char_type* sink_destination_param_name() { return L"Destination"; } file_name_param_nameboost::aux::char_constants229 static const char_type* file_name_param_name() { return L"FileName"; } rotation_size_param_nameboost::aux::char_constants230 static const char_type* rotation_size_param_name() { return L"RotationSize"; } rotation_interval_param_nameboost::aux::char_constants231 static const char_type* rotation_interval_param_name() { return L"RotationInterval"; } rotation_time_point_param_nameboost::aux::char_constants232 static const char_type* rotation_time_point_param_name() { return L"RotationTimePoint"; } append_param_nameboost::aux::char_constants233 static const char_type* append_param_name() { return L"Append"; } enable_final_rotation_param_nameboost::aux::char_constants234 static const char_type* enable_final_rotation_param_name() { return L"EnableFinalRotation"; } auto_flush_param_nameboost::aux::char_constants235 static const char_type* auto_flush_param_name() { return L"AutoFlush"; } auto_newline_mode_param_nameboost::aux::char_constants236 static const char_type* auto_newline_mode_param_name() { return L"AutoNewline"; } asynchronous_param_nameboost::aux::char_constants237 static const char_type* asynchronous_param_name() { return L"Asynchronous"; } format_param_nameboost::aux::char_constants238 static const char_type* format_param_name() { return L"Format"; } provider_id_param_nameboost::aux::char_constants239 static const char_type* provider_id_param_name() { return L"ProviderID"; } log_name_param_nameboost::aux::char_constants240 static const char_type* log_name_param_name() { return L"LogName"; } source_name_param_nameboost::aux::char_constants241 static const char_type* source_name_param_name() { return L"LogSource"; } registration_param_nameboost::aux::char_constants242 static const char_type* registration_param_name() { return L"Registration"; } local_address_param_nameboost::aux::char_constants243 static const char_type* local_address_param_name() { return L"LocalAddress"; } target_address_param_nameboost::aux::char_constants244 static const char_type* target_address_param_name() { return L"TargetAddress"; } target_param_nameboost::aux::char_constants245 static const char_type* target_param_name() { return L"Target"; } max_size_param_nameboost::aux::char_constants246 static const char_type* max_size_param_name() { return L"MaxSize"; } max_files_param_nameboost::aux::char_constants247 static const char_type* max_files_param_name() { return L"MaxFiles"; } min_free_space_param_nameboost::aux::char_constants248 static const char_type* min_free_space_param_name() { return L"MinFreeSpace"; } scan_for_files_param_nameboost::aux::char_constants249 static const char_type* scan_for_files_param_name() { return L"ScanForFiles"; } 250 scan_method_allboost::aux::char_constants251 static const char_type* scan_method_all() { return L"All"; } scan_method_matchingboost::aux::char_constants252 static const char_type* scan_method_matching() { return L"Matching"; } 253 auto_newline_mode_disabledboost::aux::char_constants254 static const char_type* auto_newline_mode_disabled() { return L"Disabled"; } auto_newline_mode_always_insertboost::aux::char_constants255 static const char_type* auto_newline_mode_always_insert() { return L"AlwaysInsert"; } auto_newline_mode_insert_if_missingboost::aux::char_constants256 static const char_type* auto_newline_mode_insert_if_missing() { return L"InsertIfMissing"; } 257 registration_neverboost::aux::char_constants258 static const char_type* registration_never() { return L"Never"; } registration_on_demandboost::aux::char_constants259 static const char_type* registration_on_demand() { return L"OnDemand"; } registration_forcedboost::aux::char_constants260 static const char_type* registration_forced() { return L"Forced"; } 261 text_file_destinationboost::aux::char_constants262 static const char_type* text_file_destination() { return L"TextFile"; } console_destinationboost::aux::char_constants263 static const char_type* console_destination() { return L"Console"; } syslog_destinationboost::aux::char_constants264 static const char_type* syslog_destination() { return L"Syslog"; } simple_event_log_destinationboost::aux::char_constants265 static const char_type* simple_event_log_destination() { return L"SimpleEventLog"; } debugger_destinationboost::aux::char_constants266 static const char_type* debugger_destination() { return L"Debugger"; } 267 monday_keywordboost::aux::char_constants268 static literal_type monday_keyword() { return literal_type(L"Monday"); } short_monday_keywordboost::aux::char_constants269 static literal_type short_monday_keyword() { return literal_type(L"Mon"); } tuesday_keywordboost::aux::char_constants270 static literal_type tuesday_keyword() { return literal_type(L"Tuesday"); } short_tuesday_keywordboost::aux::char_constants271 static literal_type short_tuesday_keyword() { return literal_type(L"Tue"); } wednesday_keywordboost::aux::char_constants272 static literal_type wednesday_keyword() { return literal_type(L"Wednesday"); } short_wednesday_keywordboost::aux::char_constants273 static literal_type short_wednesday_keyword() { return literal_type(L"Wed"); } thursday_keywordboost::aux::char_constants274 static literal_type thursday_keyword() { return literal_type(L"Thursday"); } short_thursday_keywordboost::aux::char_constants275 static literal_type short_thursday_keyword() { return literal_type(L"Thu"); } friday_keywordboost::aux::char_constants276 static literal_type friday_keyword() { return literal_type(L"Friday"); } short_friday_keywordboost::aux::char_constants277 static literal_type short_friday_keyword() { return literal_type(L"Fri"); } saturday_keywordboost::aux::char_constants278 static literal_type saturday_keyword() { return literal_type(L"Saturday"); } short_saturday_keywordboost::aux::char_constants279 static literal_type short_saturday_keyword() { return literal_type(L"Sat"); } sunday_keywordboost::aux::char_constants280 static literal_type sunday_keyword() { return literal_type(L"Sunday"); } short_sunday_keywordboost::aux::char_constants281 static literal_type short_sunday_keyword() { return literal_type(L"Sun"); } 282 get_console_log_streamboost::aux::char_constants283 static std::wostream& get_console_log_stream() { return std::wclog; } 284 to_numberboost::aux::char_constants285 static int to_number(char_type c) 286 { 287 int n = 0; 288 if (c >= L'0' && c <= L'9') 289 n = c - L'0'; 290 else if (c >= L'a' && c <= L'f') 291 n = c - L'a' + 10; 292 else if (c >= L'A' && c <= L'F') 293 n = c - L'A' + 10; 294 return n; 295 } 296 iswxdigitboost::aux::char_constants297 static bool iswxdigit(char_type c) 298 { 299 return (c >= L'0' && c <= L'9') || (c >= L'a' && c <= L'f') || (c >= L'A' && c <= L'F'); 300 } 301 302 //! Skips spaces in the beginning of the input 303 static const char_type* trim_spaces_left(const char_type* begin, const char_type* end); 304 //! Skips spaces in the end of the input 305 static const char_type* trim_spaces_right(const char_type* begin, const char_type* end); 306 //! Scans for the attribute name placeholder in the input 307 static const char_type* scan_attr_placeholder(const char_type* begin, const char_type* end); 308 //! Parses an operand string (possibly quoted) from the input 309 static const char_type* parse_operand(const char_type* begin, const char_type* end, string_type& operand); 310 //! Converts escape sequences to the corresponding characters 311 static void translate_escape_sequences(string_type& str); 312 }; 313 #endif 314 315 } // namespace aux 316 317 BOOST_LOG_CLOSE_NAMESPACE // namespace log 318 319 } // namespace boost 320 321 #include <boost/log/detail/footer.hpp> 322 323 #endif // BOOST_LOG_PARSER_UTILS_HPP_INCLUDED_ 324