a
Dependencies: mbed
LogUtil/LogUtil.h@0:85567bbcebdb, 2014-12-14 (annotated)
- Committer:
- Jagang
- Date:
- Sun Dec 14 17:49:01 2014 +0000
- Revision:
- 0:85567bbcebdb
New
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jagang | 0:85567bbcebdb | 1 | /** |
Jagang | 0:85567bbcebdb | 2 | * @file LogUtil.h |
Jagang | 0:85567bbcebdb | 3 | * @brief Utility to log messages during runtime |
Jagang | 0:85567bbcebdb | 4 | * @author sam grove |
Jagang | 0:85567bbcebdb | 5 | * @version 1.0 |
Jagang | 0:85567bbcebdb | 6 | * @see http://www.drdobbs.com/cpp/a-lightweight-logger-for-c/240147505 |
Jagang | 0:85567bbcebdb | 7 | * |
Jagang | 0:85567bbcebdb | 8 | * Copyright (c) 2013 |
Jagang | 0:85567bbcebdb | 9 | * |
Jagang | 0:85567bbcebdb | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Jagang | 0:85567bbcebdb | 11 | * you may not use this file except in compliance with the License. |
Jagang | 0:85567bbcebdb | 12 | * You may obtain a copy of the License at |
Jagang | 0:85567bbcebdb | 13 | * |
Jagang | 0:85567bbcebdb | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jagang | 0:85567bbcebdb | 15 | * |
Jagang | 0:85567bbcebdb | 16 | * Unless required by applicable law or agreed to in writing, software |
Jagang | 0:85567bbcebdb | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
Jagang | 0:85567bbcebdb | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Jagang | 0:85567bbcebdb | 19 | * See the License for the specific language governing permissions and |
Jagang | 0:85567bbcebdb | 20 | * limitations under the License. |
Jagang | 0:85567bbcebdb | 21 | */ |
Jagang | 0:85567bbcebdb | 22 | |
Jagang | 0:85567bbcebdb | 23 | #ifndef LOGUTIL_H |
Jagang | 0:85567bbcebdb | 24 | #define LOGUTIL_H |
Jagang | 0:85567bbcebdb | 25 | |
Jagang | 0:85567bbcebdb | 26 | #include <stdio.h> |
Jagang | 0:85567bbcebdb | 27 | #include <stdlib.h> |
Jagang | 0:85567bbcebdb | 28 | #include "mbed.h" |
Jagang | 0:85567bbcebdb | 29 | |
Jagang | 0:85567bbcebdb | 30 | #define STREAM stdout |
Jagang | 0:85567bbcebdb | 31 | #define LOG(...) \ |
Jagang | 0:85567bbcebdb | 32 | fprintf(STREAM, "LOG: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \ |
Jagang | 0:85567bbcebdb | 33 | fprintf(STREAM, ##__VA_ARGS__); \ |
Jagang | 0:85567bbcebdb | 34 | fflush(STREAM) |
Jagang | 0:85567bbcebdb | 35 | #define WARN(...) \ |
Jagang | 0:85567bbcebdb | 36 | fprintf(STREAM, "WARN: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \ |
Jagang | 0:85567bbcebdb | 37 | fprintf(STREAM, ##__VA_ARGS__); \ |
Jagang | 0:85567bbcebdb | 38 | fflush(STREAM) |
Jagang | 0:85567bbcebdb | 39 | #define ERROR(...) \ |
Jagang | 0:85567bbcebdb | 40 | fprintf(STREAM, "ERROR: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \ |
Jagang | 0:85567bbcebdb | 41 | fprintf(STREAM, ##__VA_ARGS__); \ |
Jagang | 0:85567bbcebdb | 42 | fflush(STREAM); \ |
Jagang | 0:85567bbcebdb | 43 | exit(1) |
Jagang | 0:85567bbcebdb | 44 | |
Jagang | 0:85567bbcebdb | 45 | /** Using the LogUtil class |
Jagang | 0:85567bbcebdb | 46 | * |
Jagang | 0:85567bbcebdb | 47 | * Example: |
Jagang | 0:85567bbcebdb | 48 | * @code |
Jagang | 0:85567bbcebdb | 49 | * #include "mbed.h" |
Jagang | 0:85567bbcebdb | 50 | * #include "LogUtil.h" |
Jagang | 0:85567bbcebdb | 51 | * |
Jagang | 0:85567bbcebdb | 52 | * LogUtil logger; |
Jagang | 0:85567bbcebdb | 53 | * |
Jagang | 0:85567bbcebdb | 54 | * int main() |
Jagang | 0:85567bbcebdb | 55 | * { |
Jagang | 0:85567bbcebdb | 56 | * LOG("This is a log\n"); |
Jagang | 0:85567bbcebdb | 57 | * WARN("This is a warning\n"); |
Jagang | 0:85567bbcebdb | 58 | * |
Jagang | 0:85567bbcebdb | 59 | * for(int i=0; i<3; ++i) { |
Jagang | 0:85567bbcebdb | 60 | * LOG("Log message #%d\n", i); |
Jagang | 0:85567bbcebdb | 61 | * } |
Jagang | 0:85567bbcebdb | 62 | * |
Jagang | 0:85567bbcebdb | 63 | * for(int i=0; i<3; ++i) { |
Jagang | 0:85567bbcebdb | 64 | * WARN("Warn message #%d\n", i); |
Jagang | 0:85567bbcebdb | 65 | * } |
Jagang | 0:85567bbcebdb | 66 | * |
Jagang | 0:85567bbcebdb | 67 | * ERROR("This is an error\n"); |
Jagang | 0:85567bbcebdb | 68 | * } |
Jagang | 0:85567bbcebdb | 69 | * @endcode |
Jagang | 0:85567bbcebdb | 70 | */ |
Jagang | 0:85567bbcebdb | 71 | |
Jagang | 0:85567bbcebdb | 72 | /** |
Jagang | 0:85567bbcebdb | 73 | * @class LogUtil |
Jagang | 0:85567bbcebdb | 74 | * @brief Different ways to log messages having a standard interface |
Jagang | 0:85567bbcebdb | 75 | */ |
Jagang | 0:85567bbcebdb | 76 | class LogUtil |
Jagang | 0:85567bbcebdb | 77 | { |
Jagang | 0:85567bbcebdb | 78 | private: |
Jagang | 0:85567bbcebdb | 79 | Serial *_serial; |
Jagang | 0:85567bbcebdb | 80 | public: |
Jagang | 0:85567bbcebdb | 81 | |
Jagang | 0:85567bbcebdb | 82 | /** Construct the LogUtil class and configure |
Jagang | 0:85567bbcebdb | 83 | */ |
Jagang | 0:85567bbcebdb | 84 | LogUtil(Serial &serial, uint32_t baudrate = 0); |
Jagang | 0:85567bbcebdb | 85 | |
Jagang | 0:85567bbcebdb | 86 | }; |
Jagang | 0:85567bbcebdb | 87 | |
Jagang | 0:85567bbcebdb | 88 | |
Jagang | 0:85567bbcebdb | 89 | #endif |
Jagang | 0:85567bbcebdb | 90 | |
Jagang | 0:85567bbcebdb | 91 |