3 /* layout_simple - a dumb log layout */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "portable.h" /* execinfo when available */
11 #include "xbt/sysdep.h"
13 #include "gras/virtu.h" /* gras_os_myname (KILLME) */
14 #include "xbt/synchro.h" /* xbt_thread_self_name */
17 extern const char *xbt_log_priority_names[7];
20 static char *xbt_log_layout_format_doit(xbt_log_layout_t l,
22 const char *msg_fmt) {
23 static char res[2048];
24 static double begin_of_time = -1;
29 begin_of_time=gras_os_time();
40 fprintf(stderr,"Layout format (%s) ending with %%\n",(char*)l->data);
45 case 'n': /* platform-dependant line separator (LOG4J compliant) */
48 case 'e': /* plain space (SimGrid extension) */
52 case '.': /* precision specifyier */
54 q += sscanf(q,"%d",&precision);
57 case 'c': /* category name; LOG4J compliant
58 should accept a precision postfix to show the hierarchy */
60 p += sprintf(p,"%s",ev->cat->name);
62 p += sprintf(p,"%.*s",precision,ev->cat->name);
66 case 'p': /* priority name; LOG4J compliant */
68 p += sprintf(p, "%s", xbt_log_priority_names[ev->priority] );
70 p += sprintf(p, "%.*s", precision, xbt_log_priority_names[ev->priority] );
75 case 'h': /* host name; SimGrid extension */
77 p += sprintf(p, "%s", gras_os_myname());
79 p += sprintf(p, "%.*s", precision, gras_os_myname());
83 case 't': /* thread name; LOG4J compliant */
85 p += sprintf(p, "%s", xbt_thread_self_name());
87 p += sprintf(p, "%.*s", precision, xbt_thread_self_name());
91 case 'P': /* process name; SimGrid extension */
93 p += sprintf(p, "%s", xbt_procname());
95 p += sprintf(p, "%.*s", precision,xbt_procname());
99 case 'i': /* process PID name; SimGrid extension */
101 p += sprintf(p, "%d", (*xbt_getpid)());
103 p += sprintf(p, "%.*d", precision, (*xbt_getpid)());
108 case 'F': /* file name; LOG4J compliant */
110 p += sprintf(p,"%s",ev->fileName);
112 p += sprintf(p,"%.*s",precision, ev->fileName);
116 case 'l': /* location; LOG4J compliant */
118 p += sprintf(p, "%s:%d", ev->fileName, ev->lineNum);
120 p += snprintf(p, precision, "%s:%d", ev->fileName, ev->lineNum);
124 case 'L': /* line number; LOG4J compliant */
126 p += sprintf(p, "%d", ev->lineNum);
128 p += sprintf(p, "%.*d", precision, ev->lineNum);
132 case 'M': /* method (ie, function) name; LOG4J compliant */
134 p += sprintf(p, "%s", ev->functionName);
136 p += sprintf(p, "%.*s", precision, ev->functionName);
140 case 'b': /* backtrace; called %throwable in LOG4J */
141 case 'B': /* short backtrace; called %throwable{short} in LOG4J */
142 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
147 e.used = backtrace((void**)e.bt,XBT_BACKTRACE_SIZE);
151 xbt_backtrace_current(&e);
154 p += sprintf(p,"%s",e.bt_strings[2]+8);
156 p += sprintf(p,"%.*s",precision, e.bt_strings[2]+8);
160 for (i=2; i<e.used; i++)
162 p += sprintf(p,"%s\n",e.bt_strings[i]+8);
164 p += sprintf(p,"%.*s\n",precision,e.bt_strings[i]+8);
172 p+=sprintf(p,"(no backtrace on this arch)");
176 case 'd': /* date; LOG4J compliant */
178 p += sprintf(p,"%f", gras_os_time());
180 p += sprintf(p,"%.*f", precision, gras_os_time());
184 case 'r': /* application age; LOG4J compliant */
186 p += sprintf(p,"%f", gras_os_time()-begin_of_time);
188 p += sprintf(p,"%.*f", precision, gras_os_time()-begin_of_time);
193 case 'm': /* user-provided message; LOG4J compliant */
195 p += vsprintf(p, msg_fmt, ev->ap);
197 p += vsnprintf(p, precision, msg_fmt, ev->ap);
203 fprintf(stderr,"Unknown %%%c sequence in layout format (%s)\n",
216 static void xbt_log_layout_format_free(xbt_log_layout_t lay) {
219 xbt_log_layout_t xbt_log_layout_format_new(char *arg) {
220 xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t,1);
221 res->do_layout = xbt_log_layout_format_doit;
222 res->free_ = xbt_log_layout_format_free;
223 res->data = xbt_strdup((char*)arg);