From: mquinson Date: Fri, 8 Jun 2007 06:31:52 +0000 (+0000) Subject: Implement a format precision modifier X-Git-Tag: v3.3~1779 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/aefa13b20fbf858a796d4bd1573bd5e8fa9f7212 Implement a format precision modifier git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3581 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/xbt/xbt_log_layout_format.c b/src/xbt/xbt_log_layout_format.c index 0f86c80516..3321e5aca9 100644 --- a/src/xbt/xbt_log_layout_format.c +++ b/src/xbt/xbt_log_layout_format.c @@ -22,7 +22,8 @@ static char *xbt_log_layout_format_doit(xbt_log_layout_t l, const char *msg_fmt) { static char res[2048]; static double begin_of_time = -1; - char *p,*q; + char *p,*q; + int precision=-1; if (begin_of_time<0) begin_of_time=gras_os_time(); @@ -33,6 +34,7 @@ static char *xbt_log_layout_format_doit(xbt_log_layout_t l, while (*q != '\0') { if (*q == '%') { q++; + handle_modifier: switch (*q) { case '\0': THROW1(arg_error,0,"Layout format (%s) ending with %%",(char*)l->data); @@ -42,39 +44,89 @@ static char *xbt_log_layout_format_doit(xbt_log_layout_t l, case 'n': /* platform-dependant line separator (LOG4J compliant) */ p += sprintf(p,"\n"); break; - case ' ': /* plain space (SimGrid extension) */ + case 'e': /* plain space (SimGrid extension) */ p += sprintf(p," "); break; + + case '.': /* precision specifyier */ + q++; + q += sscanf(q,"%d",&precision); + goto handle_modifier; case 'c': /* category name; LOG4J compliant should accept a precision postfix to show the hierarchy */ - p += sprintf(p,"%s",ev->cat->name); + if (precision == -1) + p += sprintf(p,"%s",ev->cat->name); + else { + p += sprintf(p,"%.*s",precision,ev->cat->name); + precision = -1; + } break; case 'p': /* priority name; LOG4J compliant */ - p += sprintf(p, "%s", xbt_log_priority_names[ev->priority] ); + if (precision == -1) + p += sprintf(p, "%s", xbt_log_priority_names[ev->priority] ); + else { + p += sprintf(p, "%.*s", precision, xbt_log_priority_names[ev->priority] ); + precision = -1; + } break; case 'h': /* host name; SimGrid extension */ - p += sprintf(p, "%s", gras_os_myname()); + if (precision == -1) + p += sprintf(p, "%s", gras_os_myname()); + else { + p += sprintf(p, "%.*s", precision, gras_os_myname()); + precision = -1; + } break; case 't': /* process name; LOG4J compliant (thread name) */ - p += sprintf(p, "%s", xbt_procname()); + if (precision == -1) + p += sprintf(p, "%s", xbt_procname()); + else { + p += sprintf(p, "%.*s", precision,xbt_procname()); + precision = -1; + } break; case 'i': /* process PID name; SimGrid extension */ - p += sprintf(p, "%ld", gras_os_getpid()); + if (precision == -1) + p += sprintf(p, "%ld", gras_os_getpid()); + else { + p += sprintf(p, "%.*ld", precision, gras_os_getpid()); + precision = -1; + } break; case 'F': /* file name; LOG4J compliant */ - p += sprintf(p,"%s",ev->fileName); + if (precision == -1) + p += sprintf(p,"%s",ev->fileName); + else { + p += sprintf(p,"%.*s",precision, ev->fileName); + precision = -1; + } break; case 'l': /* location; LOG4J compliant */ - p += sprintf(p, "%s:%d", ev->fileName, ev->lineNum); + if (precision == -1) + p += sprintf(p, "%s:%d", ev->fileName, ev->lineNum); + else { + p += snprintf(p, precision, "%s:%d", ev->fileName, ev->lineNum); + precision = -1; + } break; case 'L': /* line number; LOG4J compliant */ - p += sprintf(p, "%d", ev->lineNum); + if (precision == -1) + p += sprintf(p, "%d", ev->lineNum); + else { + p += sprintf(p, "%.*d", precision, ev->lineNum); + precision = -1; + } break; case 'M': /* method (ie, function) name; LOG4J compliant */ - p += sprintf(p, "%s", ev->functionName); + if (precision == -1) + p += sprintf(p, "%s", ev->functionName); + else { + p += sprintf(p, "%.*s", precision, ev->functionName); + precision = -1; + } break; case 'b': /* backtrace; called %throwable in LOG4J */ case 'B': /* short backtrace; called %throwable{short} in LOG4J */ @@ -85,15 +137,26 @@ static char *xbt_log_layout_format_doit(xbt_log_layout_t l, e.used = backtrace((void**)e.bt,XBT_BACKTRACE_SIZE); e.bt_strings = NULL; - xbt_ex_setup_backtrace(&e); - if (*q=='B') - p += sprintf(p,"%s",e.bt_strings[2]+8); - else - for (i=2; iap); + if (precision == -1) + p += vsprintf(p, msg_fmt, ev->ap); + else { + p += vsnprintf(p, precision, msg_fmt, ev->ap); + precision = -1; + } break; default: