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 */
15 #include "xbt/xbt_os_time.h" /* xbt_os_time */
18 extern const char *xbt_log_priority_names[7];
21 static char *xbt_log_layout_format_doit(xbt_log_layout_t l,
23 const char *msg_fmt) {
24 static char res[2048];
25 static double begin_of_time = -1;
30 begin_of_time=xbt_os_time();
41 fprintf(stderr,"Layout format (%s) ending with %%\n",(char*)l->data);
46 case 'n': /* platform-dependant line separator (LOG4J compliant) */
49 case 'e': /* plain space (SimGrid extension) */
53 case '.': /* precision specifyier */
55 q += sscanf(q,"%d",&precision);
58 case 'c': /* category name; LOG4J compliant
59 should accept a precision postfix to show the hierarchy */
61 p += sprintf(p,"%s",ev->cat->name);
63 p += sprintf(p,"%.*s",precision,ev->cat->name);
67 case 'p': /* priority name; LOG4J compliant */
69 p += sprintf(p, "%s", xbt_log_priority_names[ev->priority] );
71 p += sprintf(p, "%.*s", precision, xbt_log_priority_names[ev->priority] );
76 case 'h': /* host name; SimGrid extension */
78 p += sprintf(p, "%s", gras_os_myname());
80 p += sprintf(p, "%.*s", precision, gras_os_myname());
84 case 't': /* thread name; LOG4J compliant */
86 p += sprintf(p, "%s", xbt_thread_self_name());
88 p += sprintf(p, "%.*s", precision, xbt_thread_self_name());
92 case 'P': /* process name; SimGrid extension */
94 p += sprintf(p, "%s", xbt_procname());
96 p += sprintf(p, "%.*s", precision,xbt_procname());
100 case 'i': /* process PID name; SimGrid extension */
102 p += sprintf(p, "%d", (*xbt_getpid)());
104 p += sprintf(p, "%.*d", precision, (*xbt_getpid)());
109 case 'F': /* file name; LOG4J compliant */
111 p += sprintf(p,"%s",ev->fileName);
113 p += sprintf(p,"%.*s",precision, ev->fileName);
117 case 'l': /* location; LOG4J compliant */
119 p += sprintf(p, "%s:%d", ev->fileName, ev->lineNum);
121 p += snprintf(p, precision, "%s:%d", ev->fileName, ev->lineNum);
125 case 'L': /* line number; LOG4J compliant */
127 p += sprintf(p, "%d", ev->lineNum);
129 p += sprintf(p, "%.*d", precision, ev->lineNum);
133 case 'M': /* method (ie, function) name; LOG4J compliant */
135 p += sprintf(p, "%s", ev->functionName);
137 p += sprintf(p, "%.*s", precision, ev->functionName);
141 case 'b': /* backtrace; called %throwable in LOG4J */
142 case 'B': /* short backtrace; called %throwable{short} in LOG4J */
143 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
148 e.used = backtrace((void**)e.bt,XBT_BACKTRACE_SIZE);
152 xbt_backtrace_current(&e);
155 p += sprintf(p,"%s",e.bt_strings[2]+8);
157 p += sprintf(p,"%.*s",precision, e.bt_strings[2]+8);
161 for (i=2; i<e.used; i++)
163 p += sprintf(p,"%s\n",e.bt_strings[i]+8);
165 p += sprintf(p,"%.*s\n",precision,e.bt_strings[i]+8);
173 p+=sprintf(p,"(no backtrace on this arch)");
177 case 'd': /* date; LOG4J compliant */
179 p += sprintf(p,"%f", xbt_os_time());
181 p += sprintf(p,"%.*f", precision, xbt_os_time());
185 case 'r': /* application age; LOG4J compliant */
187 p += sprintf(p,"%f", xbt_os_time()-begin_of_time);
189 p += sprintf(p,"%.*f", precision, xbt_os_time()-begin_of_time);
194 case 'm': /* user-provided message; LOG4J compliant */
196 p += vsprintf(p, msg_fmt, ev->ap);
198 p += vsnprintf(p, precision, msg_fmt, ev->ap);
204 fprintf(stderr,"Unknown %%%c sequence in layout format (%s)\n",
217 static void xbt_log_layout_format_free(xbt_log_layout_t lay) {
220 xbt_log_layout_t xbt_log_layout_format_new(char *arg) {
221 xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t,1);
222 res->do_layout = xbt_log_layout_format_doit;
223 res->free_ = xbt_log_layout_format_free;
224 res->data = xbt_strdup((char*)arg);