SD card interface
mbed-export/error.h@0:22612ae617a0, 2012-10-08 (annotated)
- Committer:
- lharoon
- Date:
- Mon Oct 08 11:14:07 2012 +0000
- Revision:
- 0:22612ae617a0
1st edition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lharoon | 0:22612ae617a0 | 1 | /* mbed Microcontroller Library - error |
lharoon | 0:22612ae617a0 | 2 | * Copyright (c) 2006-2009 ARM Limited. All rights reserved. |
lharoon | 0:22612ae617a0 | 3 | */ |
lharoon | 0:22612ae617a0 | 4 | |
lharoon | 0:22612ae617a0 | 5 | #ifndef MBED_ERROR_H |
lharoon | 0:22612ae617a0 | 6 | #define MBED_ERROR_H |
lharoon | 0:22612ae617a0 | 7 | |
lharoon | 0:22612ae617a0 | 8 | /* Reporting Compile-Time Errors: |
lharoon | 0:22612ae617a0 | 9 | * To generate a fatal compile-time error, you can use the pre-processor #error directive. |
lharoon | 0:22612ae617a0 | 10 | * |
lharoon | 0:22612ae617a0 | 11 | * > #error "That shouldn't have happened!" |
lharoon | 0:22612ae617a0 | 12 | * |
lharoon | 0:22612ae617a0 | 13 | * If the compiler evaluates this line, it will report the error and stop the compile. |
lharoon | 0:22612ae617a0 | 14 | * |
lharoon | 0:22612ae617a0 | 15 | * For example, you could use this to check some user-defined compile-time variables: |
lharoon | 0:22612ae617a0 | 16 | * |
lharoon | 0:22612ae617a0 | 17 | * > #define NUM_PORTS 7 |
lharoon | 0:22612ae617a0 | 18 | * > #if (NUM_PORTS > 4) |
lharoon | 0:22612ae617a0 | 19 | * > #error "NUM_PORTS must be less than 4" |
lharoon | 0:22612ae617a0 | 20 | * > #endif |
lharoon | 0:22612ae617a0 | 21 | * |
lharoon | 0:22612ae617a0 | 22 | * Reporting Run-Time Errors: |
lharoon | 0:22612ae617a0 | 23 | * To generate a fatal run-time error, you can use the mbed error() function. |
lharoon | 0:22612ae617a0 | 24 | * |
lharoon | 0:22612ae617a0 | 25 | * > error("That shouldn't have happened!"); |
lharoon | 0:22612ae617a0 | 26 | * |
lharoon | 0:22612ae617a0 | 27 | * If the mbed running the program executes this function, it will print the |
lharoon | 0:22612ae617a0 | 28 | * message via the USB serial port, and then die with the blue lights of death! |
lharoon | 0:22612ae617a0 | 29 | * |
lharoon | 0:22612ae617a0 | 30 | * The message can use printf-style formatting, so you can report variables in the |
lharoon | 0:22612ae617a0 | 31 | * message too. For example, you could use this to check a run-time condition: |
lharoon | 0:22612ae617a0 | 32 | * |
lharoon | 0:22612ae617a0 | 33 | * > if(x >= 5) { |
lharoon | 0:22612ae617a0 | 34 | * > error("expected x to be less than 5, but got %d", x); |
lharoon | 0:22612ae617a0 | 35 | * > } |
lharoon | 0:22612ae617a0 | 36 | */ |
lharoon | 0:22612ae617a0 | 37 | |
lharoon | 0:22612ae617a0 | 38 | #if 0 // for documentation only |
lharoon | 0:22612ae617a0 | 39 | /* Function: error |
lharoon | 0:22612ae617a0 | 40 | * Report a fatal runtime error |
lharoon | 0:22612ae617a0 | 41 | * |
lharoon | 0:22612ae617a0 | 42 | * Outputs the specified error message to stderr so it will appear via the USB |
lharoon | 0:22612ae617a0 | 43 | * serial port, and then calls exit(1) to die with the blue lights of death. |
lharoon | 0:22612ae617a0 | 44 | * |
lharoon | 0:22612ae617a0 | 45 | * Variables: |
lharoon | 0:22612ae617a0 | 46 | * format - printf-style format string, followed by associated variables |
lharoon | 0:22612ae617a0 | 47 | */ |
lharoon | 0:22612ae617a0 | 48 | void error(const char* format, ...); |
lharoon | 0:22612ae617a0 | 49 | #endif |
lharoon | 0:22612ae617a0 | 50 | |
lharoon | 0:22612ae617a0 | 51 | #include <stdlib.h> |
lharoon | 0:22612ae617a0 | 52 | |
lharoon | 0:22612ae617a0 | 53 | #ifdef NDEBUG |
lharoon | 0:22612ae617a0 | 54 | #define error(...) (exit(1)) |
lharoon | 0:22612ae617a0 | 55 | #else |
lharoon | 0:22612ae617a0 | 56 | #include <stdio.h> |
lharoon | 0:22612ae617a0 | 57 | #define error(...) (fprintf(stderr, __VA_ARGS__), exit(1)) |
lharoon | 0:22612ae617a0 | 58 | #endif |
lharoon | 0:22612ae617a0 | 59 | |
lharoon | 0:22612ae617a0 | 60 | #endif |