source: issm/trunk-jpl/src/c/io/PrintfFunction.cpp@ 12428

Last change on this file since 12428 was 12428, checked in by Mathieu Morlighem, 13 years ago

changing xmalloc to xNew and xfree to xDelete

File size: 1.4 KB
RevLine 
[9761]1/*\file PrintfFunction.c
2 *\brief: this function is used by the _printf_ macro, to take into account the
3 *fact we may be running on a cluster.
4 */
5
6#include <stdarg.h>
7#include <stdio.h>
8#include "../shared/shared.h"
9#include "../include/include.h"
10
[11199]11int PrintfFunction(const char* format,...){
[9761]12 /*http://linux.die.net/man/3/vsnprintf*/
13
14 /*string to be printed: */
15 char *buffer = NULL;
16 int n,size = 100;
17 int string_size;
18 extern int my_rank;
19 extern int num_procs;
20
21 //variable list of arguments
22 va_list args;
23
24 while(true){
25
26 /*allocate buffer for given string size*/
[12428]27 buffer=xNew<char>(size);
[9761]28
29 /* Try to print in the allocated space. */
30 va_start(args, format);
31#ifndef WIN32
32 n=vsnprintf(buffer,size,format,args);
33#else
34 n=vsnprintf(buffer,size,format,args);
35#endif
36 va_end(args);
37
38 /* If that worked, return the string. */
39 if(n>-1 && n<size) break;
40
41 /* Else try again with more space. */
42 if(n>-1) /* glibc 2.1 */
43 size=n+1; /* precisely what is needed */
44 else /* glibc 2.0 */
45 size*=2; /* twice the old size */
46
[12428]47 xDelete<char>(buffer);
[9761]48 }
49
50 /*Ok, if we are running in parallel, get node 0 to print*/
51 if(my_rank==0)printf(buffer);
52
53 /*Clean up and return*/
[12428]54 xDelete<char>(buffer);
[9761]55 return 1;
56}
Note: See TracBrowser for help on using the repository browser.