|
| 1 | +--- |
| 2 | +title: Inline Logging |
| 3 | +--- |
| 4 | + |
| 5 | +import ParamsTable from "components/params-table.tsx"; |
| 6 | + |
| 7 | +# Inline Logging |
| 8 | + |
| 9 | +Sometimes, we just want to log a simple message without the full traceback. |
| 10 | +In this case, we can use the inline logging API. |
| 11 | + |
| 12 | +## API |
| 13 | + |
| 14 | +### `CTB_LOG_ERROR_INLINE` <Badge text="Macro" /> |
| 15 | +Wrapper for logging an error to stderr without stacktrace. |
| 16 | +```c |
| 17 | +CTB_LOG_ERROR_INLINE(ctb_error, msg, ...) |
| 18 | +``` |
| 19 | +
|
| 20 | +<Callout type="warning">Logging an inline error has no effect on the context / call stack. It only logs an error message.</Callout> |
| 21 | +
|
| 22 | +#### Parameters |
| 23 | +<ParamsTable |
| 24 | + params={[ |
| 25 | + { name: "ctb_error", type: "CTB_Error", desc: "The error type." }, |
| 26 | + { name: "msg", type: "const char *", desc: "Error message." }, |
| 27 | + { name: "...", type: "N/A", desc: "Additional arguments for formatting the message." }, |
| 28 | + ]} |
| 29 | +/> |
| 30 | +
|
| 31 | +#### Expands to |
| 32 | +```c |
| 33 | +do \ |
| 34 | +{ \ |
| 35 | + ctb_log_error_inline( \ |
| 36 | + __FILE__, __LINE__, __func__, ctb_error, msg, __VA_ARGS__ \ |
| 37 | + ); \ |
| 38 | +} while (0) |
| 39 | +``` |
| 40 | + |
| 41 | +#### Usage |
| 42 | +```c |
| 43 | +double division(double x, double y) |
| 44 | +{ |
| 45 | + if (y == 0) |
| 46 | + { |
| 47 | + CTB_LOG_ERROR_INLINE( |
| 48 | + CTB_ZERO_DIVISION_ERROR, |
| 49 | + "y cannot be zero! Received: %lf", |
| 50 | + y |
| 51 | + ); |
| 52 | + return 0.0; |
| 53 | + } |
| 54 | + return x / y; |
| 55 | +} |
| 56 | +``` |
| 57 | +Output: |
| 58 | +```ansi |
| 59 | +[1;31mZeroDivisionError:[0m [38;5;240mFile "[0m[38;5;240m/home/alvinng/Desktop/c_trackback/examples/[0m[1;36mexample.c[0m[38;5;240m", line[0m [1;36m19[0m [38;5;240min[0m [1;36mdivision[0m: |
| 60 | + [0;31my cannot be zero! Received: 0.000000[0m |
| 61 | +``` |
| 62 | + |
| 63 | +### `CTB_LOG_WARNING_INLINE` <Badge text="Macro" /> |
| 64 | +Wrapper for logging a warning to stderr without stacktrace. |
| 65 | +```c |
| 66 | +CTB_LOG_WARNING_INLINE(ctb_warning, msg, ...) |
| 67 | +``` |
| 68 | +
|
| 69 | +#### Parameters |
| 70 | +<ParamsTable |
| 71 | + params={[ |
| 72 | + { name: "ctb_warning", type: "CTB_Warning", desc: "The warning type." }, |
| 73 | + { name: "msg", type: "const char *", desc: "Warning message." }, |
| 74 | + { name: "...", type: "N/A", desc: "Additional arguments for formatting the message." }, |
| 75 | + ]} |
| 76 | +/> |
| 77 | +
|
| 78 | +#### Expands to |
| 79 | +```c |
| 80 | +do \ |
| 81 | +{ \ |
| 82 | + ctb_log_warning_inline( \ |
| 83 | + __FILE__, __LINE__, __func__, ctb_warning, msg, __VA_ARGS__ \ |
| 84 | + ); \ |
| 85 | +} while (0) |
| 86 | +``` |
| 87 | + |
| 88 | +#### Usage |
| 89 | +```c |
| 90 | +double some_function(void) |
| 91 | +{ |
| 92 | + CTB_LOG_WARNING_INLINE( |
| 93 | + CTB_DEPRECATION_WARNING, |
| 94 | + "Function \"%s\" is deprecated. It will be removed in the next version.", |
| 95 | + __func__ |
| 96 | + ); |
| 97 | + |
| 98 | + /* Do something */ |
| 99 | + |
| 100 | + return; |
| 101 | +} |
| 102 | +``` |
| 103 | +Output: |
| 104 | +```ansi |
| 105 | +[1;33mDeprecationWarning:[0m [38;5;240mFile "[0m[38;5;240m/home/alvinng/Desktop/c_trackback/examples/[0m[1;36mexample.c[0m[38;5;240m", line[0m [1;36m17[0m [38;5;240min[0m [1;36msome_function[0m: |
| 106 | + [5;33mFunction "some_function" is deprecated. It will be removed in the next version.[0m |
| 107 | +``` |
| 108 | + |
| 109 | +### `CTB_LOG_MESSAGE_INLINE` <Badge text="Macro" /> |
| 110 | +Wrapper for logging a message to stdout without stacktrace. |
| 111 | +```c |
| 112 | +CTB_LOG_MESSAGE_INLINE(msg, ...) |
| 113 | +``` |
| 114 | +
|
| 115 | +#### Parameters |
| 116 | +<ParamsTable |
| 117 | + params={[ |
| 118 | + { name: "msg", type: "const char *", desc: "Message." }, |
| 119 | + { name: "...", type: "N/A", desc: "Additional arguments for formatting the message." }, |
| 120 | + ]} |
| 121 | +/> |
| 122 | +
|
| 123 | +#### Expands to |
| 124 | +```c |
| 125 | +do \ |
| 126 | +{ \ |
| 127 | + ctb_log_message_inline(__FILE__, __LINE__, __func__, msg, __VA_ARGS__); \ |
| 128 | +} while (0) |
| 129 | +``` |
| 130 | + |
| 131 | +#### Usage |
| 132 | +```c |
| 133 | +void inline_message(int i) |
| 134 | +{ |
| 135 | + CTB_LOG_MESSAGE_INLINE("(Test %d) Hello, world! :)", i); |
| 136 | +} |
| 137 | +``` |
| 138 | +
|
| 139 | +Output: |
| 140 | +```ansi |
| 141 | +[1;95mMessage:[0m [38;5;240mFile "[0m[38;5;240m/home/alvinng/Desktop/c_trackback/examples/[0m[1;36mexample.c[0m[38;5;240m", line[0m [1;36m75[0m [38;5;240min[0m [1;36minline_message[0m: |
| 142 | + [0;35m(Test 0) Hello, world! :)[0m |
| 143 | +``` |
0 commit comments