3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
MessageLogger/MessageLogger.cpp@85:422d0a1b95cf, 2017-05-13 (annotated)
- Committer:
- niallfrancis
- Date:
- Sat May 13 17:35:58 2017 +0000
- Revision:
- 85:422d0a1b95cf
- Parent:
- 83:0d3572a8a851
Finished commenting classes;
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 | |
niallfrancis | 85:422d0a1b95cf | 6 | /** |
niallfrancis | 85:422d0a1b95cf | 7 | @file : MessageLogger.cpp |
niallfrancis | 85:422d0a1b95cf | 8 | @authors : Radu Marcu, Jacob Williams, Niall Francis, Arron Burch |
niallfrancis | 85:422d0a1b95cf | 9 | |
niallfrancis | 85:422d0a1b95cf | 10 | @section DESCRIPTION |
niallfrancis | 85:422d0a1b95cf | 11 | |
niallfrancis | 85:422d0a1b95cf | 12 | This is the MessageLogger class, which is responsible for handling the printing |
niallfrancis | 85:422d0a1b95cf | 13 | of messages. All messages are placed in a mailbox to be printed out in order of |
niallfrancis | 85:422d0a1b95cf | 14 | oldest to newest, with the exception of error messages, which are prioritized by |
niallfrancis | 85:422d0a1b95cf | 15 | the logging thread (main.cpp). |
niallfrancis | 85:422d0a1b95cf | 16 | */ |
niallfrancis | 85:422d0a1b95cf | 17 | |
aburch1 | 81:996c0a3319b4 | 18 | |
aburch1 | 81:996c0a3319b4 | 19 | Mail<Message, 16> message_mail; |
aburch1 | 74:749727490f44 | 20 | |
aburch1 | 83:0d3572a8a851 | 21 | MessageLogger::MessageLogger() |
aburch1 | 83:0d3572a8a851 | 22 | { |
aburch1 | 83:0d3572a8a851 | 23 | hasError = false; |
aburch1 | 83:0d3572a8a851 | 24 | messageCount = 0; |
aburch1 | 83:0d3572a8a851 | 25 | } |
aburch1 | 83:0d3572a8a851 | 26 | |
aburch1 | 83:0d3572a8a851 | 27 | /** |
aburch1 | 83:0d3572a8a851 | 28 | @param logger : Pointer to the loggingThread. |
aburch1 | 83:0d3572a8a851 | 29 | */ |
aburch1 | 83:0d3572a8a851 | 30 | void MessageLogger::SetThread(Thread* logger) |
aburch1 | 83:0d3572a8a851 | 31 | { |
aburch1 | 83:0d3572a8a851 | 32 | loggingThread = logger; |
aburch1 | 83:0d3572a8a851 | 33 | } |
aburch1 | 83:0d3572a8a851 | 34 | |
aburch1 | 83:0d3572a8a851 | 35 | /** |
aburch1 | 83:0d3572a8a851 | 36 | Sends a signal to the loggingThread, indicating an error needs to be printed. |
aburch1 | 83:0d3572a8a851 | 37 | |
aburch1 | 83:0d3572a8a851 | 38 | @param errorMessage : Error Message to be sent through the loggingThread. |
aburch1 | 83:0d3572a8a851 | 39 | */ |
aburch1 | 83:0d3572a8a851 | 40 | void MessageLogger::SendError(string errorMessage) |
aburch1 | 83:0d3572a8a851 | 41 | { |
aburch1 | 83:0d3572a8a851 | 42 | fatalError << errorMessage <<"Terminating Program...\r\n"; |
aburch1 | 83:0d3572a8a851 | 43 | |
aburch1 | 83:0d3572a8a851 | 44 | // Tells the loggingThread there is an error queued to be printed. |
aburch1 | 83:0d3572a8a851 | 45 | loggingThread->signal_set(SIGNAL_printMessage); |
aburch1 | 83:0d3572a8a851 | 46 | hasError = true; |
aburch1 | 83:0d3572a8a851 | 47 | } |
aburch1 | 83:0d3572a8a851 | 48 | |
aburch1 | 83:0d3572a8a851 | 49 | Message *newMsg; |
aburch1 | 83:0d3572a8a851 | 50 | |
aburch1 | 83:0d3572a8a851 | 51 | /** |
aburch1 | 83:0d3572a8a851 | 52 | Sends a signal to the loggingThread, indicating a message needs to be printed. |
aburch1 | 83:0d3572a8a851 | 53 | |
aburch1 | 83:0d3572a8a851 | 54 | @param message : Message to be sent through the loggingThread. |
aburch1 | 83:0d3572a8a851 | 55 | */ |
aburch1 | 83:0d3572a8a851 | 56 | void MessageLogger::SendMessage(char* message) |
aburch1 | 83:0d3572a8a851 | 57 | { |
aburch1 | 83:0d3572a8a851 | 58 | messageLock.lock(); |
aburch1 | 83:0d3572a8a851 | 59 | newMsg = message_mail.alloc(); |
aburch1 | 83:0d3572a8a851 | 60 | |
aburch1 | 83:0d3572a8a851 | 61 | // Checks if space in mailbox has been unsuccessfully allocated for the message. |
aburch1 | 83:0d3572a8a851 | 62 | if (newMsg == NULL) |
aburch1 | 83:0d3572a8a851 | 63 | { |
aburch1 | 83:0d3572a8a851 | 64 | // Sends an error though the loggingThread. |
aburch1 | 83:0d3572a8a851 | 65 | SendError("ERROR: Message queue is full.\r\n"); |
aburch1 | 83:0d3572a8a851 | 66 | messageLock.unlock(); |
aburch1 | 83:0d3572a8a851 | 67 | return; |
aburch1 | 83:0d3572a8a851 | 68 | } |
aburch1 | 83:0d3572a8a851 | 69 | |
aburch1 | 83:0d3572a8a851 | 70 | newMsg->copy(message); |
aburch1 | 83:0d3572a8a851 | 71 | |
aburch1 | 83:0d3572a8a851 | 72 | stat = message_mail.put(newMsg); |
aburch1 | 83:0d3572a8a851 | 73 | |
aburch1 | 83:0d3572a8a851 | 74 | // Checks if message has been unsuccessfully put into the mailbox. |
aburch1 | 83:0d3572a8a851 | 75 | if (stat == osErrorResource) |
aburch1 | 83:0d3572a8a851 | 76 | { |
aburch1 | 83:0d3572a8a851 | 77 | message_mail.free(newMsg); |
aburch1 | 83:0d3572a8a851 | 78 | ostringstream error; |
aburch1 | 83:0d3572a8a851 | 79 | error << "ERROR CODE: " << stat << ", Failed to put message into queue\r\n"; |
aburch1 | 83:0d3572a8a851 | 80 | SendError(error.str()); |
aburch1 | 83:0d3572a8a851 | 81 | messageLock.unlock(); |
aburch1 | 83:0d3572a8a851 | 82 | return; |
aburch1 | 83:0d3572a8a851 | 83 | } |
aburch1 | 83:0d3572a8a851 | 84 | |
aburch1 | 83:0d3572a8a851 | 85 | messageCount++; |
aburch1 | 75:b44645bbf2d2 | 86 | |
aburch1 | 83:0d3572a8a851 | 87 | // Tells the logging thread there is a message queued to be printed. |
aburch1 | 83:0d3572a8a851 | 88 | loggingThread->signal_set(SIGNAL_printMessage); |
aburch1 | 83:0d3572a8a851 | 89 | |
aburch1 | 83:0d3572a8a851 | 90 | printf("\033[1A\n"); |
aburch1 | 83:0d3572a8a851 | 91 | messageLock.unlock(); |
aburch1 | 83:0d3572a8a851 | 92 | } |
aburch1 | 83:0d3572a8a851 | 93 | |
aburch1 | 83:0d3572a8a851 | 94 | /** |
aburch1 | 83:0d3572a8a851 | 95 | Used by the loggingThread to initialise the printing of a queued error message. |
aburch1 | 83:0d3572a8a851 | 96 | |
aburch1 | 83:0d3572a8a851 | 97 | @return : Whether there is an error ready to be printed. |
aburch1 | 83:0d3572a8a851 | 98 | */ |
aburch1 | 83:0d3572a8a851 | 99 | bool MessageLogger::GetError() |
aburch1 | 83:0d3572a8a851 | 100 | { |
aburch1 | 83:0d3572a8a851 | 101 | if(hasError) |
aburch1 | 83:0d3572a8a851 | 102 | { |
aburch1 | 83:0d3572a8a851 | 103 | PrintError(); |
aburch1 | 83:0d3572a8a851 | 104 | return true; |
aburch1 | 83:0d3572a8a851 | 105 | } |
aburch1 | 83:0d3572a8a851 | 106 | |
aburch1 | 83:0d3572a8a851 | 107 | return false; |
aburch1 | 83:0d3572a8a851 | 108 | } |
aburch1 | 83:0d3572a8a851 | 109 | |
aburch1 | 83:0d3572a8a851 | 110 | /** |
aburch1 | 83:0d3572a8a851 | 111 | Used by the loggingThread to initialise the printing of a queued message. |
aburch1 | 83:0d3572a8a851 | 112 | |
aburch1 | 83:0d3572a8a851 | 113 | @return : Whether there is a message ready to be printed. |
aburch1 | 83:0d3572a8a851 | 114 | */ |
aburch1 | 83:0d3572a8a851 | 115 | bool MessageLogger::GetMessage() |
aburch1 | 83:0d3572a8a851 | 116 | { |
aburch1 | 83:0d3572a8a851 | 117 | if(messageCount > 0) |
aburch1 | 83:0d3572a8a851 | 118 | { |
aburch1 | 83:0d3572a8a851 | 119 | PrintMessage(); |
aburch1 | 83:0d3572a8a851 | 120 | return true; |
aburch1 | 83:0d3572a8a851 | 121 | } |
aburch1 | 83:0d3572a8a851 | 122 | return false; |
aburch1 | 83:0d3572a8a851 | 123 | } |
aburch1 | 83:0d3572a8a851 | 124 | |
aburch1 | 83:0d3572a8a851 | 125 | /** |
aburch1 | 83:0d3572a8a851 | 126 | Prints the queued error message to the terminal. |
aburch1 | 83:0d3572a8a851 | 127 | */ |
aburch1 | 83:0d3572a8a851 | 128 | void MessageLogger::PrintError() |
aburch1 | 83:0d3572a8a851 | 129 | { |
aburch1 | 83:0d3572a8a851 | 130 | printf("%s", fatalError.str().c_str()); |
aburch1 | 83:0d3572a8a851 | 131 | hasError = false; |
aburch1 | 83:0d3572a8a851 | 132 | } |
aburch1 | 83:0d3572a8a851 | 133 | |
aburch1 | 83:0d3572a8a851 | 134 | Message *message; |
aburch1 | 83:0d3572a8a851 | 135 | char copyOfString[256]; |
aburch1 | 83:0d3572a8a851 | 136 | |
aburch1 | 83:0d3572a8a851 | 137 | /** |
aburch1 | 83:0d3572a8a851 | 138 | Prints the oldest queued message to the terminal. |
aburch1 | 83:0d3572a8a851 | 139 | */ |
aburch1 | 83:0d3572a8a851 | 140 | void MessageLogger::PrintMessage() |
aburch1 | 83:0d3572a8a851 | 141 | { |
aburch1 | 83:0d3572a8a851 | 142 | messageLock.lock(); |
aburch1 | 83:0d3572a8a851 | 143 | osEvent evt = message_mail.get(); |
aburch1 | 83:0d3572a8a851 | 144 | |
aburch1 | 83:0d3572a8a851 | 145 | // Checks if the message has been retreived from the mailbox successfully. |
aburch1 | 83:0d3572a8a851 | 146 | if (evt.status == osEventMail) |
aburch1 | 83:0d3572a8a851 | 147 | { |
aburch1 | 83:0d3572a8a851 | 148 | // Copies the content from the mailbox into an array and prints to the terminal. |
aburch1 | 83:0d3572a8a851 | 149 | message = (Message*)evt.value.p; |
aburch1 | 83:0d3572a8a851 | 150 | strncpy(copyOfString, message->getText(), 256); |
aburch1 | 83:0d3572a8a851 | 151 | message_mail.free(message); |
aburch1 | 83:0d3572a8a851 | 152 | printf("%s\033[1A\n", copyOfString); |
aburch1 | 75:b44645bbf2d2 | 153 | |
aburch1 | 83:0d3572a8a851 | 154 | messageCount--; |
aburch1 | 83:0d3572a8a851 | 155 | } |
aburch1 | 83:0d3572a8a851 | 156 | else // If unsuccessful, send an error to the loggingThread to be printed. |
aburch1 | 83:0d3572a8a851 | 157 | { |
aburch1 | 83:0d3572a8a851 | 158 | ostringstream error; |
aburch1 | 83:0d3572a8a851 | 159 | error << "ERROR CODE: " << evt.status << ", Failed to retrieve message from queue\r\n"; |
aburch1 | 83:0d3572a8a851 | 160 | SendError(error.str()); |
aburch1 | 83:0d3572a8a851 | 161 | } |
aburch1 | 83:0d3572a8a851 | 162 | |
aburch1 | 83:0d3572a8a851 | 163 | messageLock.unlock(); |
aburch1 | 83:0d3572a8a851 | 164 | } |