Discussion:
[asterisk-dev] Regarding Unique Id In Logs
Balraj Singh
2018-07-02 07:07:09 UTC
Permalink
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.
Tech Support
2018-07-02 14:02:51 UTC
Permalink
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-***@lists.digium.com] On Behalf Of Balraj Singh
Sent: Monday, July 02, 2018 03:07 AM
To: asterisk-***@lists.digium.com
Cc: Mani Kanta Gadde <***@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.
Balraj Singh
2018-07-02 19:57:55 UTC
Permalink
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.
Post by Tech Support
Hello;
What version of Asterisk are you using? Do you want to post your
modifications to the two files?
Thanks;
John V.
Behalf Of *Balraj Singh
*Sent:* Monday, July 02, 2018 03:07 AM
*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
http://lists.digium.com/mailman/listinfo/asterisk-dev
Richard Mudgett
2018-07-02 21:02:07 UTC
Permalink
You are trying to reimplement callid[1] which has been in Asterisk since
v11. Callid
is accessible from dialplan using CHANNEL(callid)[2]. Accessibility from
the
dialplan has been in Asterisk 13 since 13.15.0 and in Asterisk 15 since it
was first
released. The callid is created when the incoming channel goes into
dialplan not
when it is bridged.

As to the approach. You do know that Asterisk is multi-threaded and can
handle
more than one call at a time? Using a simple global variable sets the
uniqueid
for ALL log messages regardless of whether that message has anything to do
with
the channel of that uniqueid.

You really should use callid rather than attempt to re-implement this
wheel. The
callid functionality was a large change to the codebase when it was added
in v11.

Richard

[1] https://wiki.asterisk.org/wiki/display/AST/Call+Identifier+Logging
[2] https://wiki.asterisk.org/wiki/display/AST/Asterisk+15+Function_CHANNEL
Post by Balraj Singh
Asterisk version : 15.4.1 ( current )
extern volatile char* getUniqueId(void);
//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
//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
//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);
//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.
Post by Tech Support
Hello;
What version of Asterisk are you using? Do you want to post your
modifications to the two files?
Thanks;
John V.
Behalf Of *Balraj Singh
*Sent:* Monday, July 02, 2018 03:07 AM
*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
http://lists.digium.com/mailman/listinfo/asterisk-dev
--
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --
asterisk-dev mailing list
http://lists.digium.com/mailman/listinfo/asterisk-dev
Balraj Singh
2018-07-03 08:49:28 UTC
Permalink
Thank you for your suggestion, We initially considered only using Call-id
as unique id to grep the logs. But there were several reason for us not to:
1) This Call-Identifier variable gets reset after we shut down asterisk and
starts again from C-00000001.
2) We're using CDR and unique-id by default as it is saved in CDR table and
so we can take unique id from cdr to grep the logs.
3) We needed unique-id to be in every log line e.g. executing Dialplan
applications, sip debug logs, rtp streaming logs, etc. So to achieve this
customisation, we had to modify logger.c file to easily grep the logs
related to a call using that id, so that we can trace / debug it if some
error occurs.

