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