3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
MessageLogger/MessageLogger.cpp@77:db3384071634, 2017-04-26 (annotated)
- Committer:
- aburch1
- Date:
- Wed Apr 26 21:58:08 2017 +0000
- Revision:
- 77:db3384071634
- Parent:
- 76:ee1f132e5744
- Child:
- 78:6c2b8ade8414
- Child:
- 79:4e6b53eb678b
Finished message logger so both errors and normal messages can be sent through to be printed. Any error codes are also displayed.
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 | 77:db3384071634 | 22 | fatalError << errorMessage <<"\n\rTerminating Program...\n\r"; |
aburch1 | 75:b44645bbf2d2 | 23 | loggingThread->signal_set(SIGNAL_printMessage); |
aburch1 | 75:b44645bbf2d2 | 24 | hasError = true; |
aburch1 | 74:749727490f44 | 25 | } |
aburch1 | 75:b44645bbf2d2 | 26 | |
aburch1 | 77:db3384071634 | 27 | void MessageLogger::SendMessage(string message) |
aburch1 | 74:749727490f44 | 28 | { |
aburch1 | 77:db3384071634 | 29 | string *messageObj = message_mail.alloc(); |
aburch1 | 77:db3384071634 | 30 | |
aburch1 | 75:b44645bbf2d2 | 31 | if (messageObj == NULL) |
aburch1 | 75:b44645bbf2d2 | 32 | { |
aburch1 | 75:b44645bbf2d2 | 33 | SendError("ERROR: Message queue is full."); |
aburch1 | 75:b44645bbf2d2 | 34 | return; |
aburch1 | 75:b44645bbf2d2 | 35 | } |
aburch1 | 77:db3384071634 | 36 | |
aburch1 | 77:db3384071634 | 37 | (*messageObj) = message; |
aburch1 | 75:b44645bbf2d2 | 38 | |
aburch1 | 77:db3384071634 | 39 | stat = message_mail.put(messageObj); |
aburch1 | 75:b44645bbf2d2 | 40 | |
aburch1 | 75:b44645bbf2d2 | 41 | //Check if succesful |
aburch1 | 75:b44645bbf2d2 | 42 | if (stat == osErrorResource) |
aburch1 | 75:b44645bbf2d2 | 43 | { |
aburch1 | 75:b44645bbf2d2 | 44 | message_mail.free(messageObj); |
aburch1 | 77:db3384071634 | 45 | ostringstream error; |
aburch1 | 77:db3384071634 | 46 | error << "ERROR CODE: " << stat << ", Failed to retrieve message from queue"; |
aburch1 | 77:db3384071634 | 47 | SendError(error.str()); |
aburch1 | 75:b44645bbf2d2 | 48 | return; |
aburch1 | 75:b44645bbf2d2 | 49 | } |
aburch1 | 77:db3384071634 | 50 | |
aburch1 | 77:db3384071634 | 51 | messageCount++; |
aburch1 | 77:db3384071634 | 52 | loggingThread->signal_set(SIGNAL_printMessage); |
aburch1 | 74:749727490f44 | 53 | } |
aburch1 | 75:b44645bbf2d2 | 54 | |
aburch1 | 74:749727490f44 | 55 | bool MessageLogger::GetError() |
aburch1 | 74:749727490f44 | 56 | { |
aburch1 | 75:b44645bbf2d2 | 57 | if(hasError) |
aburch1 | 75:b44645bbf2d2 | 58 | { |
aburch1 | 75:b44645bbf2d2 | 59 | PrintError(); |
aburch1 | 75:b44645bbf2d2 | 60 | return true; |
aburch1 | 75:b44645bbf2d2 | 61 | } |
aburch1 | 75:b44645bbf2d2 | 62 | return false; |
aburch1 | 74:749727490f44 | 63 | } |
aburch1 | 74:749727490f44 | 64 | bool MessageLogger::GetMessage() |
aburch1 | 74:749727490f44 | 65 | { |
aburch1 | 75:b44645bbf2d2 | 66 | if(messageCount > 0) |
aburch1 | 75:b44645bbf2d2 | 67 | { |
aburch1 | 75:b44645bbf2d2 | 68 | PrintMessage(); |
aburch1 | 75:b44645bbf2d2 | 69 | return true; |
aburch1 | 75:b44645bbf2d2 | 70 | } |
aburch1 | 77:db3384071634 | 71 | return false; |
aburch1 | 74:749727490f44 | 72 | } |
aburch1 | 74:749727490f44 | 73 | |
aburch1 | 74:749727490f44 | 74 | void MessageLogger::PrintError() |
aburch1 | 74:749727490f44 | 75 | { |
aburch1 | 77:db3384071634 | 76 | printf("%s", fatalError.str()); |
aburch1 | 74:749727490f44 | 77 | } |
aburch1 | 75:b44645bbf2d2 | 78 | |
aburch1 | 74:749727490f44 | 79 | void MessageLogger::PrintMessage() |
aburch1 | 74:749727490f44 | 80 | { |
aburch1 | 75:b44645bbf2d2 | 81 | osEvent evt = message_mail.get(); |
aburch1 | 74:749727490f44 | 82 | |
aburch1 | 75:b44645bbf2d2 | 83 | //Check status |
aburch1 | 75:b44645bbf2d2 | 84 | if (evt.status == osEventMail) |
aburch1 | 75:b44645bbf2d2 | 85 | { |
aburch1 | 77:db3384071634 | 86 | string *message = (string*)evt.value.p; |
aburch1 | 75:b44645bbf2d2 | 87 | |
aburch1 | 77:db3384071634 | 88 | // Ask about cout as everyone on the internet recommends it as a type safe version of printf. |
aburch1 | 77:db3384071634 | 89 | //std::cout << message; |
aburch1 | 77:db3384071634 | 90 | printf("%s", (*message)); |
aburch1 | 75:b44645bbf2d2 | 91 | |
aburch1 | 75:b44645bbf2d2 | 92 | message_mail.free(message); |
aburch1 | 75:b44645bbf2d2 | 93 | messageCount--; |
aburch1 | 75:b44645bbf2d2 | 94 | } |
aburch1 | 75:b44645bbf2d2 | 95 | else |
aburch1 | 75:b44645bbf2d2 | 96 | { |
aburch1 | 77:db3384071634 | 97 | ostringstream error; |
aburch1 | 77:db3384071634 | 98 | error << "ERROR CODE: " << evt.status << ", Failed to retrieve message from queue"; |
aburch1 | 77:db3384071634 | 99 | SendError(error.str()); |
aburch1 | 75:b44645bbf2d2 | 100 | } |
aburch1 | 74:749727490f44 | 101 | } |