Skip to content

Commit 839fef0

Browse files
committed
Added more tests for error log and error reporting, updated doxygen comments
1 parent 9041b47 commit 839fef0

File tree

5 files changed

+518
-143
lines changed

5 files changed

+518
-143
lines changed

TESTS/mbed_platform/error_handling/main.cpp

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "utest/utest.h"
1818
#include "unity/unity.h"
1919
#include "mbed.h"
20+
#include <LittleFileSystem.h>
21+
#include "HeapBlockDevice.h"
2022

2123
using utest::v1::Case;
2224

@@ -319,7 +321,7 @@ void test_error_logging()
319321

320322
}
321323

322-
#define NUM_TEST_THREADS 15
324+
#define NUM_TEST_THREADS 10
323325

324326
//Error logger threads
325327
void err_thread_func(MbedErrorStatus *error_status)
@@ -338,8 +340,7 @@ void test_error_logging_multithread()
338340
Thread errThread[NUM_TEST_THREADS];
339341
MbedErrorStatus error_status[NUM_TEST_THREADS] = {
340342
ERROR_INVALID_ARGUMENT, ERROR_INVALID_DATA, ERROR_INVALID_FORMAT, ERROR_INVALID_SIZE, ERROR_INVALID_OPERATION,
341-
ERROR_NOT_FOUND, ERROR_ACCESS_DENIED, ERROR_FAILED_OPERATION, ERROR_OPERATION_PROHIBITED, ERROR_OPERATION_ABORTED,
342-
ERROR_NO_RESPONSE, ERROR_SEMAPHORE_LOCK_FAILED, ERROR_MUTEX_LOCK_FAILED, ERROR_OPEN_FAILED, ERROR_CLOSE_FAILED
343+
ERROR_NOT_FOUND, ERROR_ACCESS_DENIED, ERROR_FAILED_OPERATION, ERROR_OPERATION_PROHIBITED, ERROR_OPERATION_ABORTED
343344
};
344345

345346

@@ -355,7 +356,11 @@ void test_error_logging_multithread()
355356
printf("\nError log count = %d\n", i+1);
356357
for(;i>=0;--i) {
357358
MbedErrorStatus status = get_error_log_info( i, &error_ctx );
358-
printf("\nError Status[%d] = 0x%08X Value = 0x%08X\n", i, error_ctx.error_status, error_ctx.error_value);
359+
if(status != ERROR_SUCCESS) {
360+
TEST_FAIL();
361+
}
362+
363+
printf("\nError Status[%d] = 0x%08X Value = 0x%08X\n", i, (unsigned int)error_ctx.error_status, (unsigned int)error_ctx.error_value);
359364
TEST_ASSERT_EQUAL_UINT((unsigned int)error_ctx.error_value, (unsigned int)error_ctx.error_status);
360365
}
361366
}
@@ -381,6 +386,94 @@ void test_error_hook()
381386
TEST_ASSERT(sem_status > 0);
382387
}
383388

