3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
MessageLogger/MessageLogger.cpp@79:4e6b53eb678b, 2017-04-28 (annotated)
- Committer:
- aburch1
- Date:
- Fri Apr 28 17:17:24 2017 +0000
- Revision:
- 79:4e6b53eb678b
- Parent:
- 77:db3384071634
- Child:
- 80:959151952153
Message logger used to print most messages, bugs including commands failing need to be discussed with nicholas
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aburch1 | 74:749727490f44 | 1 | #include "MessageLogger.h" |
aburch1 | 74:749727490f44 | 2 | |
aburch1 | 75:b44645bbf2d2 | 3 | #define SIGNAL_printMessage 2 |
aburch1 | 75:b44645bbf2d2 | 4 | |
aburch1 | 76:ee1f132e5744 | 5 | Mail<string, 16> message_mail; |
aburch1 | 74:749727490f44 | 6 | |
aburch1 | 74:749727490f44 | 7 | // constructor |
aburch1 | 75:b44645bbf2d2 | 8 | MessageLogger::MessageLogger() |
aburch1 | 74:749727490f44 | 9 | { |
aburch1 | 75:b44645bbf2d2 | 10 | hasError = false; |
aburch1 | 75:b44645bbf2d2 | 11 | messageCount = 0; |
aburch1 | 75:b44645bbf2d2 | 12 | } |
aburch1 | 75:b44645bbf2d2 | 13 | |
aburch1 | 75:b44645bbf2d2 | 14 | void MessageLogger::SetThread(Thread* logger) |
aburch1 | 75:b44645bbf2d2 | 15 | { |
aburch1 | 75:b44645bbf2d2 | 16 | loggingThread = logger; |
aburch1 | 74:749727490f44 | 17 | } |
aburch1 | 74:749727490f44 | 18 | |
aburch1 | 74:749727490f44 | 19 | // public methods: |
aburch1 | 77:db3384071634 | 20 | void MessageLogger::SendError(string errorMessage) |
aburch1 | 74:749727490f44 | 21 | { |
aburch1 | 79:4e6b53eb678b | 22 | fatalError << errorMessage <<"\n\rTerminating Program...\r\n"; |
aburch1 | 75:b44645bbf2d2 | 23 | loggingThread->signal_set(SIGNAL_printMessage); |
aburch1 | 75:b44645bbf2d2 | 24 | hasError = true; |
aburch1 | 74:749727490f44 | 25 | } |
aburch1 | 75:b44645bbf2d2 | 26 | |
aburch1 | 79:4e6b53eb678b | 27 | string *messageObj; |
aburch1 | 79:4e6b53eb678b | 28 | |
aburch1 | 77:db3384071634 | 29 | void MessageLogger::SendMessage(string message) |
aburch1 | 74:749727490f44 | 30 | { |
aburch1 | 79:4e6b53eb678b | 31 | messageObj = message_mail.alloc(); |
aburch1 | 77:db3384071634 | 32 | |
aburch1 | 75:b44645bbf2d2 | 33 | if (messageObj == NULL) |
aburch1 | 75:b44645bbf2d2 | 34 | { |
aburch1 | 75:b44645bbf2d2 | 35 | SendError("ERROR: Message queue is full."); |
aburch1 | 75:b44645bbf2d2 | 36 | return; |
aburch1 | 75:b44645bbf2d2 | 37 | } |
aburch1 | 77:db3384071634 | 38 | |
aburch1 | 77:db3384071634 | 39 | (*messageObj) = message; |
aburch1 | 75:b44645bbf2d2 | 40 | |
aburch1 | 77:db3384071634 | 41 | stat = message_mail.put(messageObj); |
aburch1 | 75:b44645bbf2d2 | 42 | |
aburch1 | 75:b44645bbf2d2 | 43 | //Check if succesful |
aburch1 | 75:b44645bbf2d2 | 44 | if (stat == osErrorResource) |
aburch1 | 75:b44645bbf2d2 | 45 | { |
aburch1 | 75:b44645bbf2d2 | 46 | message_mail.free(messageObj); |
aburch1 | 77:db3384071634 | 47 | ostringstream error; |
aburch1 | 77:db3384071634 | 48 | error << "ERROR CODE: " << stat << ", Failed to retrieve message from queue"; |
aburch1 | 77:db3384071634 | 49 | SendError(error.str()); |
aburch1 | 75:b44645bbf2d2 | 50 | return; |
aburch1 | 75:b44645bbf2d2 | 51 | } |
aburch1 | 77:db3384071634 | 52 | |
aburch1 | 77:db3384071634 | 53 | messageCount++; |
aburch1 | 77:db3384071634 | 54 | loggingThread->signal_set(SIGNAL_printMessage); |
aburch1 | 74:749727490f44 | 55 | } |
aburch1 | 75:b44645bbf2d2 | 56 | |
aburch1 | 74:749727490f44 | 57 | bool MessageLogger::GetError() |
aburch1 | 74:749727490f44 | 58 | { |
aburch1 | 75:b44645bbf2d2 | 59 | if(hasError) |
aburch1 | 75:b44645bbf2d2 | 60 | { |
aburch1 | 75:b44645bbf2d2 | 61 | PrintError(); |
aburch1 | 75:b44645bbf2d2 | 62 | return true; |
aburch1 | 75:b44645bbf2d2 | 63 | } |
aburch1 | 75:b44645bbf2d2 | 64 | return false; |
aburch1 | 74:749727490f44 | 65 | } |
aburch1 | 74:749727490f44 | 66 | bool MessageLogger::GetMessage() |
aburch1 | 74:749727490f44 | 67 | { |
aburch1 | 75:b44645bbf2d2 | 68 | if(messageCount > 0) |
aburch1 | 75:b44645bbf2d2 | 69 | { |
aburch1 | 75:b44645bbf2d2 | 70 | PrintMessage(); |
aburch1 | 75:b44645bbf2d2 | 71 | return true; |
aburch1 | 75:b44645bbf2d2 | 72 | } |
aburch1 | 77:db3384071634 | 73 | return false; |
aburch1 | 74:749727490f44 | 74 | } |
aburch1 | 74:749727490f44 | 75 | |
aburch1 | 74:749727490f44 | 76 | void MessageLogger::PrintError() |
aburch1 | 74:749727490f44 | 77 | { |
aburch1 | 77:db3384071634 | 78 | printf("%s", fatalError.str()); |
aburch1 | 79:4e6b53eb678b | 79 | hasError = false; |
aburch1 | 74:749727490f44 | 80 | } |
aburch1 | 75:b44645bbf2d2 | 81 | |
aburch1 | 79:4e6b53eb678b | 82 | string *message; |
aburch1 | 79:4e6b53eb678b | 83 | |
aburch1 | 74:749727490f44 | 84 | void MessageLogger::PrintMessage() |
aburch1 | 74:749727490f44 | 85 | { |
aburch1 | 75:b44645bbf2d2 | 86 | osEvent evt = message_mail.get(); |
aburch1 | 74:749727490f44 | 87 | |
aburch1 | 75:b44645bbf2d2 | 88 | //Check status |
aburch1 | 75:b44645bbf2d2 | 89 | if (evt.status == osEventMail) |
aburch1 | 75:b44645bbf2d2 | 90 | { |
aburch1 | 79:4e6b53eb678b | 91 | message = (string*)evt.value.p; |
aburch1 | 75:b44645bbf2d2 | 92 | |
aburch1 | 77:db3384071634 | 93 | // Ask about cout as everyone on the internet recommends it as a type safe version of printf. |
aburch1 | 77:db3384071634 | 94 | //std::cout << message; |
aburch1 | 79:4e6b53eb678b | 95 | printf("%s\n\033[2A\n", (*message)); |
aburch1 | 75:b44645bbf2d2 | 96 | |
aburch1 | 75:b44645bbf2d2 | 97 | message_mail.free(message); |
aburch1 | 75:b44645bbf2d2 | 98 | messageCount--; |
aburch1 | 75:b44645bbf2d2 | 99 | } |
aburch1 | 75:b44645bbf2d2 | 100 | else |
aburch1 | 75:b44645bbf2d2 | 101 | { |
aburch1 | 77:db3384071634 | 102 | ostringstream error; |
aburch1 | 77:db3384071634 | 103 | error << "ERROR CODE: " << evt.status << ", Failed to retrieve message from queue"; |
aburch1 | 77:db3384071634 | 104 | SendError(error.str()); |
aburch1 | 75:b44645bbf2d2 | 105 | } |
aburch1 | 74:749727490f44 | 106 | } |