Hence, we had to re-implement this functionality to suffice our needs. So,
please kindly can you re-verify our approach to this.
Post by Richard Mudgett
You are trying to reimplement callid[1] which has been in Asterisk since
v11. Callid
is accessible from dialplan using CHANNEL(callid)[2]. Accessibility from
the
dialplan has been in Asterisk 13 since 13.15.0 and in Asterisk 15 since it
was first
released. The callid is created when the incoming channel goes into
dialplan not
when it is bridged.
As to the approach. You do know that Asterisk is multi-threaded and can
handle
more than one call at a time? Using a simple global variable sets the
uniqueid
for ALL log messages regardless of whether that message has anything to do
with
the channel of that uniqueid.
You really should use callid rather than attempt to re-implement this
wheel. The
callid functionality was a large change to the codebase when it was added
in v11.
Richard
[1] https://wiki.asterisk.org/wiki/display/AST/Call+Identifier+Logging
[2]
https://wiki.asterisk.org/wiki/display/AST/Asterisk+15+Function_CHANNEL
Post by Balraj Singh
Asterisk version : 15.4.1 ( current )
extern volatile char* getUniqueId(void);
//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
//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
//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);
//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.
Post by Tech Support
Hello;
What version of Asterisk are you using? Do you want to post your
modifications to the two files?
Thanks;
John V.
Behalf Of *Balraj Singh
*Sent:* Monday, July 02, 2018 03:07 AM
*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
http://lists.digium.com/mailman/listinfo/asterisk-dev
--
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --
asterisk-dev mailing list
http://lists.digium.com/mailman/listinfo/asterisk-dev
--
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --
asterisk-dev mailing list
http://lists.digium.com/mailman/listinfo/asterisk-dev
Richard Mudgett
2018-07-03 12:19:45 UTC
Permalink
Post by Balraj Singh
Thank you for your suggestion, We initially considered only using Call-id
1) This Call-Identifier variable gets reset after we shut down asterisk
and starts again from C-00000001.
2) We're using CDR and unique-id by default as it is saved in CDR table
and so we can take unique id from cdr to grep the logs.
3) We needed unique-id to be in every log line e.g. executing Dialplan
applications, sip debug logs, rtp streaming logs, etc. So to achieve this
customisation, we had to modify logger.c file to easily grep the logs
related to a call using that id, so that we can trace / debug it if some
error occurs.
Hence, we had to re-implement this functionality to suffice our needs. So,
please kindly can you re-verify our approach to this.
Post by Richard Mudgett
You are trying to reimplement callid[1] which has been in Asterisk since
v11. Callid
is accessible from dialplan using CHANNEL(callid)[2]. Accessibility from
the
dialplan has been in Asterisk 13 since 13.15.0 and in Asterisk 15 since
it was first
released. The callid is created when the incoming channel goes into
dialplan not
when it is bridged.
As to the approach. You do know that Asterisk is multi-threaded and can
handle
more than one call at a time? Using a simple global variable sets the
uniqueid
for ALL log messages regardless of whether that message has anything to
do with
the channel of that uniqueid.
Nothing has changed. It is not a good approach. Your code assumes that
there is one
and only one call happening at a time. You have to associate the uniqueid
with EVERY
log message when the log message is posted just like callid does. This was
why the callid
patch was so large.

Richard
Balraj Singh
2018-07-03 12:54:17 UTC
Permalink
Sorry if I confused you, but I tested my code with concurrent calls
happening at the same time and I'm able to get the UniqueId ( associated
with a call) in every log line ( associated with that call ) for every
call that was happening.

What I meant is, I've tested my approach with multiple calls and the unique
id that is generated for each call are printed in the logs associated to
that call.
So what do you think?
Post by Richard Mudgett
Post by Balraj Singh
Thank you for your suggestion, We initially considered only using Call-id
1) This Call-Identifier variable gets reset after we shut down asterisk
and starts again from C-00000001.
2) We're using CDR and unique-id by default as it is saved in CDR table
and so we can take unique id from cdr to grep the logs.
3) We needed unique-id to be in every log line e.g. executing Dialplan
applications, sip debug logs, rtp streaming logs, etc. So to achieve this
customisation, we had to modify logger.c file to easily grep the logs
related to a call using that id, so that we can trace / debug it if some
error occurs.
Hence, we had to re-implement this functionality to suffice our needs.
So, please kindly can you re-verify our approach to this.
Post by Richard Mudgett
You are trying to reimplement callid[1] which has been in Asterisk since
v11. Callid
is accessible from dialplan using CHANNEL(callid)[2]. Accessibility
from the
dialplan has been in Asterisk 13 since 13.15.0 and in Asterisk 15 since
it was first
released. The callid is created when the incoming channel goes into
dialplan not
when it is bridged.
As to the approach. You do know that Asterisk is multi-threaded and can
handle
more than one call at a time? Using a simple global variable sets the
uniqueid
for ALL log messages regardless of whether that message has anything to
do with
the channel of that uniqueid.
Nothing has changed. It is not a good approach. Your code assumes that
there is one
and only one call happening at a time. You have to associate the uniqueid
with EVERY
log message when the log message is posted just like callid does. This
was why the callid
patch was so large.
Richard
--
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --
asterisk-dev mailing list
http://lists.digium.com/mailman/listinfo/asterisk-dev
Dennis Buteyn
2018-07-03 14:34:23 UTC
Permalink
I think you do not understand what the effect of multi-threading has on
static variables.

