* Adds a textual log message to the message buffer. * * @param text Message to add. * @param add_newline Either DO_ADD_NEWLINE or DONT_ADD_NEWLINE **/
| 147 | * @param add_newline Either DO_ADD_NEWLINE or DONT_ADD_NEWLINE |
| 148 | **/ |
| 149 | void AddLogText(const char *text, unsigned int add_newline) |
| 150 | { |
| 151 | // pass log to debugger as well |
| 152 | OutputDebugString(text); |
| 153 | if (add_newline) OutputDebugString("\n"); |
| 154 | |
| 155 | // Used to count the number of new line characters in text |
| 156 | int number_of_newlines; |
| 157 | |
| 158 | // Used to iterate over the text |
| 159 | const char *text_iterator_c; |
| 160 | |
| 161 | // Used to iterate over the message log created in this function |
| 162 | char* msg_iterator; |
| 163 | |
| 164 | // Free a log message if more messages than necessary were logged. |
| 165 | if(logcount >= MAXIMUM_NUMBER_OF_LOGS) |
| 166 | { |
| 167 | free(logtext[truncated_logcount()]); |
| 168 | } |
| 169 | |
| 170 | number_of_newlines = 0; |
| 171 | text_iterator_c = text; |
| 172 | |
| 173 | // Count the number of \n characters in the text |
| 174 | while(*text_iterator_c) |
| 175 | { |
| 176 | if(*text_iterator_c == '\n') |
| 177 | { |
| 178 | number_of_newlines++; |
| 179 | } |
| 180 | |
| 181 | text_iterator_c++; |
| 182 | } |
| 183 | |
| 184 | unsigned int necessary_size = strlen(text) // len(text) |
| 185 | + 1 // 0-byte |
| 186 | + number_of_newlines // Space for additional \r characters |
| 187 | + 2 * add_newline; // \r\n if a newline was requested |
| 188 | |
| 189 | //mbg merge 7/17/06 added cast |
| 190 | logtext[truncated_logcount()] = (char*)malloc(necessary_size); |
| 191 | |
| 192 | // Apparently there's no memory left. |
| 193 | if(!logtext[truncated_logcount()]) |
| 194 | { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | msg_iterator = logtext[truncated_logcount()]; |
| 199 | |
| 200 | // Copy the characters from text to the allocated buffer |
| 201 | while(*text) |
| 202 | { |
| 203 | // Replace \n with \r\n |
| 204 | if(*text == '\n') |
| 205 | { |
| 206 | *msg_iterator='\r'; |
no test coverage detected