1 /*
2    (C) 2007 by Argonne National Laboratory.
3        See COPYRIGHT in top-level directory.
4 */
5 #include "mpe_callstack_conf.h"
6 
7 #if defined( STDC_HEADERS ) || defined( HAVE_STDIO_H )
8 #include <stdio.h>
9 #endif
10 #if defined( STDC_HEADERS ) || defined( HAVE_STRING_H )
11 #include <string.h>
12 #endif
13 #if defined( HAVE_UNISTD_H )
14 #include <unistd.h>
15 #endif
16 
17 #include "mpe_callstack.h"
18 
19 /*@
20     void MPE_CallStack_fancyprint - A decorated version of
21                                     MPE_CallStack_print()
22 
23     Input Parameters:
24 + cstk        - pointer to the MPE_CallStack_t data structure.
25 . fd          - file descriptor of where output will go.
26 . prefix      - prefix string for each line of output.
27 . printidx    - boolean variable to indicate to print stack index,
28                 1=true, 0=false.
29 - maxframes   - maximum number of frames to be printed.
30                 MPE_CALLSTACK_UNLIMITED means unlimited frames.
31 @*/
MPE_CallStack_fancyprint(MPE_CallStack_t * cstk,int fd,const char * prefix,int printidx,int maxframes)32 void MPE_CallStack_fancyprint( MPE_CallStack_t *cstk, int fd,
33                                const char      *prefix,
34                                      int        printidx,
35                                      int        maxframes )
36 {
37     char   strbuf[ MPE_CALLSTACK_MAXLINE ];
38     int    printf_mode, idx;
39     int    ierr;
40 
41     MPE_CallStack_iteratorInit( cstk );
42 #if defined( HAVE_PRINTSTACK )
43     /* Take off the stackframe that contains this function call. */
44     MPE_CallStack_iteratorHasMore( cstk );
45 #endif
46 
47     printf_mode = printidx ? 1 : 0;
48     printf_mode = prefix != NULL ? printf_mode+2 : printf_mode;
49 
50     idx = 0;
51     while ( MPE_CallStack_iteratorHasMore( cstk ) && idx < maxframes ) {
52         switch( printf_mode ) {
53             case 3:
54                 sprintf( strbuf, "%s[%d]: %s\n", prefix, idx,
55                          MPE_CallStack_iteratorFetchNext( cstk ) );
56                 break;
57             case 2:
58                 sprintf( strbuf, "%s%s\n", prefix,
59                          MPE_CallStack_iteratorFetchNext( cstk ) );
60                 break;
61             case 1:
62                 sprintf( strbuf, "[%d]: %s\n", idx,
63                          MPE_CallStack_iteratorFetchNext( cstk ) );
64                 break;
65             default:
66                 sprintf( strbuf, "%s\n",
67                          MPE_CallStack_iteratorFetchNext( cstk ) );
68         }
69         ierr = write( fd, strbuf, strlen(strbuf)+1 );
70         idx++ ;
71     }
72 }
73