[asterisk-dev] Regarding Unique Id In Logs

Balraj Singh balraj.singh at zemosolabs.com
Mon Jul 2 14:57:55 CDT 2018


Asterisk version : 15.4.1 ( current )

Ok so I added in :
cdr.h :

extern volatile char* getUniqueId(void);

cdr.c :

//save unique id in this variable
//this is a global variable
volatile char *UNIQUEID;

//function to get unique id
volatile char *getUniqueId() {
return UNIQUEID;
}

//start saving unique id from channel

static struct cdr_object *cdr_object_alloc(struct ast_channel_snapshot
*chan) {
....
UNIQUEID = chan->uniqueid;
....
}

static struct cdr_object *cdr_object_create_and_append(struct cdr_object
*cdr) {
....
if (!UNIQUEID || (UNIQUEID[0] != '\0')) {
UNIQUEID = cdr_last->uniqueid;
}
....
}

static struct ast_cdr *cdr_object_create_public_records(struct cdr_object
*cdr) {
....
ast_copy_string(UNIQUEID, party_a->uniqueid, sizeof(party_a->uniqueid));
....
}

static void handle_channel_cache_message(void *data, struct
stasis_subscription *sub, struct stasis_message *message) {
....
  UNIQUEID = new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid;
....
} //end saving unique id

logger.c :

//include cdr header file
#include "asterisk/cdr.h"

static int format_log_default(struct logchannel *chan, struct logmsg *msg,
char *buf, size_t size) { char call_identifier_str[13];

//Get unique id from cdr here volatile char *UNIQUEID = getUniqueId(); if
(msg->callid) { snprintf(call_identifier_str, sizeof(call_identifier_str),
"[C-%08x]", msg->callid); } else { call_identifier_str[0] = '\0'; } switch
(chan->type) { case LOGTYPE_SYSLOG:

//Print unique id along with other log content
snprintf(buf, size, "%s[%d][%s]: %s:%d in %s: %s", levels[msg->level],
msg->lwp, UNIQUEID, msg->file, msg->line, msg->function, msg->message);
term_strip(buf, buf, size); break; case LOGTYPE_FILE:

//Print unique id along with other log content

snprintf(buf, size, "[%s] %s[%d][%s] %s: %s",
msg->date, msg->level_name, msg->lwp, UNIQUEID, msg->file, msg->message);
term_strip(buf, buf, size); break; case LOGTYPE_CONSOLE: { char
linestr[32]; /* * Verbose messages are interpreted by console channels in
their own * special way */ if (msg->level == __LOG_VERBOSE) { return
logger_add_verbose_magic(msg, buf, size); } /* Turn the numeric line number
into a string for colorization */ snprintf(linestr, sizeof(linestr), "%d",
msg->line); snprintf(buf, size, "[%s] " COLORIZE_FMT "[%d][%s]: "
COLORIZE_FMT ":" COLORIZE_FMT " " COLORIZE_FMT ": %s", msg->date,
COLORIZE(colors[msg->level], 0, msg->level_name), msg->lwp,

//add unique id here too UNIQUEID,
COLORIZE(COLOR_BRWHITE, 0, msg->file), COLORIZE(COLOR_BRWHITE, 0, linestr),
COLORIZE(COLOR_BRWHITE, 0, msg->function), msg->message); } break; } return
0; }

void __ast_verbose(const char *file, int line, const char *func, int level,
const char *fmt, ...) {
    ast_callid callid;
    va_list ap;

    callid = ast_read_threadstorage_callid();

    va_start(ap, fmt);

//get the unique id
    const char *UNIQUEID = getUniqueId();
//check if unique id is null or empty and print debug statement accordingly
    if (UNIQUEID) {
        if (UNIQUEID[0] != '\0') {
            printf("[%s]", UNIQUEID);
        } else {
            printf("UNIQUEID IS EMPTY ");
        }
    }

    __ast_verbose_ap(file, line, func, level, callid, fmt, ap);
    va_end(ap);
}



Thank you.



On Mon, Jul 2, 2018 at 7:33 PM Tech Support <asterisk at voipbusiness.us>
wrote:

> Hello;
>
>     What version of Asterisk are you using? Do you want to post your
> modifications to the two files?
>
> Thanks;
>
> John V.
>
>
>
> *From:* asterisk-dev [mailto:asterisk-dev-bounces at lists.digium.com] *On
> Behalf Of *Balraj Singh
> *Sent:* Monday, July 02, 2018 03:07 AM
> *To:* asterisk-dev at lists.digium.com
> *Cc:* Mani Kanta Gadde <manikanta.gadde at zemosolabs.com>
> *Subject:* [asterisk-dev] Regarding Unique Id In Logs
>
>
>
> Hi,
>
> Problem Statement : How to print $(UNIQUEID) in every log line.
>
> So we posted this query on asterisk community (here
> <https://community.asterisk.org/t/how-to-print-uniqueid-in-every-log-line/75041>),
> and I was able to get that uniqueid in every log line by modifying the
> source code of logger.c and cdr.c files of asterisk.
>
> So further we had a doubt that the way we approached to that solution was
> correct or not? . The developers on that forum told us to post this in
> asterisk-dev community as this was unrelated to that forum. So, please tell
> us, that the approach ( please read the approach in the link provided ) we
> used is correct or not.
>
> Technically what could be the caveats related to the approach if any?,
> could there be any alternates of that? .
>
>
>
> Thank you.
> --
> _____________________________________________________________________
> -- Bandwidth and Colocation Provided by http://www.api-digital.com --
>
> asterisk-dev mailing list
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-dev/attachments/20180703/66133f74/attachment-0001.html>


More information about the asterisk-dev mailing list