3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
MessageLogger/MessageLogger.cpp@80:959151952153, 2017-05-10 (annotated)
- Committer:
- aburch1
- Date:
- Wed May 10 10:02:22 2017 +0000
- Revision:
- 80:959151952153
- Parent:
- 79:4e6b53eb678b
- Child:
- 81:996c0a3319b4
Fixed application crashing when sending 2 messages through to the logger at the same time. Other messages need to be changed to use the logger
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 | 80:959151952153 | 12 | messageLock = new Mutex(); |
aburch1 | 75:b44645bbf2d2 | 13 | } |
aburch1 | 75:b44645bbf2d2 | 14 | |
aburch1 | 75:b44645bbf2d2 | 15 | void MessageLogger::SetThread(Thread* logger) |
aburch1 | 75:b44645bbf2d2 | 16 | { |
aburch1 | 75:b44645bbf2d2 | 17 | loggingThread = logger; |
aburch1 | 74:749727490f44 | 18 | } |
aburch1 | 74:749727490f44 | 19 | |
aburch1 | 74:749727490f44 | 20 | // public methods: |
aburch1 | 77:db3384071634 | 21 | void MessageLogger::SendError(string errorMessage) |
aburch1 | 74:749727490f44 | 22 | { |
aburch1 | 79:4e6b53eb678b | 23 | fatalError << errorMessage <<"\n\rTerminating Program...\r\n"; |
aburch1 | 75:b44645bbf2d2 | 24 | loggingThread->signal_set(SIGNAL_printMessage); |
aburch1 | 75:b44645bbf2d2 | 25 | hasError = true; |
aburch1 | 74:749727490f44 | 26 | } |
aburch1 | 75:b44645bbf2d2 | 27 | |
aburch1 | 79:4e6b53eb678b | 28 | string *messageObj; |
aburch1 | 79:4e6b53eb678b | 29 | |
aburch1 | 77:db3384071634 | 30 | void MessageLogger::SendMessage(string message) |
aburch1 | 74:749727490f44 | 31 | { |
aburch1 | 80:959151952153 | 32 | messageLock->lock(); |
aburch1 | 79:4e6b53eb678b | 33 | messageObj = message_mail.alloc(); |
aburch1 | 77:db3384071634 | 34 | |
aburch1 | 75:b44645bbf2d2 | 35 | if (messageObj == NULL) |
aburch1 | 75:b44645bbf2d2 | 36 | { |
aburch1 | 75:b44645bbf2d2 | 37 | SendError("ERROR: Message queue is full."); |
aburch1 | 80:959151952153 | 38 | messageLock->unlock(); |
aburch1 | 75:b44645bbf2d2 | 39 | return; |
aburch1 | 75:b44645bbf2d2 | 40 | } |
aburch1 | 77:db3384071634 | 41 | |
aburch1 | 77:db3384071634 | 42 | (*messageObj) = message; |
aburch1 | 75:b44645bbf2d2 | 43 | |
aburch1 | 77:db3384071634 | 44 | stat = message_mail.put(messageObj); |
aburch1 | 75:b44645bbf2d2 | 45 | |
aburch1 | 75:b44645bbf2d2 | 46 | //Check if succesful |
aburch1 | 75:b44645bbf2d2 | 47 | if (stat == osErrorResource) |
aburch1 | 75:b44645bbf2d2 | 48 | { |
aburch1 | 75:b44645bbf2d2 | 49 | message_mail.free(messageObj); |
aburch1 | 77:db3384071634 | 50 | ostringstream error; |
aburch1 | 77:db3384071634 | 51 | error << "ERROR CODE: " << stat << ", Failed to retrieve message from queue"; |
aburch1 | 77:db3384071634 | 52 | SendError(error.str()); |
aburch1 | 80:959151952153 | 53 | messageLock->unlock(); |
aburch1 | 75:b44645bbf2d2 | 54 | return; |
aburch1 | 75:b44645bbf2d2 | 55 | } |
aburch1 | 77:db3384071634 | 56 | |
aburch1 | 77:db3384071634 | 57 | messageCount++; |
aburch1 | 77:db3384071634 | 58 | loggingThread->signal_set(SIGNAL_printMessage); |
aburch1 | 80:959151952153 | 59 | |
aburch1 | 80:959151952153 | 60 | printf("\033[1A\n"); |
aburch1 | 80:959151952153 | 61 | messageLock->unlock(); |
aburch1 | 74:749727490f44 | 62 | } |
aburch1 | 75:b44645bbf2d2 | 63 | |
aburch1 | 74:749727490f44 | 64 | bool MessageLogger::GetError() |
aburch1 | 74:749727490f44 | 65 | { |
aburch1 | 75:b44645bbf2d2 | 66 | if(hasError) |
aburch1 | 75:b44645bbf2d2 | 67 | { |
aburch1 | 75:b44645bbf2d2 | 68 | PrintError(); |
aburch1 | 75:b44645bbf2d2 | 69 | return true; |
aburch1 | 75:b44645bbf2d2 | 70 | } |
aburch1 | 75:b44645bbf2d2 | 71 | return false; |
aburch1 | 74:749727490f44 | 72 | } |
aburch1 | 74:749727490f44 | 73 | bool MessageLogger::GetMessage() |
aburch1 | 74:749727490f44 | 74 | { |
aburch1 | 75:b44645bbf2d2 | 75 | if(messageCount > 0) |
aburch1 | 75:b44645bbf2d2 | 76 | { |
aburch1 | 75:b44645bbf2d2 | 77 | PrintMessage(); |
aburch1 | 75:b44645bbf2d2 | 78 | return true; |
aburch1 | 75:b44645bbf2d2 | 79 | } |
aburch1 | 77:db3384071634 | 80 | return false; |
aburch1 | 74:749727490f44 | 81 | } |
aburch1 | 74:749727490f44 | 82 | |
aburch1 | 74:749727490f44 | 83 | void MessageLogger::PrintError() |
aburch1 | 74:749727490f44 | 84 | { |
aburch1 | 77:db3384071634 | 85 | printf("%s", fatalError.str()); |
aburch1 | 79:4e6b53eb678b | 86 | hasError = false; |
aburch1 | 74:749727490f44 | 87 | } |
aburch1 | 75:b44645bbf2d2 | 88 | |
aburch1 | 79:4e6b53eb678b | 89 | string *message; |
aburch1 | 79:4e6b53eb678b | 90 | |
aburch1 | 74:749727490f44 | 91 | void MessageLogger::PrintMessage() |
aburch1 | 74:749727490f44 | 92 | { |
aburch1 | 80:959151952153 | 93 | messageLock->lock(); |
aburch1 | 75:b44645bbf2d2 | 94 | osEvent evt = message_mail.get(); |
aburch1 | 74:749727490f44 | 95 | |
aburch1 | 75:b44645bbf2d2 | 96 | //Check status |
aburch1 | 75:b44645bbf2d2 | 97 | if (evt.status == osEventMail) |
aburch1 | 75:b44645bbf2d2 | 98 | { |
aburch1 | 79:4e6b53eb678b | 99 | message = (string*)evt.value.p; |
aburch1 | 75:b44645bbf2d2 | 100 | |
aburch1 | 77:db3384071634 | 101 | // Ask about cout as everyone on the internet recommends it as a type safe version of printf. |
aburch1 | 77:db3384071634 | 102 | //std::cout << message; |
aburch1 | 80:959151952153 | 103 | printf("%s\033[1A\n", (*message)); |
aburch1 | 75:b44645bbf2d2 | 104 | |
aburch1 | 75:b44645bbf2d2 | 105 | message_mail.free(message); |
aburch1 | 75:b44645bbf2d2 | 106 | messageCount--; |
aburch1 | 75:b44645bbf2d2 | 107 | } |
aburch1 | 75:b44645bbf2d2 | 108 | else |
aburch1 | 75:b44645bbf2d2 | 109 | { |
aburch1 | 77:db3384071634 | 110 | ostringstream error; |
aburch1 | 77:db3384071634 | 111 | error << "ERROR CODE: " << evt.status << ", Failed to retrieve message from queue"; |
aburch1 | 77:db3384071634 | 112 | SendError(error.str()); |
aburch1 | 75:b44645bbf2d2 | 113 | } |
aburch1 | 80:959151952153 | 114 | |
aburch1 | 80:959151952153 | 115 | messageLock->unlock(); |
aburch1 | 74:749727490f44 | 116 | } |