The unique IDs you are seeing are whatever the unique ID happened to be
at the time the message was formatted for output. This is not
necessarily the unique ID of the call that wanted to generate that log
message as the static variable gets stomped on repeatedly by other threads.

You may want to know more about the concept of "race condition".

Dennis Buteyn
Xorcom Ltd
Post by Balraj Singh
Sorry if I confused you, but I tested my code with concurrent calls
happening at the same time and I'm able to get the UniqueId (
associated with a call) in every log line ( associated with that call
)  for every call that was happening.
What I meant is, I've tested my approach with multiple calls and the
unique id that is generated for each call are printed in the logs
associated to that call.
So what do you think?
On Tue, Jul 3, 2018 at 3:49 AM, Balraj Singh
Thank you for your suggestion, We initially considered only
using Call-id as unique id to grep the logs. But there were
1) This Call-Identifier variable gets reset after we shut down
asterisk and starts again from C-00000001.
2) We're using CDR and unique-id by default as it is saved in
CDR table and so we can take unique id from cdr to grep the logs.
3) We needed unique-id to be in every log line e.g. executing
Dialplan applications, sip debug logs, rtp streaming logs,
etc. So to achieve this customisation, we had to modify
logger.c file to easily grep the logs related to a call using
that id, so that we can trace / debug it if some error occurs.
Hence, we had to re-implement this functionality to suffice
our needs. So, please kindly can you re-verify our approach to
this.
On Tue, Jul 3, 2018 at 2:32 AM Richard Mudgett
You are trying to reimplement callid[1] which has been in
Asterisk since v11. Callid
is accessible from dialplan using CHANNEL(callid)[2]. 
Accessibility from the
dialplan has been in Asterisk 13 since 13.15.0 and in
Asterisk 15 since it was first
released. The callid is created when the incoming channel
goes into dialplan not
when it is bridged.
As to the approach.  You do know that Asterisk is
multi-threaded and can handle
more than one call at a time?  Using a simple global
variable sets the uniqueid
for ALL log messages regardless of whether that message
has anything to do with
the channel of that uniqueid.
Nothing has changed.  It is not a good approach. Your code assumes
that there is one
and only one call happening at a time.  You have to associate the
uniqueid with EVERY
log message when the log message is posted just like callid does. 
This was why the callid
patch was so large.
Richard
--
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --
asterisk-dev mailing list
http://lists.digium.com/mailman/listinfo/asterisk-dev
Balraj Singh
2018-07-04 12:40:50 UTC
Permalink
I know the working of static variables in Multi-threaded environment, but I
never used static variables ( please check the code sent in one of my
previous reply ) to save the unique-id. I'm using volatile keyword which is
completely different from static variable ( one of the storage classes in c
), and the usage of volatile keyword here is to tell the compiler to not
perform optimisation ( while compilation ) on this variable. This is useful
in multi-threaded environment, as in global variable should be declared
volatile[1] in this type of environment for shared global variables.

Regarding the second comment about unique-id being whatever at some point
of time : let me clear this thing, I already stated in my last reply that
I'm getting unique-id ( belonging to a call ) in logs correctly even in
concurrent calls ( tested with more than 1 call ).

Please correct me if I'm wrong here.
@Richard Mudgett : Please validate the changes I did.
[1]
<https://docs.google.com/presentation/d/11LC-_e1EGL3X-n7gU_Z2uSNvPX41t6LVngO9AxBi4wo/edit?usp=sharing>

Thank you all.
Balraj Singh
2018-07-04 12:41:50 UTC
Permalink
Sorry the reference link is wrong in my last mail.
Correct one : [1]
<https://www.embedded.com/electronics-blogs/beginner-s-corner/4023801/Introduction-to-the-Volatile-Keyword>

