| 1 |
#ifndef __CRITCL_UTIL_TRACE_H |
| 2 |
#define __CRITCL_UTIL_TRACE_H 1 |
| 3 |
|
| 4 |
/* |
| 5 |
* Copyright (c) 2017-2024 Andreas Kupries <[email protected]> |
| 6 |
* |
| 7 |
* Narrative tracing support, controlled by CRITCL_TRACER |
| 8 |
* = = == === ===== ======== ============= ===================== |
| 9 |
* |
| 10 |
* Further control of the active logical sub-streams is done via the |
| 11 |
* declarators |
| 12 |
* - TRACE_ON |
| 13 |
* - TRACE_OFF |
| 14 |
* - TRACE_TAG_ON |
| 15 |
* - TRACE_TAG_OFF |
| 16 |
* |
| 17 |
* The macros make use of the standard macros __FILE__ and __LINE__ |
| 18 |
* to identify traced locations (physically). |
| 19 |
* |
| 20 |
* ATTENTION: The trace facility assumes a C99 compiler to have |
| 21 |
* access to the __func__ string which holds the name |
| 22 |
* of the current function. |
| 23 |
* |
| 24 |
* NOTE: define CRITCL_TRACE_NOTHREADS if the tracer is run on a single-threaded |
| 25 |
* process for sure. Else leave it at the new default of multi-threaded |
| 26 |
* operation. |
| 27 |
* |
| 28 |
* In this mode it generates one `.trace` file per thread with active tracing. |
| 29 |
* In single-threaded mode it writes to stdout as before. |
| 30 |
* |
| 31 |
* NOTE 2: The above can be done through the `critcl::tracer-config` command, i.e. |
| 32 |
* invoke: |
| 33 |
* critcl::tracer-config -nothreads |
| 34 |
*/ |
| 35 |
|
| 36 |
#include <tcl.h> |
| 37 |
|
| 38 |
/* |
| 39 |
* Main (convenience) commands: |
| 40 |
* |
| 41 |
* - TRACE_FUNC :: Function entry, formatted parameters |
| 42 |
* - TRACE_FUNC_VOID :: Function entry, no parameters |
| 43 |
* - TRACE_RETURN :: Function exit, formatted result |
| 44 |
* - TRACE_RETURN_VOID :: Function exit, no result |
| 45 |
* - TRACE :: Additional trace line. |
| 46 |
* |
| 47 |
* The above commands are composed from the lower level commands below. |
| 48 |
* |
| 49 |
* Scoping |
| 50 |
* - TRACE_PUSH_SCOPE :: Start a named scope, no output |
| 51 |
* - TRACE_PUSH_FUNC :: Start a scope, named by the current function, no output |
| 52 |
* - TRACE_POP :: End a scope, no output |
| 53 |
|
| 54 |
* Tracing |
| 55 |
* - TRACE_HEADER :: Start of trace line (location, indentation, scope) |
| 56 |
* - TRACE_ADD :: Extend trace line, formatted information |
| 57 |
* - TRACE_CLOSER :: End of trace line |
| 58 |
* |
| 59 |
* All of the tracing command also come in TRACE_TAG_ forms which take an |
| 60 |
* additional 1st argument, the tag of the stream. The scoping commands do not |
| 61 |
* take tags. They manage indentation without generating output on their own. |
| 62 |
*/ |
| 63 |
|
| 64 |
#ifndef CRITCL_TRACER |
| 65 |
/* Tracing is disabled. All macros vanish / devolve to their untraced functionality. |
| 66 |
*/ |
| 67 |
|
| 68 |
#define TRACE_THREAD_EXIT TCL_THREAD_CREATE_RETURN |
| 69 |
#define TRACE_PUSH_SCOPE(string) |
| 70 |
#define TRACE_PUSH_FUNC |
| 71 |
#define TRACE_POP |
| 72 |
#define TRACE_ON |
| 73 |
#define TRACE_OFF |
| 74 |
#define TRACE_HEADER(indent) |
| 75 |
#define TRACE_ADD(format, ...) |
| 76 |
#define TRACE_CLOSER |
| 77 |
#define TRACE_TAG_ON(tag) |
| 78 |
#define TRACE_TAG_OFF(tag) |
| 79 |
#define TRACE_TAG_HEADER(tag,indent) |
| 80 |
#define TRACE_TAG_ADD(tag, format, ...) |
| 81 |
#define TRACE_TAG_CLOSER(tag) |
| 82 |
#define TRACE_FUNC(format, ...) |
| 83 |
#define TRACE_FUNC_VOID |
| 84 |
#define TRACE_RETURN(format,x) return (x); |
| 85 |
#define TRACE_RETURN_VOID return; |
| 86 |
#define TRACE(format, ...) |
| 87 |
#define TRACE_TAG_FUNC(tag, format, ...) |
| 88 |
#define TRACE_TAG_FUNC_VOID(tag) |
| 89 |
#define TRACE_TAG_RETURN(tag, format, x) return (x); |
| 90 |
#define TRACE_TAG_RETURN_VOID(tag) return; |
| 91 |
#define TRACE_TAG(tag, format, ...) |
| 92 |
#define TRACE_RUN(code) |
| 93 |
#define TRACE_DO(code) |
| 94 |
#define TRACE_TAG_DO(tag, code) |
| 95 |
#define TRACE_TAG_VAR(tag) 0 |
| 96 |
#endif |
| 97 |
|
| 98 |
#ifdef CRITCL_TRACER |
| 99 |
/* Tracing is active. All macros are properly defined. |
| 100 |
*/ |
| 101 |
#define TRACE_THREAD_EXIT TRACE ("THREAD EXIT %s", "(void)") ; TRACE_POP ; critcl_trace_thread_end() ; TCL_THREAD_CREATE_RETURN |
| 102 |
|
| 103 |
#define TRACE_PUSH_SCOPE(string) critcl_trace_push (string) |
| 104 |
#define TRACE_PUSH_FUNC TRACE_PUSH_SCOPE (__func__) |
| 105 |
#define TRACE_POP critcl_trace_pop() |
| 106 |
|
| 107 |
#define TRACE_ON TRACE_TAG_ON (THIS_FILE) |
| 108 |
#define TRACE_OFF TRACE_TAG_OFF (THIS_FILE) |
| 109 |
|
| 110 |
#define TRACE_HEADER(indent) TRACE_TAG_HEADER (THIS_FILE, indent) |
| 111 |
#define TRACE_ADD(format, ...) TRACE_TAG_ADD (THIS_FILE, format, __VA_ARGS__) |
| 112 |
#define TRACE_CLOSER TRACE_TAG_CLOSER (THIS_FILE) |
| 113 |
|
| 114 |
#define TRACE_TAG_ON(tag) static int TRACE_TAG_VAR (tag) = 1 |
| 115 |
#define TRACE_TAG_OFF(tag) static int TRACE_TAG_VAR (tag) = 0 |
| 116 |
#define TRACE_TAG_VAR(tag) __critcl_tag_ ## tag ## _status |
| 117 |
|
| 118 |
#define TRACE_TAG_HEADER(tag, indent) critcl_trace_header (TRACE_TAG_VAR (tag), (indent), __FILE__, __LINE__) |
| 119 |
#define TRACE_TAG_ADD(tag, format, ...) critcl_trace_printf (TRACE_TAG_VAR (tag), format, __VA_ARGS__) |
| 120 |
#define TRACE_TAG_CLOSER(tag) critcl_trace_closer (TRACE_TAG_VAR (tag)) |
| 121 |
|
| 122 |
/* |
| 123 |
* Highlevel (convenience) tracing support. |
| 124 |
*/ |
| 125 |
|
| 126 |
#define TRACE_FUNC(format, ...) TRACE_TAG_FUNC (THIS_FILE, format, __VA_ARGS__) |
| 127 |
#define TRACE_FUNC_VOID TRACE_TAG_FUNC_VOID (THIS_FILE) |
| 128 |
#define TRACE_RETURN(format,x) TRACE_TAG_RETURN (THIS_FILE, format, x) |
| 129 |
#define TRACE_RETURN_VOID TRACE_TAG_RETURN_VOID (THIS_FILE) |
| 130 |
#define TRACE(format, ...) TRACE_TAG (THIS_FILE, format, __VA_ARGS__) |
| 131 |
|
| 132 |
#define TRACE_TAG_FUNC(tag, format, ...) TRACE_PUSH_FUNC; TRACE_TAG_HEADER (tag,1); TRACE_TAG_ADD (tag, format, __VA_ARGS__); TRACE_TAG_CLOSER (tag) |
| 133 |
#define TRACE_TAG_FUNC_VOID(tag) TRACE_PUSH_FUNC; TRACE_TAG_HEADER (tag,1); TRACE_TAG_ADD (tag, "(%s)", "void"); TRACE_TAG_CLOSER (tag) |
| 134 |
#define TRACE_TAG_RETURN(tag, format, x) TRACE_TAG_HEADER (tag,1); TRACE_TAG_ADD (tag, "%s", "RETURN = ") ; TRACE_TAG_ADD (tag, format, x) ; TRACE_TAG_CLOSER (tag) ; TRACE_POP ; return (x) |
| 135 |
#define TRACE_TAG_RETURN_VOID(tag) TRACE_TAG_HEADER (tag,1); TRACE_TAG_ADD (tag, "RETURN %s", "(void)") ; TRACE_TAG_CLOSER (tag) ; TRACE_POP ; return |
| 136 |
#define TRACE_TAG(tag, format, ...) TRACE_TAG_HEADER (tag,1); TRACE_TAG_ADD (tag, format, __VA_ARGS__) ; TRACE_TAG_CLOSER (tag) |
| 137 |
|
| 138 |
#define TRACE_RUN(code) code |
| 139 |
#define TRACE_DO(code) TRACE_TAG_DO (THIS_FILE, code) |
| 140 |
#define TRACE_TAG_DO(tag, code) if (TRACE_TAG_VAR (tag)) { code ; } |
| 141 |
|
| 142 |
/* |
| 143 |
* Declarations for the support functions used in the macros. |
| 144 |
*/ |
| 145 |
|
| 146 |
extern void critcl_trace_push (const char* scope); |
| 147 |
extern void critcl_trace_pop (void); |
| 148 |
extern void critcl_trace_header (int on, int indent, const char *filename, int line); |
| 149 |
extern void critcl_trace_printf (int on, const char *pat, ...); |
| 150 |
extern void critcl_trace_closer (int on); |
| 151 |
extern void critcl_trace_thread_end (void); |
| 152 |
|
| 153 |
/* |
| 154 |
* Declarations for the support functions used by the |
| 155 |
* implementation of "critcl::cproc". |
| 156 |
*/ |
| 157 |
|
| 158 |
extern void critcl_trace_cmd_args (const char* scope, int oc, Tcl_Obj*const* ov); |
| 159 |
extern int critcl_trace_cmd_result (int status, Tcl_Interp* ip); |
| 160 |
|
| 161 |
#endif |
| 162 |
#endif /* __CRITCL_UTIL_TRACE_H */ |
| 163 |
|
| 164 |
/* |
| 165 |
* Local Variables: |
| 166 |
* mode: c |
| 167 |
* c-basic-offset: 4 |
| 168 |
* fill-column: 78 |
| 169 |
* End: |
| 170 |
*/ |