Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement a format precision modifier
[simgrid.git] / src / xbt / xbt_log_layout_format.c
1 /* $Id$ */
2
3 /* layout_simple - a dumb log layout                                        */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
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. */
9
10 #include "portable.h" /* execinfo when available */
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "gras/virtu.h"
14 #include <stdio.h>
15 #include "xbt/ex_interface.h" /* backtraces */
16
17 extern const char *xbt_log_priority_names[7];
18
19
20 static char *xbt_log_layout_format_doit(xbt_log_layout_t l,
21                                         xbt_log_event_t ev, 
22                                         const char *msg_fmt) {
23   static char res[2048];
24   static double begin_of_time = -1;
25   char *p,*q;
26   int precision=-1;
27
28   if (begin_of_time<0) 
29     begin_of_time=gras_os_time();
30
31   p = res;
32   q = l->data;
33
34   while (*q != '\0') {
35     if (*q == '%') {
36       q++;
37        handle_modifier:
38       switch (*q) {
39       case '\0':
40         THROW1(arg_error,0,"Layout format (%s) ending with %%",(char*)l->data);
41       case '%':
42         *p++ = '%';
43         break;
44       case 'n': /* platform-dependant line separator (LOG4J compliant) */
45         p += sprintf(p,"\n");
46         break;
47       case 'e': /* plain space (SimGrid extension) */
48         p += sprintf(p," ");
49         break;
50          
51       case '.': /* precision specifyier */
52         q++;
53         q += sscanf(q,"%d",&precision);
54         goto handle_modifier;
55
56       case 'c': /* category name; LOG4J compliant
57                    should accept a precision postfix to show the hierarchy */
58         if (precision == -1)
59            p += sprintf(p,"%s",ev->cat->name);
60         else {        
61            p += sprintf(p,"%.*s",precision,ev->cat->name);
62            precision = -1;
63         }        
64         break;
65       case 'p': /* priority name; LOG4J compliant */    
66         if (precision == -1)
67            p += sprintf(p, "%s", xbt_log_priority_names[ev->priority] );
68         else {        
69            p += sprintf(p, "%.*s", precision, xbt_log_priority_names[ev->priority] );
70            precision = -1;
71         }        
72         break;
73
74       case 'h': /* host name; SimGrid extension */
75         if (precision == -1)
76            p += sprintf(p, "%s", gras_os_myname());
77         else {        
78            p += sprintf(p, "%.*s", precision, gras_os_myname());
79            precision = -1;
80         }        
81         break;
82       case 't': /* process name; LOG4J compliant (thread name) */
83         if (precision == -1)
84            p += sprintf(p, "%s", xbt_procname());
85         else {        
86            p += sprintf(p, "%.*s", precision,xbt_procname());
87            precision = -1;
88         }        
89         break;
90       case 'i': /* process PID name; SimGrid extension */
91         if (precision == -1)
92            p += sprintf(p, "%ld", gras_os_getpid());
93         else {        
94            p += sprintf(p, "%.*ld", precision, gras_os_getpid());
95            precision = -1;
96         }        
97         break;
98
99       case 'F': /* file name; LOG4J compliant */
100         if (precision == -1)
101            p += sprintf(p,"%s",ev->fileName);
102         else {        
103            p += sprintf(p,"%.*s",precision, ev->fileName);
104            precision = -1;
105         }        
106         break;
107       case 'l': /* location; LOG4J compliant */
108         if (precision == -1)
109            p += sprintf(p, "%s:%d", ev->fileName, ev->lineNum);
110         else {        
111            p += snprintf(p, precision, "%s:%d", ev->fileName, ev->lineNum);
112            precision = -1;
113         }        
114         break;
115       case 'L': /* line number; LOG4J compliant */
116         if (precision == -1)
117            p += sprintf(p, "%d", ev->lineNum);
118         else {        
119            p += sprintf(p, "%.*d", precision, ev->lineNum);
120            precision = -1;
121         }        
122         break;
123       case 'M': /* method (ie, function) name; LOG4J compliant */
124         if (precision == -1)
125            p += sprintf(p, "%s", ev->functionName);
126         else {        
127            p += sprintf(p, "%.*s", precision, ev->functionName);
128            precision = -1;
129         }        
130         break;
131       case 'b': /* backtrace; called %throwable in LOG4J */
132       case 'B': /* short backtrace; called %throwable{short} in LOG4J */
133 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
134         {
135           xbt_ex_t e;
136           int i;
137           
138           e.used     = backtrace((void**)e.bt,XBT_BACKTRACE_SIZE);
139           e.bt_strings = NULL;
140           e.msg=NULL;
141           e.remote=0;
142           xbt_ex_setup_backtrace(&e);
143           if (*q=='B') {
144              if (precision == -1)
145                p += sprintf(p,"%s",e.bt_strings[2]+8);
146              else {           
147                 p += sprintf(p,"%.*s",precision, e.bt_strings[2]+8);
148                 precision = -1;
149              }   
150           } else {
151             for (i=2; i<e.used; i++)
152                if (precision == -1)
153                  p += sprintf(p,"%s\n",e.bt_strings[i]+8);
154              else {           
155                  p += sprintf(p,"%.*s\n",precision,e.bt_strings[i]+8);
156                 precision = -1;
157              }   
158           }
159            
160           xbt_ex_free(e);
161         }
162 #else
163         p+=sprintf(p,"(no backtrace on this arch)");
164 #endif
165         break;
166
167       case 'd': /* date; LOG4J compliant */
168         if (precision == -1)
169            p += sprintf(p,"%f", gras_os_time());
170         else {        
171            p += sprintf(p,"%.*f", precision, gras_os_time());
172            precision = -1;
173         }        
174         break;
175       case 'r': /* application age; LOG4J compliant */
176         if (precision == -1)
177            p += sprintf(p,"%f", gras_os_time()-begin_of_time);
178         else {        
179            p += sprintf(p,"%.*f", precision, gras_os_time()-begin_of_time);
180            precision = -1;
181         }        
182         break;
183         
184       case 'm': /* user-provided message; LOG4J compliant */
185         if (precision == -1)
186            p += vsprintf(p, msg_fmt, ev->ap);
187         else {        
188            p += vsnprintf(p, precision, msg_fmt, ev->ap);
189            precision = -1;
190         }        
191         break;
192
193       default:
194         THROW2(arg_error,0,"Unknown %%%c sequence in layout format (%s)",
195                *q,(char*)l->data);
196       }
197       q++;
198     } else {
199       *(p++) = *(q++);
200     }
201   }
202   *p = '\0';
203   return res;
204 }
205
206 static void xbt_log_layout_format_free(xbt_log_layout_t lay) {
207   free(lay->data);
208 }
209 xbt_log_layout_t xbt_log_layout_format_new(char *arg) {
210   xbt_log_layout_t res = xbt_new0(s_xbt_log_layout_t,1);
211   res->do_layout = xbt_log_layout_format_doit;
212   res->free_     = xbt_log_layout_format_free;
213   res->data = xbt_strdup((char*)arg);
214   return res;
215 }