Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LogUtil by
LogUtil.h@4:cf2ada8ed11b, 2013-04-14 (annotated)
- Committer:
- sam_grove
- Date:
- Sun Apr 14 22:34:13 2013 +0000
- Revision:
- 4:cf2ada8ed11b
- Parent:
- 2:c54746c59ba4
- Child:
- 6:163b9d47fa87
Added Serial object as public member rather than local to the constructor. Need to think about how to abstract this and re-route the content.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:3054bbdace4c | 1 | /** |
sam_grove | 0:3054bbdace4c | 2 | * @file LogUtil.h |
sam_grove | 0:3054bbdace4c | 3 | * @brief Utility to log messages during runtime |
sam_grove | 0:3054bbdace4c | 4 | * @author sam grove |
sam_grove | 0:3054bbdace4c | 5 | * @version 1.0 |
sam_grove | 1:491d2a7f4207 | 6 | * @see http://www.drdobbs.com/cpp/a-lightweight-logger-for-c/240147505 |
sam_grove | 0:3054bbdace4c | 7 | * |
sam_grove | 0:3054bbdace4c | 8 | * Copyright (c) 2013 |
sam_grove | 0:3054bbdace4c | 9 | * |
sam_grove | 0:3054bbdace4c | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 0:3054bbdace4c | 11 | * you may not use this file except in compliance with the License. |
sam_grove | 0:3054bbdace4c | 12 | * You may obtain a copy of the License at |
sam_grove | 0:3054bbdace4c | 13 | * |
sam_grove | 0:3054bbdace4c | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 0:3054bbdace4c | 15 | * |
sam_grove | 0:3054bbdace4c | 16 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 0:3054bbdace4c | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 0:3054bbdace4c | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 0:3054bbdace4c | 19 | * See the License for the specific language governing permissions and |
sam_grove | 0:3054bbdace4c | 20 | * limitations under the License. |
sam_grove | 0:3054bbdace4c | 21 | */ |
sam_grove | 0:3054bbdace4c | 22 | |
sam_grove | 0:3054bbdace4c | 23 | #ifndef LOGUTIL_H |
sam_grove | 0:3054bbdace4c | 24 | #define LOGUTIL_H |
sam_grove | 0:3054bbdace4c | 25 | |
sam_grove | 2:c54746c59ba4 | 26 | #include <stdio.h> |
sam_grove | 2:c54746c59ba4 | 27 | #include <stdlib.h> |
sam_grove | 4:cf2ada8ed11b | 28 | #include "mbed.h" |
sam_grove | 2:c54746c59ba4 | 29 | |
sam_grove | 0:3054bbdace4c | 30 | #define STREAM stdout |
sam_grove | 0:3054bbdace4c | 31 | #define LOG(...) \ |
sam_grove | 0:3054bbdace4c | 32 | fprintf(STREAM, "LOG: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \ |
sam_grove | 0:3054bbdace4c | 33 | fprintf(STREAM, ##__VA_ARGS__) |
sam_grove | 0:3054bbdace4c | 34 | #define WARN(...) \ |
sam_grove | 0:3054bbdace4c | 35 | fprintf(STREAM, "WARN: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \ |
sam_grove | 0:3054bbdace4c | 36 | fprintf(STREAM, ##__VA_ARGS__) |
sam_grove | 0:3054bbdace4c | 37 | #define ERROR(...) \ |
sam_grove | 0:3054bbdace4c | 38 | fprintf(STREAM, "ERROR: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \ |
sam_grove | 1:491d2a7f4207 | 39 | fprintf(STREAM, ##__VA_ARGS__); \ |
sam_grove | 1:491d2a7f4207 | 40 | fflush(STREAM); \ |
sam_grove | 1:491d2a7f4207 | 41 | exit(1) |
sam_grove | 0:3054bbdace4c | 42 | |
sam_grove | 0:3054bbdace4c | 43 | /** Using the LogUtil class |
sam_grove | 0:3054bbdace4c | 44 | * |
sam_grove | 0:3054bbdace4c | 45 | * Example: |
sam_grove | 0:3054bbdace4c | 46 | * @code |
sam_grove | 0:3054bbdace4c | 47 | * #include "mbed.h" |
sam_grove | 0:3054bbdace4c | 48 | * #include "LogUtil.h" |
sam_grove | 0:3054bbdace4c | 49 | * |
sam_grove | 1:491d2a7f4207 | 50 | * LogUtil logger; |
sam_grove | 0:3054bbdace4c | 51 | * |
sam_grove | 0:3054bbdace4c | 52 | * int main() |
sam_grove | 0:3054bbdace4c | 53 | * { |
sam_grove | 0:3054bbdace4c | 54 | * LOG("This is a log\n"); |
sam_grove | 0:3054bbdace4c | 55 | * WARN("This is a warning\n"); |
sam_grove | 0:3054bbdace4c | 56 | * |
sam_grove | 1:491d2a7f4207 | 57 | * for(int i=0; i<3; ++i) { |
sam_grove | 0:3054bbdace4c | 58 | * LOG("Log message #%d\n", i); |
sam_grove | 0:3054bbdace4c | 59 | * } |
sam_grove | 0:3054bbdace4c | 60 | * |
sam_grove | 1:491d2a7f4207 | 61 | * for(int i=0; i<3; ++i) { |
sam_grove | 0:3054bbdace4c | 62 | * WARN("Warn message #%d\n", i); |
sam_grove | 0:3054bbdace4c | 63 | * } |
sam_grove | 0:3054bbdace4c | 64 | * |
sam_grove | 1:491d2a7f4207 | 65 | * ERROR("This is an error\n"); |
sam_grove | 0:3054bbdace4c | 66 | * } |
sam_grove | 0:3054bbdace4c | 67 | * @endcode |
sam_grove | 0:3054bbdace4c | 68 | */ |
sam_grove | 0:3054bbdace4c | 69 | |
sam_grove | 0:3054bbdace4c | 70 | /** |
sam_grove | 0:3054bbdace4c | 71 | * @class LogUtil |
sam_grove | 0:3054bbdace4c | 72 | * @brief Different ways to log messages having a standard interface |
sam_grove | 0:3054bbdace4c | 73 | */ |
sam_grove | 0:3054bbdace4c | 74 | class LogUtil |
sam_grove | 0:3054bbdace4c | 75 | { |
sam_grove | 0:3054bbdace4c | 76 | public: |
sam_grove | 0:3054bbdace4c | 77 | |
sam_grove | 0:3054bbdace4c | 78 | /** Construct the LogUtil class and configure |
sam_grove | 0:3054bbdace4c | 79 | */ |
sam_grove | 0:3054bbdace4c | 80 | LogUtil(); |
sam_grove | 4:cf2ada8ed11b | 81 | Serial debug; |
sam_grove | 0:3054bbdace4c | 82 | |
sam_grove | 0:3054bbdace4c | 83 | }; |
sam_grove | 0:3054bbdace4c | 84 | |
sam_grove | 0:3054bbdace4c | 85 | |
sam_grove | 0:3054bbdace4c | 86 | #endif |
sam_grove | 0:3054bbdace4c | 87 | |
sam_grove | 0:3054bbdace4c | 88 |