Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbg: add dbg_printf_id #946

Closed
wants to merge 1 commit into from
Closed

dbg: add dbg_printf_id #946

wants to merge 1 commit into from

Conversation

sreimers
Copy link
Member

@sreimers sreimers commented Sep 6, 2023

No description provided.

@sreimers sreimers added this to the v3.6.0 milestone Sep 6, 2023
@alfredh
Copy link
Contributor

alfredh commented Sep 7, 2023

is unsigned 32-bit generic enough for all cases ?

Perhaps "string" is an option ?

In the past when I was debugging a server with hundreds of calls, we had a
session ID for each call, which was a UUID. The first 4-5 chars of the UUID was used
in the debug tag, it was usually unique enough.

@sreimers
Copy link
Member Author

sreimers commented Sep 7, 2023

The problem I see with a string is that this is maybe harder to handle, the basic idea I have in mind is to add setter functions to relevant objects:

struct icem {
...
	uint32_t debug_id;
}

void icem_set_debug_id(uint32_t debug_id);

static struct ice_candpair *construct_valid_pair(struct icem *icem,
					     struct ice_candpair *cp,
					     const struct sa *mapped,
					     const struct sa *dest)
{
	uint32_t debug_id = icem->debug_id;
...

	if (!lcand) {
		DEBUG_WARNING_ID("no such local candidate: %J\n", mapped);
		return NULL;
	}
	if (!rcand) {
		DEBUG_WARNING_ID("no such remote candidate: %J\n", dest);
		return NULL;
	}
...
}

Maybe a fixed length could work?

#define DEBUG_ID_LEN 8;

struct icem {
...
	char debug_id[DEBUG_ID_LEN];
}

void icem_set_debug_id(char *debug_id);

static struct ice_candpair *construct_valid_pair(struct icem *icem,
					     struct ice_candpair *cp,
					     const struct sa *mapped,
					     const struct sa *dest)
{
	char *debug_id = icem->debug_id;
...

	if (!lcand) {
		DEBUG_WARNING_ID("no such local candidate: %J\n", mapped);
		return NULL;
	}
	if (!rcand) {
		DEBUG_WARNING_ID("no such remote candidate: %J\n", dest);
		return NULL;
	}
...
}

@alfredh
Copy link
Contributor

alfredh commented Sep 9, 2023

there is also an option to implement this without changes in DBG module.

You can define a session-id string, for example SIP Call-ID or webrtc session-id.
This string is the passed manually to all relevant modules.

Each module, for example ICE, will add this string to all debug messages.

When you session is finished, you can take the logfile and grep for the session-id.

@sreimers
Copy link
Member Author

sreimers commented Sep 9, 2023

One benefit is with dbg_printf_id custom log handlers for example can generate separate logfiles per call/session or if you use central logging like graylog or grafana loki, you can use the identifier as indexed key value metric, without parsing the string.

@sreimers
Copy link
Member Author

I refactored the PR with struct pl instead of uint32_t. This should be more flexible.

dbg_printf(DBG_WARNING, DEBUG_MODULE ": " __VA_ARGS__)
#define DEBUG_WARNING_ID(...) \
dbg_printf_id(debug_id, DBG_WARNING, DEBUG_MODULE ": " __VA_ARGS__)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it better to pass "debug_id" via the macro parameters, instead of assume the name ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It follows the same technique used for DEBUG_LEVEL and DEBUG_MODULE.
The compiler errors if debug_id is not defined. I think it's much easier for rewriting existing DEBUG code since you have only define debug_id and add _ID. And this needs less reformatting new lines, that was the idea behind it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider uppercase: DEBUG_ID ?

@alfredh
Copy link
Contributor

alfredh commented Sep 17, 2023

as an example usecase, this ID could be used by baresip/src/call.c

with multiple calls, each call has a unique ID. Perhaps also consider options to turn it on/off ?

src/async/async.c Outdated Show resolved Hide resolved
{
(void)level;
(void)arg;
(void)id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the id (aka "tag") be part of the log line ?

@sreimers sreimers force-pushed the debug_id branch 2 times, most recently from 46eed9a to c6e302b Compare October 5, 2023 12:14
@alfredh alfredh removed this from the v3.6.0 milestone Oct 5, 2023
@@ -130,7 +130,7 @@ void dbg_handler_set(dbg_print_h *ph, void *arg)


/* NOTE: This function should not allocate memory */
static void dbg_vprintf(int level, const char *fmt, va_list ap)
static void dbg_vprintf(struct pl *id, int level, const char *fmt, va_list ap)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const for id ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a custom log handler should able to call mem_ref(id), this discards the const qualifier.

@alfredh
Copy link
Contributor

alfredh commented Mar 16, 2024

Should we merge this one to main, or close it ?

@alfredh
Copy link
Contributor

alfredh commented Aug 3, 2024

this can probably be merged now

Consider the name "ID" or "TAG" for your change.

Another thing we should fix is to avoid double locking and formatting here:

void dbg_printf(int level, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	dbg_vprintf(level, fmt, ap);
	va_end(ap);

	va_start(ap, fmt);
	dbg_fmt_vprintf(level, fmt, ap);
	va_end(ap);
}

@cspiel1 cspiel1 added this to the v3.17.0 milestone Oct 2, 2024
@alfredh alfredh removed this from the v3.17.0 milestone Oct 28, 2024
@sreimers sreimers closed this Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants