Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SOFT253_Template_Weather_OS_54 by
MessageLogger/MessageLogger.cpp@82:668b51a39148, 2017-05-11 (annotated)
- Committer:
- aburch1
- Date:
- Thu May 11 15:53:15 2017 +0000
- Revision:
- 82:668b51a39148
- Parent:
- 81:996c0a3319b4
- Child:
- 83:0d3572a8a851
Added formatting to messages, errors now terminate the program and wait for user input to restart
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| aburch1 | 74:749727490f44 | 1 | #include "MessageLogger.h" |
| aburch1 | 81:996c0a3319b4 | 2 | #include "Message.h" |
| aburch1 | 74:749727490f44 | 3 | |
| aburch1 | 75:b44645bbf2d2 | 4 | #define SIGNAL_printMessage 2 |
| aburch1 | 75:b44645bbf2d2 | 5 | |
| aburch1 | 81:996c0a3319b4 | 6 | |
| aburch1 | 81:996c0a3319b4 | 7 | Mail<Message, 16> message_mail; |
| aburch1 | 74:749727490f44 | 8 | |
| aburch1 | 74:749727490f44 | 9 | // constructor |
| aburch1 | 75:b44645bbf2d2 | 10 | MessageLogger::MessageLogger() |
| aburch1 | 74:749727490f44 | 11 | { |
| aburch1 | 75:b44645bbf2d2 | 12 | hasError = false; |
| aburch1 | 75:b44645bbf2d2 | 13 | messageCount = 0; |
| aburch1 | 81:996c0a3319b4 | 14 | //messageLock = new Mutex(); |
| aburch1 | 75:b44645bbf2d2 | 15 | } |
| aburch1 | 75:b44645bbf2d2 | 16 | |
| aburch1 | 75:b44645bbf2d2 | 17 | void MessageLogger::SetThread(Thread* logger) |
| aburch1 | 75:b44645bbf2d2 | 18 | { |
| aburch1 | 75:b44645bbf2d2 | 19 | loggingThread = logger; |
| aburch1 | 74:749727490f44 | 20 | } |
| aburch1 | 74:749727490f44 | 21 | |
| aburch1 | 74:749727490f44 | 22 | // public methods: |
| aburch1 | 77:db3384071634 | 23 | void MessageLogger::SendError(string errorMessage) |
| aburch1 | 74:749727490f44 | 24 | { |
| aburch1 | 82:668b51a39148 | 25 | fatalError << errorMessage <<"Terminating Program...\r\n"; |
| aburch1 | 75:b44645bbf2d2 | 26 | loggingThread->signal_set(SIGNAL_printMessage); |
| aburch1 | 75:b44645bbf2d2 | 27 | hasError = true; |
| aburch1 | 74:749727490f44 | 28 | } |
| aburch1 | 75:b44645bbf2d2 | 29 | |
| aburch1 | 81:996c0a3319b4 | 30 | Message *newMsg; |
| aburch1 | 79:4e6b53eb678b | 31 | |
| aburch1 | 81:996c0a3319b4 | 32 | void MessageLogger::SendMessage(char* message) |
| aburch1 | 74:749727490f44 | 33 | { |
| aburch1 | 81:996c0a3319b4 | 34 | messageLock.lock(); |
| aburch1 | 81:996c0a3319b4 | 35 | newMsg = message_mail.alloc(); |
| aburch1 | 77:db3384071634 | 36 | |
| aburch1 | 81:996c0a3319b4 | 37 | if (newMsg == NULL) |
| aburch1 | 75:b44645bbf2d2 | 38 | { |
| aburch1 | 82:668b51a39148 | 39 | SendError("ERROR: Message queue is full.\r\n"); |
| aburch1 | 81:996c0a3319b4 | 40 | messageLock.unlock(); |
| aburch1 | 75:b44645bbf2d2 | 41 | return; |
| aburch1 | 75:b44645bbf2d2 | 42 | } |
| aburch1 | 77:db3384071634 | 43 | |
| aburch1 | 81:996c0a3319b4 | 44 | newMsg->copy(message); |
| aburch1 | 75:b44645bbf2d2 | 45 | |
| aburch1 | 81:996c0a3319b4 | 46 | stat = message_mail.put(newMsg); |
| aburch1 | 75:b44645bbf2d2 | 47 | |
| aburch1 | 75:b44645bbf2d2 | 48 | //Check if succesful |
| aburch1 | 75:b44645bbf2d2 | 49 | if (stat == osErrorResource) |
| aburch1 | 75:b44645bbf2d2 | 50 | { |
| aburch1 | 81:996c0a3319b4 | 51 | message_mail.free(newMsg); |
| aburch1 | 77:db3384071634 | 52 | ostringstream error; |
| aburch1 | 82:668b51a39148 | 53 | error << "ERROR CODE: " << stat << ", Failed to put message into queue\r\n"; |
| aburch1 | 77:db3384071634 | 54 | SendError(error.str()); |
| aburch1 | 81:996c0a3319b4 | 55 | messageLock.unlock(); |
| aburch1 | 75:b44645bbf2d2 | 56 | return; |
| aburch1 | 75:b44645bbf2d2 | 57 | } |
| aburch1 | 77:db3384071634 | 58 | |
| aburch1 | 77:db3384071634 | 59 | messageCount++; |
| aburch1 | 77:db3384071634 | 60 | loggingThread->signal_set(SIGNAL_printMessage); |
| aburch1 | 80:959151952153 | 61 | |
| aburch1 | 80:959151952153 | 62 | printf("\033[1A\n"); |
| aburch1 | 81:996c0a3319b4 | 63 | messageLock.unlock(); |
| aburch1 | 74:749727490f44 | 64 | } |
| aburch1 | 75:b44645bbf2d2 | 65 | |
| aburch1 | 74:749727490f44 | 66 | bool MessageLogger::GetError() |
| aburch1 | 74:749727490f44 | 67 | { |
| aburch1 | 75:b44645bbf2d2 | 68 | if(hasError) |
| aburch1 | 75:b44645bbf2d2 | 69 | { |
| aburch1 | 75:b44645bbf2d2 | 70 | PrintError(); |
| aburch1 | 75:b44645bbf2d2 | 71 | return true; |
| aburch1 | 75:b44645bbf2d2 | 72 | } |
| aburch1 | 75:b44645bbf2d2 | 73 | return false; |
| aburch1 | 74:749727490f44 | 74 | } |
| aburch1 | 74:749727490f44 | 75 | bool MessageLogger::GetMessage() |
| aburch1 | 74:749727490f44 | 76 | { |
| aburch1 | 75:b44645bbf2d2 | 77 | if(messageCount > 0) |
| aburch1 | 75:b44645bbf2d2 | 78 | { |
| aburch1 | 75:b44645bbf2d2 | 79 | PrintMessage(); |
| aburch1 | 75:b44645bbf2d2 | 80 | return true; |
| aburch1 | 75:b44645bbf2d2 | 81 | } |
| aburch1 | 82:668b51a39148 | 82 | return false; |
| aburch1 | 74:749727490f44 | 83 | } |
| aburch1 | 74:749727490f44 | 84 | |
| aburch1 | 74:749727490f44 | 85 | void MessageLogger::PrintError() |
| aburch1 | 74:749727490f44 | 86 | { |
| aburch1 | 81:996c0a3319b4 | 87 | printf("%s", fatalError.str().c_str()); |
| aburch1 | 79:4e6b53eb678b | 88 | hasError = false; |
| aburch1 | 74:749727490f44 | 89 | } |
| aburch1 | 75:b44645bbf2d2 | 90 | |
| aburch1 | 81:996c0a3319b4 | 91 | Message *message; |
| aburch1 | 81:996c0a3319b4 | 92 | char copyOfString[256]; |
| aburch1 | 79:4e6b53eb678b | 93 | |
| aburch1 | 74:749727490f44 | 94 | void MessageLogger::PrintMessage() |
| aburch1 | 74:749727490f44 | 95 | { |
| aburch1 | 81:996c0a3319b4 | 96 | messageLock.lock(); |
| aburch1 | 75:b44645bbf2d2 | 97 | osEvent evt = message_mail.get(); |
| aburch1 | 74:749727490f44 | 98 | |
| aburch1 | 75:b44645bbf2d2 | 99 | //Check status |
| aburch1 | 75:b44645bbf2d2 | 100 | if (evt.status == osEventMail) |
| aburch1 | 75:b44645bbf2d2 | 101 | { |
| aburch1 | 81:996c0a3319b4 | 102 | message = (Message*)evt.value.p; |
| aburch1 | 81:996c0a3319b4 | 103 | strncpy(copyOfString, message->getText(), 256); |
| aburch1 | 81:996c0a3319b4 | 104 | message_mail.free(message); |
| aburch1 | 77:db3384071634 | 105 | // Ask about cout as everyone on the internet recommends it as a type safe version of printf. |
| aburch1 | 77:db3384071634 | 106 | //std::cout << message; |
| aburch1 | 81:996c0a3319b4 | 107 | printf("%s\033[2A\n", copyOfString); |
| aburch1 | 75:b44645bbf2d2 | 108 | |
| aburch1 | 75:b44645bbf2d2 | 109 | messageCount--; |
| aburch1 | 75:b44645bbf2d2 | 110 | } |
| aburch1 | 75:b44645bbf2d2 | 111 | else |
| aburch1 | 75:b44645bbf2d2 | 112 | { |
| aburch1 | 77:db3384071634 | 113 | ostringstream error; |
| aburch1 | 82:668b51a39148 | 114 | error << "ERROR CODE: " << evt.status << ", Failed to retrieve message from queue\r\n"; |
| aburch1 | 77:db3384071634 | 115 | SendError(error.str()); |
| aburch1 | 75:b44645bbf2d2 | 116 | } |
| aburch1 | 80:959151952153 | 117 | |
| aburch1 | 81:996c0a3319b4 | 118 | messageLock.unlock(); |
| aburch1 | 74:749727490f44 | 119 | } |
