3rd Repo, trying to figure this out.

Dependencies:   LPS25H hts221

Fork of SOFT253_Template_Weather_OS_54 by Stage-1 Students SoCEM

Committer:
aburch1
Date:
Thu May 11 19:23:55 2017 +0000
Revision:
83:0d3572a8a851
Parent:
82:668b51a39148
Child:
85:422d0a1b95cf
Comments cleaned and improved throughout classes. Main header class comment complete, other class comments still need to be written.

Who changed what in which revision?

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