Thank you.
Post by Balraj Singh
I know the working of static variables in Multi-threaded environment, but
I never used static variables ( please check the code sent in one of my
previous reply ) to save the unique-id. I'm using volatile keyword which is
completely different from static variable ( one of the storage classes in c
), and the usage of volatile keyword here is to tell the compiler to not
perform optimisation ( while compilation ) on this variable. This is useful
in multi-threaded environment, as in global variable should be declared
volatile[1] in this type of environment for shared global variables.
Regarding the second comment about unique-id being whatever at some point
of time : let me clear this thing, I already stated in my last reply that
I'm getting unique-id ( belonging to a call ) in logs correctly even in
concurrent calls ( tested with more than 1 call ).
Please correct me if I'm wrong here.
@Richard Mudgett : Please validate the changes I did.
[1]
<https://docs.google.com/presentation/d/11LC-_e1EGL3X-n7gU_Z2uSNvPX41t6LVngO9AxBi4wo/edit?usp=sharing>
Thank you all.
--
*Thanks, *
*Balraj.*
Balraj Singh
2018-07-05 06:08:32 UTC
Permalink
Thank you so much! That was quite a thorough explanation. I understood now
what you were trying to imply.
So, I'm going to do some research on how to make my code bulletproof. In
the mean time, can you suggest something on how to make this happen? .
I'll get back to you, once I've figured out this thing.

Thank you again,
Balraj.
Thank you so much! That was quite a thorough explanation. I understood now
what you were trying to imply.
So, I'm going to do some research on how to make my code bulletproof. In
the mean time, can you suggest something on how to make this happen? .
I'll get back to you, once I've figured out this thing.
Thank you again,
Balraj.
Volatile only hints the compiler not to perform read optimizations, to
assume that the value is changed by some external process. This keyword was
designed for use in hardware drivers where memory-mapped devices would
alter values in-memory of which the compiler was not aware of. The volatile
keyword however later found a place in multi-processor systems as well
where memory is shared between execution units. The important thing to
understand however is that it only disabled read operations. It does not
provide any safety in regards to concurrent writes. This is what both me
and Richard were trying to explain to you.
When two concurrent updates are performed on the same variable (making it
volatile does not matter) the final value will be that of whichever update
came last. Since no control mechanism is in place to ensure proper
ordering, this is no different than throwing dice.
To ensure proper ordering of writes and to prevent modification by other
execution units you must use locks, of which you have none.
1. The system you are testing on is not a true SMP system.
2. Process affinity restrictions apply, forcing your application to
run exclusively on a single execution unit.
3. Your test calls are not actually concurrent.
4. Your test calls are too slow.
5. One of the many locks that Asterisk uses to prevent corruption of
its internal data structures is making your changes work.
6. Luck.
I understand you are getting "a" unique ID, what you fail to understand
is that it will not be the one you are looking for.
Compile the following code on gcc with default options and watch it crash
instantly despite looking "correct", you will understand what we are trying
to tell you.
#include <pthread.h>
#include <stdio.h>
static volatile char * shared_var = NULL;
void * threadfunc(void * ptr) {
for (;;) {
shared_var = "mystring";
char c = *shared_var;
shared_var = NULL;
}
return NULL;
}
int main() {
pthread_t t1, t2;
pthread_create(&t1, NULL, threadfunc, NULL);
pthread_create(&t2, NULL, threadfunc, NULL); // comment this line to
make it "work".
pthread_join(t1, NULL);
pthread_join(t2, NULL);
}
Dennis Buteyn
Xorcom Ltd
I know the working of static variables in Multi-threaded environment, but
I never used static variables ( please check the code sent in one of my
previous reply ) to save the unique-id. I'm using volatile keyword which is
completely different from static variable ( one of the storage classes in c
), and the usage of volatile keyword here is to tell the compiler to not
perform optimisation ( while compilation ) on this variable. This is useful
in multi-threaded environment, as in global variable should be declared
volatile[1] in this type of environment for shared global variables.
Regarding the second comment about unique-id being whatever at some point
of time : let me clear this thing, I already stated in my last reply that
I'm getting unique-id ( belonging to a call ) in logs correctly even in
concurrent calls ( tested with more than 1 call ).
Please correct me if I'm wrong here.
@Richard Mudgett : Please validate the changes I did.
[1]
<https://docs.google.com/presentation/d/11LC-_e1EGL3X-n7gU_Z2uSNvPX41t6LVngO9AxBi4wo/edit?usp=sharing>
Thank you all.
--
*Thanks, *
*Balraj.*
--
*Thanks, *
*Balraj.*
Loading...