Created
May 30, 2024 22:46
-
-
Save aryairani/4368e7613dab73e508a933c3f3c62006 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdarg.h> | |
#include <string.h> | |
typedef struct Node { | |
char *header; | |
struct Node **children; | |
int children_count; | |
} Node; | |
Node *makeTree(char *header, int children_count, ...) { | |
Node *node = (Node *)malloc(sizeof(Node)); | |
node->header = header; | |
node->children_count = children_count; | |
node->children = (Node **)malloc(children_count * sizeof(Node *)); | |
va_list args; | |
va_start(args, children_count); | |
for (int i = 0; i < children_count; i++) { | |
node->children[i] = va_arg(args, Node *); | |
} | |
va_end(args); | |
return node; | |
} | |
void printSimple(char *str) { | |
printf("%s", str); | |
} | |
void printHideButton() { | |
printf("\u25BC"); | |
} | |
void printElementSeparator() { | |
printf(" | "); | |
} | |
void startLogicalBlock() { | |
printf("{ "); | |
} | |
void endLogicalBlock() { | |
printf(" }"); | |
} | |
void printObject(void *obj, int isNode) { | |
if (isNode) { | |
Node *node = (Node *)obj; | |
startLogicalBlock(); | |
printHideButton(); | |
printf("%s", node->header); | |
for (int i = 0; i < node->children_count; i++) { | |
printElementSeparator(); | |
printObject(node->children[i], 1); | |
} | |
endLogicalBlock(); | |
} else { | |
printSimple((char *)obj); | |
} | |
} | |
int main() { | |
Node *obj = makeTree("info: GET /foo/endpoint", 4, | |
makeTree("info: user <user ID> valid auth", 0), | |
makeTree("info: request to service B", 1, | |
makeTree("info: request took 1 seconds", 0)), | |
makeTree("info: request to service B", 3, | |
makeTree("debug: opening new connection\nnext line", 0), | |
makeTree("debug: success, result = ...", 0), | |
makeTree("info: request took 1 seconds", 0)), | |
makeTree("info: preparing result took 1 seconds", 0), | |
makeTree("info: http request took 3 seconds abcdefghi", 0) | |
); | |
printObject(obj, 1); | |
printf("\n"); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment