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