1 #include "image_io/base/message_writer.h" 2 3 #include <cstring> 4 #include <sstream> 5 #include <string> 6 7 namespace photos_editing_formats { 8 namespace image_io { 9 10 using std::string; 11 using std::stringstream; 12 GetFormattedMessage(const Message & message) const13string MessageWriter::GetFormattedMessage(const Message& message) const { 14 stringstream message_stream; 15 auto type = message.GetType(); 16 if (type != Message::kStatus) { 17 message_stream << GetTypeCategory(type) << ":"; 18 } 19 if (type == Message::kInternalError || type == Message::kStdLibError) { 20 message_stream << GetTypeDescription(type, message.GetSystemErrno()) << ":"; 21 } 22 message_stream << message.GetText(); 23 return message_stream.str(); 24 } 25 GetTypeCategory(Message::Type type) const26string MessageWriter::GetTypeCategory(Message::Type type) const { 27 string category; 28 switch (type) { 29 case Message::kStatus: 30 category = "STATUS"; 31 break; 32 case Message::kWarning: 33 category = "WARNING"; 34 break; 35 case Message::kStdLibError: 36 case Message::kPrematureEndOfDataError: 37 case Message::kStringNotFoundError: 38 case Message::kDecodingError: 39 case Message::kSyntaxError: 40 case Message::kValueError: 41 case Message::kInternalError: 42 category = "ERROR"; 43 break; 44 } 45 return category; 46 } 47 GetTypeDescription(Message::Type type,int system_errno) const48string MessageWriter::GetTypeDescription(Message::Type type, 49 int system_errno) const { 50 string description; 51 switch (type) { 52 case Message::kStatus: 53 break; 54 case Message::kWarning: 55 break; 56 case Message::kStdLibError: 57 description = system_errno > 0 ? std::strerror(system_errno) : "Unknown"; 58 break; 59 case Message::kPrematureEndOfDataError: 60 description = "Premature end of data"; 61 break; 62 case Message::kStringNotFoundError: 63 description = "String not found"; 64 break; 65 case Message::kDecodingError: 66 description = "Decoding error"; 67 break; 68 case Message::kSyntaxError: 69 description = "Syntax error"; 70 break; 71 case Message::kValueError: 72 description = "Value error"; 73 break; 74 case Message::kInternalError: 75 description = "Internal error"; 76 break; 77 } 78 return description; 79 } 80 81 } // namespace image_io 82 } // namespace photos_editing_formats 83