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