389+
#ifdef MBED_TEST_SIM_BLOCKDEVICE
390+
391+
// test configuration
392+
#ifndef MBED_TEST_FILESYSTEM
393+
#define MBED_TEST_FILESYSTEM LittleFileSystem
394+
#endif
395+
396+
#ifndef MBED_TEST_FILESYSTEM_DECL
397+
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
398+
#endif
399+
400+
#ifndef MBED_TEST_BLOCK_COUNT
401+
#define MBED_TEST_BLOCK_COUNT 64
402+
#endif
403+
404+
#ifndef MBED_TEST_SIM_BLOCKDEVICE_DECL
405+
#define MBED_TEST_SIM_BLOCKDEVICE_DECL MBED_TEST_SIM_BLOCKDEVICE fd(MBED_TEST_BLOCK_COUNT*512, 1, 1, 512)
406+
#endif
407+
408+
// declarations
409+
#define STRINGIZE(x) STRINGIZE2(x)
410+
#define STRINGIZE2(x) #x
411+
#define INCLUDE(x) STRINGIZE(x.h)
412+
413+
#include INCLUDE(MBED_TEST_FILESYSTEM)
414+
#include INCLUDE(MBED_TEST_SIM_BLOCKDEVICE)
415+
416+
MBED_TEST_FILESYSTEM_DECL;
417+
MBED_TEST_SIM_BLOCKDEVICE_DECL;
418+
419+
/** Test save error log
420+
*/
421+
void test_save_error_log()
422+
{
423+
//Log some errors
424+
SET_ERROR(ERROR_TIMEOUT, "Timeout error", 1 );
425+
SET_ERROR(ERROR_ALREADY_IN_USE, "Already in use error", 2 );
426+
SET_ERROR(ERROR_NOT_SUPPORTED, "Not supported error", 3 );
427+
SET_ERROR(ERROR_ACCESS_DENIED, "Access denied error", 4 );
428+
SET_ERROR(ERROR_NOT_FOUND, "Not found error", 5 );
429+
SET_ERROR(ERROR_INVALID_ARGUMENT, "Invalid argument error", 6 );
430+
SET_ERROR(ERROR_INVALID_SIZE, "Invalid size error", 7 );
431+
SET_ERROR(ERROR_INVALID_FORMAT, "Invalid format error", 8 );
432+
SET_ERROR(ERROR_INVALID_OPERATION, "Invalid operation", 9 );
433+
SET_ERROR(ERROR_NOT_READY, "Not ready error", 10 );
434+
435+
int error = 0;
436+
437+
error = MBED_TEST_FILESYSTEM::format(&fd);
438+
if(error < 0) {
439+
printf("Failed formatting");
440+
TEST_FAIL();
441+
}
442+
443+
error = fs.mount(&fd);
444+
if(error < 0) {
445+
printf("Failed mounting fs");
446+
TEST_FAIL();
447+
}
448+
449+
if(ERROR_SUCCESS != save_error_log("/fs/errors.log")) {
450+
printf("Failed saving error log");
451+
TEST_FAIL();
452+
}
453+
454+
FILE *error_file = fopen("/fs/errors.log", "r");
455+
if(error_file == NULL) {
456+
printf("Unable to find error log in fs");
457+
TEST_FAIL();
458+
}
459+
460+
char buff[64] = {0};
461+
while (!feof(error_file)){
462+
int size = fread(&buff[0], 1, 15, error_file);
463+
fwrite(&buff[0], 1, size, stdout);
464+
}
465+
printf("\r\n");
466+
fclose(error_file);
467+
468+
error = fs.unmount();
469+
if(error < 0) {
470+
printf("Failed unmounting fs");
471+
TEST_FAIL();
472+
}
473+
}
474+
475+
#endif
476+
384477
utest::v1::status_t test_setup(const size_t number_of_cases)
385478
{
386479
GREENTEA_SETUP(100, "default_auto");
@@ -400,6 +493,9 @@ Case cases[] = {
400493
#ifndef MBED_CONF_ERROR_LOG_DISABLED
401494
Case("Test error logging", test_error_logging),
402495
Case("Test error handling multi-threaded", test_error_logging_multithread),
496+
#ifdef MBED_TEST_SIM_BLOCKDEVICE
497+
Case("Test error save log", test_save_error_log),
498+
#endif
403499
#endif
404500
};
405501

platform/mbed_error.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,5 +266,66 @@ int get_error_log_count(void)
266266
{
267267
return mbed_log_get_error_log_count();
268268
}
269+
270+
MbedErrorStatus save_error_log(const char *path)
271+
{
272+
MbedErrorStatus ret = ERROR_SUCCESS;
273+
mbed_error_ctx ctx = {0};
274+
int log_count = mbed_log_get_error_log_count();
275+
FILE *error_log_file = NULL;
276+
277+
//Ensure path is valid
278+
if(path==NULL) {
279+
ret = MAKE_ERROR(ENTITY_PLATFORM, ERROR_CODE_INVALID_ARGUMENT);
280+
goto exit;
281+
}
282+
283+
//Open the file for saving the error log info
284+
if((error_log_file = fopen( path, "w" ) ) == NULL){
285+
ret = MAKE_ERROR(ENTITY_PLATFORM, ERROR_CODE_OPEN_FAILED);
286+
goto exit;
287+
}
288+
289+
//First store the first and last errors
290+
if(fprintf(error_log_file, "\nFirst Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n",
291+
(unsigned int)first_error_ctx.error_status,
292+
(unsigned int)first_error_ctx.thread_id,
293+
(unsigned int)first_error_ctx.error_address,
294+
(unsigned int)first_error_ctx.error_value) <= 0) {
295+
ret = MAKE_ERROR(ENTITY_PLATFORM, ERROR_CODE_WRITE_FAILED);
296+
goto exit;
297+
}
298+
299+
if(fprintf(error_log_file, "\nLast Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n",
300+
(unsigned int)current_error_ctx.error_status,
301+
(unsigned int)current_error_ctx.thread_id,
302+
(unsigned int)current_error_ctx.error_address,
303+
(unsigned int)current_error_ctx.error_value) <= 0) {
304+
ret = MAKE_ERROR(ENTITY_PLATFORM, ERROR_CODE_WRITE_FAILED);
305+
goto exit;
306+
}
307+
308+
//Update with error log info
309+
while(--log_count >= 0) {
310+
mbed_log_get_error(log_count, &ctx);
311+
//first line of file will be error log count
312+
if(fprintf(error_log_file, "\n%d: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n",
313+
log_count,
314+
(unsigned int)ctx.error_status,
315+
(unsigned int)ctx.thread_id,
316+
(unsigned int)ctx.error_address,
317+
(unsigned int)ctx.error_value) <= 0) {
318+
ret = MAKE_ERROR(ENTITY_PLATFORM, ERROR_CODE_WRITE_FAILED);
319+
goto exit;
320+
}
321+
}
322+
323+
exit:
324+
fclose(error_log_file);
325+
326+
return ret;
327+
}
328+
329+
269330
#endif
270331

0 commit comments

Comments
 (0)